Created
December 21, 2018 13:31
-
-
Save valex/35f01caebe21c82759603fca0c711097 to your computer and use it in GitHub Desktop.
Laravel Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\Events\CommentAdded; | |
use App\Helpers\DatetimeHelper; | |
use App\Helpers\YoutubeHelper; | |
use App\Http\Requests\AddComment; | |
use App\Http\Requests\StoreTrainViewRequest; | |
use App\Http\Requests\StoreWaypointsRequest; | |
use App\Http\Requests\UpdateView; | |
use App\Repositories\BlVideosRepository; | |
use App\Repositories\CountriesRepository; | |
use App\Repositories\SettingsRepository; | |
use App\Repositories\UserRepository; | |
use App\Repositories\VideoRepository; | |
use App\Repositories\ViewRepository; | |
use App\Repositories\WaypointsRepository; | |
use App\Transformers\CommentTransformer; | |
use App\Transformers\ViewFullTransformer; | |
use App\Transformers\ViewTransformer; | |
use App\Transformers\WaypointTransformer; | |
use App\View; | |
use Carbon\CarbonInterval; | |
use Storage; | |
class ViewsController extends Controller | |
{ | |
protected $blVideosRepository; | |
protected $countriesRepository; | |
protected $datetimeHelper; | |
protected $settingsRepository; | |
protected $videoRepository; | |
protected $viewRepository; | |
protected $userRepository; | |
protected $youtubeHelper; | |
protected $waypointsRepository; | |
public function __construct(BlVideosRepository $blVideosRepository, | |
CountriesRepository $countriesRepository, | |
DatetimeHelper $datetimeHelper, | |
SettingsRepository $settingsRepository, | |
VideoRepository $videoRepository, | |
ViewRepository $viewRepository, | |
UserRepository $userRepository, | |
YoutubeHelper $youtubeHelper, | |
WaypointsRepository $waypointsRepository) | |
{ | |
$this->blVideosRepository = $blVideosRepository; | |
$this->countriesRepository = $countriesRepository; | |
$this->datetimeHelper = $datetimeHelper; | |
$this->settingsRepository = $settingsRepository; | |
$this->videoRepository = $videoRepository; | |
$this->viewRepository = $viewRepository; | |
$this->userRepository = $userRepository; | |
$this->youtubeHelper = $youtubeHelper; | |
$this->waypointsRepository = $waypointsRepository; | |
} | |
public function show(View $view) | |
{ | |
$video = $view->video; | |
$lines = $view->lines; | |
$activeWaypoints = $this->waypointsRepository->activeWaypoints($view); | |
$similarViews = $view->similarViews(); | |
$comments = $view->comments()->orderBy('id', 'desc')->get(); | |
$data = [ | |
'meta' => [ | |
'title' => $view->header(), | |
'description' => $view->metaDescription(), | |
], | |
'view' => $view, | |
'lines' => $lines, | |
'video' => $video, | |
'activeWaypoints' => $activeWaypoints, | |
'similarViews' => $similarViews, | |
'comments' => $comments | |
]; | |
return view('views.show', $data); | |
} | |
public function waypoints(View $view) | |
{ | |
$video = $view->video; | |
$lines = $view->lines; | |
$activeWaypoints = $this->waypointsRepository->activeWaypoints($view); | |
$data = [ | |
'meta' => [ | |
'title' => trans('main.Mark route') | |
], | |
'view' => $view, | |
'lines' => $lines, | |
'video' => $video, | |
'activeWaypoints' => $activeWaypoints, | |
]; | |
$data['countriesSelectSource'] = $this->countriesRepository->selectSource(); | |
return view('views.waypoints', $data); | |
} | |
public function add() | |
{ | |
$data = [ | |
'meta' => [ | |
'title' => trans('main.Add new video') | |
], | |
]; | |
return view('views.add', $data); | |
} | |
public function edit(View $view) | |
{ | |
$data = [ | |
'meta' => [ | |
'title' => trans('main.Edit view') | |
], | |
]; | |
$data['view'] = $view; | |
$data['lines'] = $view->lines; | |
$data['video'] = $view->video; | |
$data['activeWaypoints'] = $this->waypointsRepository->activeWaypoints($view); | |
$data['aspectRatioSelectSource'] = $this->viewRepository->aspectRatioSelectSource(); | |
$data['camLocationSelectSource'] = $this->viewRepository->camLocationSelectSource(); | |
$data['videoQualitySelectSource'] = $this->viewRepository->videoQualitySelectSource(); | |
$data['youtubeAvailableStatusSelectSource'] = $this->viewRepository->youtubeAvailableStatusSelectSource(); | |
$data['countriesSelectSource'] = $this->countriesRepository->selectSource(); | |
return view('views.edit', $data); | |
} | |
public function update(UpdateView $request, View $view){ | |
$view->name = request('name'); | |
$view->country_id = request('country_id'); | |
$view->cam_location = request('cam_location'); | |
$view->video_quality = request('video_quality'); | |
$view->audio_quality = request('audio_quality'); | |
$view->video_accelerated = request('video_accelerated'); | |
$view->video_segmented = request('video_segmented'); | |
$view->aspect_ratio = request('aspect_ratio'); | |
$view->duration = request('duration'); | |
$view->start_from_sec = request('start_from_sec'); | |
$view->description = request('description'); | |
$view->youtube_available_status = request('youtube_available_status'); | |
$view->lines()->sync(request('lines')); | |
if(auth()->check() && auth()->user()->can('set-editable', $view)){ | |
$view->editable = boolval(request('editable')); | |
} | |
$view->save(); | |
return response()->json(fractal($view, new ViewFullTransformer())); | |
} | |
public function getFull(View $view){ | |
return response()->json(fractal($view, new ViewFullTransformer())); | |
} | |
public function addComment(View $view, AddComment $request){ | |
$commentAttributes = [ | |
'content' => trim($request->get('content')), | |
]; | |
if(auth()->check()) { | |
$commentAttributes['author_id'] = auth()->user()->getAuthIdentifier(); | |
} | |
$commentAttributes['ip'] = request()->ip(); | |
$comment = $view->comments()->create($commentAttributes); | |
event(new CommentAdded($comment)); | |
return response()->json(fractal($comment, new CommentTransformer())); | |
} | |
public function storeWaypoints(StoreWaypointsRequest $request, View $view){ | |
$waypoints = request('waypoints'); | |
// check editable | |
if( ! $view->isEditable()){ | |
$message = trans('main.This view is not editable'); | |
return response()->json([ | |
'waypoints' => [$message] | |
], 422); | |
} | |
// check the same timestamps | |
$hasTheSameTimeMarks = false; | |
$sameTimeMark = 0; | |
foreach ($waypoints as $key_i => $waypoint_i){ | |
foreach ($waypoints as $key_j => $waypoint_j){ | |
if($key_i == $key_j) | |
continue; | |
if($waypoint_i['seconds'] == $waypoint_j['seconds']){ | |
$hasTheSameTimeMarks = true; | |
$sameTimeMark = $waypoint_i['seconds']; | |
break 2; | |
} | |
} | |
} | |
if(true === $hasTheSameTimeMarks){ | |
$message = trans('main.Time marks cannot be the same', ['time_mark' => $this->datetimeHelper->secondsToHim($sameTimeMark)]); | |
return response()->json([ | |
'waypoints' => [$message] | |
], 422); | |
} | |
// SUCCESS | |
$lastRevision = $this->waypointsRepository->lastRevision($view); | |
$revision = 0; | |
if( ! is_null($lastRevision)){ | |
$revision = $lastRevision + 1; | |
} | |
foreach ($waypoints as $waypoint){ | |
$station_id = $waypoint['station_id']; | |
$name = null; | |
if($station_id === null){ | |
$name = $waypoint['name']; | |
} | |
$waypointAttributes=[ | |
'view_id' => $view->id, | |
'station_id' => $station_id, | |
'name' => $name, | |
'seconds' => $waypoint['seconds'], | |
'revision' => $revision, | |
]; | |
$this->waypointsRepository->create($waypointAttributes); | |
} | |
$view->setWaypointRevision($revision); | |
return response()->json([ | |
'success' => true | |
]); | |
} | |
public function store(StoreTrainViewRequest $request) | |
{ | |
$videoAttrs = [ | |
'source' => 'youtube.com' | |
]; | |
$youtubeKey = $this->youtubeHelper->extractKey($request->input('video_url')); | |
// check BlackList | |
if(true === $this->blVideosRepository->isYoutubeInBL($youtubeKey)){ | |
$message = trans('main.Such video in BL and you cannot add it'); | |
$message .= '. '; | |
$message .= trans('main.Please read our rules', ['url' => action('HomeController@rules')]); | |
return response()->json([ | |
'video_url' => [$message] | |
], 422); | |
} | |
// check if exist | |
$videoDB = $this->videoRepository->getYoutubeVideoByKey($youtubeKey); | |
if( ! is_null($videoDB)){ | |
$message = trans('main.Such video already exist'); | |
$viewDB = $videoDB->view; | |
if( ! is_null($viewDB)){ | |
$message .= ' '.trans('main.Look link', ['url' => action('ViewsController@show', [$viewDB])]); | |
} | |
return response()->json([ | |
'video_url' => [$message] | |
], 422); | |
} | |
// check embeddable | |
$youtubeUrl = '//www.youtube.com/watch?v='.$youtubeKey; | |
$videoAttrs['key'] = $youtubeKey; | |
$videoAttrs['url'] = $youtubeUrl; | |
// https://developers.google.com/youtube/v3/docs/videos | |
$youtubeVideo = \Youtube::getVideoInfo($youtubeKey); | |
if( ! $youtubeVideo){ | |
$message = trans('main.This video is unavailable'); | |
return response()->json([ | |
'video_url' => [$message] | |
], 422); | |
} | |
if(false === $youtubeVideo->status->embeddable){ | |
$message = trans('main.non_embeddable_video_error'); | |
return response()->json([ | |
'video_url' => [$message] | |
], 422); | |
} | |
// SUCCESS | |
$videoAttrs['name'] = $youtubeVideo->snippet->title; | |
$description = trim($youtubeVideo->snippet->description); | |
if(mb_strlen($description) > 0){ | |
$videoAttrs['description'] = $description; | |
} | |
$di = new \DateInterval($youtubeVideo->contentDetails->duration); // <== instance from another API | |
$ci = CarbonInterval::instance($di); | |
$duration = intval($ci->dayz) * 86400 + | |
intval($ci->hours) * 3600 + | |
intval($ci->minutes) * 60 + | |
intval($ci->seconds) * 1; | |
$videoAttrs['duration'] = $duration; | |
$video = $this->videoRepository->create($videoAttrs); | |
$view = app('App\View'); | |
$view->fill([ | |
'video_id' => $video->id, | |
'name' => $video->name, | |
'description' => $video->description, | |
'duration' => $video->duration, | |
'video_quality' => View::VIDEO_QUALITY_LOW, | |
]); | |
if(auth()->check()) { | |
auth()->user()->views()->save($view); | |
} else { | |
$user = $this->userRepository->superadmin(); | |
$user->views()->save($view); | |
} | |
// store images | |
$attachImagesToView = []; | |
$thumbnails = $youtubeVideo->snippet->thumbnails; | |
foreach ($thumbnails as $key => $thumbnail){ | |
//$filename = basename($thumbnail->url); | |
//$path = realpath(storage_path('app/public')); | |
//$fullpath = $path.DIRECTORY_SEPARATOR.$filename; | |
// store locally | |
// $interventionFile = \IImage::make($thumbnail->url); | |
// $interventionFile->save($fullpath, 100); | |
// store on cloud | |
//$cloudFile = Storage::disk('cloudinary')->putFile('photos', new File($fullpath)); | |
$width = null; | |
if(isset($thumbnail->width)) | |
$width = intval($thumbnail->width); | |
$height = null; | |
if(isset($thumbnail->height)) | |
$height = intval($thumbnail->height); | |
$image = app('App\Image'); | |
$image->fill([ | |
'disk' => 'youtube', | |
'dirname' => pathinfo($thumbnail->url, PATHINFO_DIRNAME), | |
'filename' => pathinfo($thumbnail->url, PATHINFO_BASENAME), | |
'template' => $key, | |
'width' => $width, | |
'height' => $height, | |
'size' => null, | |
'title' => null, | |
]); | |
$image->save(); | |
array_push($attachImagesToView, $image->id); | |
} | |
$view->images()->attach($attachImagesToView); | |
return response()->json(fractal($view, new ViewTransformer())); | |
} | |
public function ajaxActiveWaypoints(View $view){ | |
$activeWaypoints = $this->waypointsRepository->activeWaypoints($view); | |
return response()->json(fractal($activeWaypoints, new WaypointTransformer())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment