mein-sterntours/packages/iqcontent/laravel-filemanager/src/Controllers/FileController.php
2019-11-21 21:12:17 +01:00

253 lines
No EOL
7 KiB
PHP

<?php
namespace IqContent\LaravelFilemanager\Controllers;
use Intervention\Image\Facades\Image;
use IqContent\LaravelFilemanager\Events\ImageIsCropping;
use IqContent\LaravelFilemanager\Events\ImageWasCropped;
use IqContent\LaravelFilemanager\Models\IQContentFile;
class FileController extends LfmController
{
private $apikey = 'AIzaSyC31Tf0rZVYqRRcUNk4XXPtV5yzKDzkb1E';
/**
* Show crop page.
*
* @return mixed
*/
public function getFile()
{
return view('laravel-filemanager::file')
->with([
'working_dir' => request('working_dir'),
]);
}
/**
* Crop the image (called via ajax).
*/
public function performFile($overWrite = true)
{
//$working_dir = request('working_dir');
$file_type = request('file_type');
if($file_type === 'youtube'){
return $this->createYoutubeFile($file_type);
}
/* if(!$file_name || $file_name == ""){
$file_name = "newEmptyFile.".$file_type;
}else{
$file_name = $file_name.".".$file_type;
}
$file_name = request('file_name');
$file_content = request('file_content');*/
return $this->response('error', "unbekannter File Typ");
}
private function createYoutubeFile($file_type){
$youtube_video_id = request('file_content');
$value = $this->readGoogleApi($youtube_video_id);
if(!$value || !isset($value['items'][0]['snippet']['title'])){
return $this->response('error', "youtube ID nicht gefunden: ".$youtube_video_id);
}
$file_title = $value['items'][0]['snippet']['title'];
if (config('lfm.alphanumeric_filename')) {
$file_title = $this->helper->sanitize($file_title);
}
$file_name = $file_title.".".$file_type;
if ($this->lfm->setName($file_name)->exists()) {
return $this->response('error', parent::getError('move-exist'));
}
$new_file = $this->lfm->pretty($file_name);
$working_folder_id = $new_file->getModelParentFolderId();
$file_path = $new_file->path();
//Storage::disk('local')->put('file.txt', 'Contents');
$new_file->put($youtube_video_id);
$mimeType = $file_type;
$extension = $file_type;
$size = \File::size($file_path);
//thumbnails
if(isset($value['items'][0]['snippet']['thumbnails']['standard']['url'])) {
// create folder for thumbnails
$this->lfm->makeThumbnailURL($file_title.".jpg", $value['items'][0]['snippet']['thumbnails']['standard']['url']);
//image from url
// $image = Image::make($value['items'][0]['snippet']['thumbnails']['standard']['url']);
// $image->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200));
// $this->lfm->storage->put($image->stream()->detach());
}
IQContentFile::create([
'folder_id' => $working_folder_id,
'name' => $new_file->name(),
'identifier' => $new_file->name(),
'ext' => $extension,
'mine' => $mimeType,
'size' => $size / 1000,
'dimensions' => '',
'content' => $youtube_video_id,
]);
return parent::$success_response;
}
private function readGoogleApi($videoId){
$googleApiUrl = 'https://www.googleapis.com/youtube/v3/videos?id=' . $videoId . '&key=' . $this->apikey . '&part=snippet';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
return $value;
}
}
// $title = $value['items'][0]['snippet']['title'];
// $description = $value['items'][0]['snippet']['description'];
/*
*
*
array(4) {
["kind"]=>
string(25) "youtube#videoListResponse"
["etag"]=>
string(57) ""j6xRRd8dTPVVptg711_CSPADRfg/TOFYJKnMf1x6cO7CgAzk3K2BaRw""
["pageInfo"]=>
array(2) {
["totalResults"]=>
int(1)
["resultsPerPage"]=>
int(1)
}
["items"]=>
array(1) {
[0]=>
array(4) {
["kind"]=>
string(13) "youtube#video"
["etag"]=>
string(57) ""j6xRRd8dTPVVptg711_CSPADRfg/pVQe28OgNai40jPirl_G00FJDw8""
["id"]=>
string(11) "Rpo5py2fjrg"
["snippet"]=>
array(10) {
["publishedAt"]=>
string(24) "2018-12-23T22:42:41.000Z"
["channelId"]=>
string(24) "UCZZ5ZL0Wiv90RF2KDuR_mFA"
["title"]=>
string(16) "Wir: STERN TOURS"
["description"]=>
string(55) "Reiseveranstalter und Kulturreise-Spezialist aus Berlin"
["thumbnails"]=>
array(5) {
["default"]=>
array(3) {
["url"]=>
string(46) "https://i.ytimg.com/vi/Rpo5py2fjrg/default.jpg"
["width"]=>
int(120)
["height"]=>
int(90)
}
["medium"]=>
array(3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/Rpo5py2fjrg/mqdefault.jpg"
["width"]=>
int(320)
["height"]=>
int(180)
}
["high"]=>
array(3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/Rpo5py2fjrg/hqdefault.jpg"
["width"]=>
int(480)
["height"]=>
int(360)
}
["standard"]=>
array(3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/Rpo5py2fjrg/sddefault.jpg"
["width"]=>
int(640)
["height"]=>
int(480)
}
["maxres"]=>
array(3) {
["url"]=>
string(52) "https://i.ytimg.com/vi/Rpo5py2fjrg/maxresdefault.jpg"
["width"]=>
int(1280)
["height"]=>
int(720)
}
}
["channelTitle"]=>
string(10) "SternTours"
["tags"]=>
array(1) {
[0]=>
string(11) "STERN TOURS"
}
["categoryId"]=>
string(2) "19"
["liveBroadcastContent"]=>
string(4) "none"
["localized"]=>
array(2) {
["title"]=>
string(16) "Wir: STERN TOURS"
["description"]=>
string(55) "Reiseveranstalter und Kulturreise-Spezialist aus Berlin"
}
}
}
}
}
*/
//Preview image
//https://img.youtube.com/vi/Rpo5py2fjrg/hqdefault.jpg
//https://www.googleapis.com/youtube/v3/videos
//https://www.googleapis.com/youtube/v3/videos?part=snippet&id={YOUTUBE_VIDEO_ID}&fields=items(id%2Csnippet)&key={YOUR_API_KEY}
//http://gdata.youtube.com/feeds/api/videos/Rpo5py2fjrg