2016-09-02 21:27:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
use App\Album;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\AlbumSources\IAlbumSource;
|
|
|
|
use App\Facade\Image;
|
|
|
|
use App\Facade\Theme;
|
|
|
|
use App\Helpers\ImageHelper;
|
2016-09-05 12:56:13 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Photo;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\Services\PhotoService;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Upload;
|
|
|
|
use App\UploadPhoto;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2016-09-07 21:44:28 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2016-09-05 12:56:13 +01:00
|
|
|
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
2016-09-02 22:00:42 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2016-09-02 21:27:50 +01:00
|
|
|
|
|
|
|
class PhotoController extends Controller
|
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
public function analyse($photoId)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = Photo::where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = ['is_successful' => false, 'message' => ''];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->analyse();
|
|
|
|
|
|
|
|
$result['is_successful'] = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$result['is_successful'] = false;
|
|
|
|
$result['message'] = $ex->getMessage();
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
// Remove the photo if it cannot be analysed
|
2016-09-08 23:22:29 +01:00
|
|
|
$photo->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($result);
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = Photo::where('id', intval($id))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:09:03 +01:00
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->delete();
|
2016-09-09 09:45:11 +01:00
|
|
|
|
|
|
|
return back();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function regenerateThumbnails($photoId)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = Photo::where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = ['is_successful' => false, 'message' => ''];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->regenerateThumbnails();
|
|
|
|
|
|
|
|
$result['is_successful'] = true;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$result['is_successful'] = false;
|
|
|
|
$result['message'] = $ex->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($result);
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function rotate($photoId, $angle)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
$photo = Photo::where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($angle != 90 && $angle != 180 && $angle != 270)
|
|
|
|
{
|
|
|
|
App::aport(400);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->rotate($angle);
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
$photoFiles = $request->files->get('photo');
|
2016-09-02 21:27:50 +01:00
|
|
|
|
|
|
|
// Load the linked album
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($request->get('album_id'));
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
foreach ($photoFiles as $photoFile)
|
|
|
|
{
|
|
|
|
$photoFile = UploadedFile::createFromBase($photoFile);
|
|
|
|
|
|
|
|
/** @var File $savedFile */
|
2016-09-08 23:22:29 +01:00
|
|
|
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($photoFile);
|
2016-09-05 12:01:30 +01:00
|
|
|
|
|
|
|
$photo = new Photo();
|
|
|
|
$photo->album_id = $album->id;
|
2016-09-06 19:47:25 +01:00
|
|
|
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
|
|
|
|
$photo->file_name = $photoFile->getClientOriginalName();
|
|
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
2016-09-05 12:01:30 +01:00
|
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
|
|
$photo->file_size = $savedFile->getSize();
|
2016-09-08 23:22:29 +01:00
|
|
|
$photo->is_analysed = false;
|
2016-09-05 12:01:30 +01:00
|
|
|
$photo->save();
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return redirect(route('albums.analyse', [
|
|
|
|
'id' => $album->id
|
2016-09-05 12:56:13 +01:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function storeBulk(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
$archiveFile = UploadedFile::createFromBase($request->files->get('archive'));
|
|
|
|
|
|
|
|
// Load the linked album
|
2016-09-08 23:22:29 +01:00
|
|
|
$album = $this->loadAlbum($request->get('album_id'));
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
// Create a temporary folder to hold the extracted files
|
|
|
|
$tempFolder = sprintf('%s/btw_upload_%s', env('TEMP_FOLDER', '/tmp'), MiscHelper::randomString());
|
|
|
|
mkdir($tempFolder);
|
|
|
|
|
2016-09-05 14:06:41 +01:00
|
|
|
$mimeType = strtolower($archiveFile->getMimeType());
|
|
|
|
switch ($mimeType)
|
2016-09-05 12:56:13 +01:00
|
|
|
{
|
|
|
|
case 'application/zip':
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
$zip->open($archiveFile->getPathname());
|
|
|
|
$zip->extractTo($tempFolder);
|
|
|
|
$zip->close();
|
|
|
|
break;
|
2016-09-05 14:06:41 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
$request->session()->flash('error', sprintf('The file type "%s" is not supported for bulk uploads.', $mimeType));
|
|
|
|
return redirect(route('albums.show', ['id' => $album->id]));
|
2016-09-05 12:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$di = new \RecursiveDirectoryIterator($tempFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
|
2016-09-05 14:06:41 +01:00
|
|
|
$recursive = new \RecursiveIteratorIterator($di);
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
/** @var \SplFileInfo $fileInfo */
|
2016-09-05 14:06:41 +01:00
|
|
|
foreach ($recursive as $fileInfo)
|
2016-09-05 12:56:13 +01:00
|
|
|
{
|
2016-09-05 14:06:41 +01:00
|
|
|
if ($fileInfo->isDir())
|
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
if ($fileInfo->getFilename() == '__MACOSX')
|
|
|
|
{
|
|
|
|
@rmdir($fileInfo->getPathname());
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = getimagesize($fileInfo->getPathname());
|
|
|
|
if ($result === false)
|
|
|
|
{
|
|
|
|
// Not an image file - skip
|
|
|
|
@unlink($fileInfo->getPathname());
|
2016-09-05 14:06:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:56:13 +01:00
|
|
|
$photoFile = new File($fileInfo->getPathname());
|
|
|
|
|
|
|
|
/** @var File $savedFile */
|
2016-09-08 23:22:29 +01:00
|
|
|
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($photoFile);
|
2016-09-05 12:56:13 +01:00
|
|
|
|
|
|
|
$photo = new Photo();
|
|
|
|
$photo->album_id = $album->id;
|
2016-09-06 19:47:25 +01:00
|
|
|
$photo->name = pathinfo($photoFile->getFilename(), PATHINFO_FILENAME);
|
|
|
|
$photo->file_name = $photoFile->getFilename();
|
|
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
2016-09-05 12:56:13 +01:00
|
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
|
|
$photo->file_size = $savedFile->getSize();
|
2016-09-08 23:22:29 +01:00
|
|
|
$photo->is_analysed = false;
|
2016-09-05 12:56:13 +01:00
|
|
|
$photo->save();
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
@rmdir($tempFolder);
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return redirect(route('albums.analyse', [
|
|
|
|
'id' => $album->id
|
2016-09-05 12:01:30 +01:00
|
|
|
]));
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
public function updateBulk(Request $request, $albumId)
|
|
|
|
{
|
|
|
|
$photos = $request->get('photo');
|
|
|
|
|
|
|
|
/** @var Album $album */
|
|
|
|
$album = Album::where('id', intval($albumId))->first();
|
|
|
|
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($photos as $photoId => $value)
|
|
|
|
{
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = $album->photos()->where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photo->fill($value);
|
|
|
|
$photo->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('albums.show', array('id' => $albumId)));
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return Album
|
|
|
|
*/
|
|
|
|
private function loadAlbum($id)
|
|
|
|
{
|
|
|
|
$album = Album::where('id', intval($id))->first();
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $album;
|
|
|
|
}
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|