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-11 07:19:11 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-09-22 07:34:18 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
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-22 07:34:18 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-09-24 08:17:51 +01:00
|
|
|
$this->middleware(['auth', 'max_post_size_exceeded']);
|
2016-09-22 07:34:18 +01:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:57:58 +01:00
|
|
|
public function flip($photoId, $horizontal, $vertical)
|
|
|
|
{
|
|
|
|
$this->authorize('admin-access');
|
|
|
|
|
|
|
|
settype($direction, 'boolean');
|
|
|
|
|
|
|
|
$photo = Photo::where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
$photoService->flip($horizontal, $vertical);
|
|
|
|
}
|
|
|
|
|
2016-09-09 09:45:11 +01:00
|
|
|
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-22 07:34:18 +01:00
|
|
|
$isSuccessful = false;
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
foreach ($photoFiles as $photoFile)
|
|
|
|
{
|
|
|
|
$photoFile = UploadedFile::createFromBase($photoFile);
|
2016-09-22 07:34:18 +01:00
|
|
|
if ($photoFile->getError() != UPLOAD_ERR_OK)
|
|
|
|
{
|
|
|
|
Log::error('Image upload failed.', ['error' => $photoFile->getError(), 'reason' => $photoFile->getErrorMessage()]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/** @var File $savedFile */
|
|
|
|
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($photoFile);
|
|
|
|
|
|
|
|
$photo = new Photo();
|
|
|
|
$photo->album_id = $album->id;
|
|
|
|
$photo->user_id = Auth::user()->id;
|
|
|
|
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
|
|
|
|
$photo->file_name = $photoFile->getClientOriginalName();
|
|
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
|
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
|
|
$photo->file_size = $savedFile->getSize();
|
|
|
|
$photo->is_analysed = false;
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
$isSuccessful = true;
|
|
|
|
}
|
2016-09-05 12:01:30 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 09:04:07 +01:00
|
|
|
if ($request->isXmlHttpRequest())
|
|
|
|
{
|
2016-09-22 07:34:18 +01:00
|
|
|
return response()->json(['is_successful' => $isSuccessful]);
|
2016-09-11 09:04:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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');
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-09-24 08:17:51 +01:00
|
|
|
$archiveFile = UploadedFile::createFromBase($request->files->get('archive'));
|
|
|
|
if ($archiveFile->getError() != UPLOAD_ERR_OK)
|
|
|
|
{
|
|
|
|
Log::error('Bulk image upload failed.', ['error' => $archiveFile->getError(), 'reason' => $archiveFile->getErrorMessage()]);
|
|
|
|
$request->session()->flash('error', $archiveFile->getErrorMessage());
|
|
|
|
return redirect(route('albums.show', ['id' => $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-11 07:19:11 +01:00
|
|
|
if ($fileInfo->getFilename() == '__MACOSX' || substr($fileInfo->getFilename(), 0, 1) == '.')
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
@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-11 07:19:11 +01:00
|
|
|
$photo->user_id = Auth::user()->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)
|
|
|
|
{
|
2016-10-03 17:00:37 +01:00
|
|
|
$photoIds = $request->get('select-photo');
|
2016-09-11 09:04:07 +01:00
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
/** @var Album $album */
|
|
|
|
$album = Album::where('id', intval($albumId))->first();
|
|
|
|
|
|
|
|
if (is_null($album))
|
|
|
|
{
|
|
|
|
App::abort(404);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:00:37 +01:00
|
|
|
$action = $request->get('bulk-action');
|
|
|
|
$numberChanged = 0;
|
|
|
|
|
|
|
|
foreach ($photoIds as $photoId)
|
2016-09-07 21:44:28 +01:00
|
|
|
{
|
|
|
|
/** @var Photo $photo */
|
|
|
|
$photo = $album->photos()->where('id', intval($photoId))->first();
|
|
|
|
if (is_null($photo))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:00:37 +01:00
|
|
|
$photoService = new PhotoService($photo);
|
|
|
|
switch (strtolower($action))
|
|
|
|
{
|
|
|
|
case 'delete':
|
|
|
|
$photoService->delete();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_both':
|
|
|
|
$photoService->flip(true, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_horizontal':
|
|
|
|
$photoService->flip(true, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'flip_vertical':
|
|
|
|
$photoService->flip(false, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'refresh_thumbnails':
|
|
|
|
$photoService->regenerateThumbnails();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rotate_left':
|
|
|
|
$photoService->rotate(90);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'rotate_right':
|
|
|
|
$photoService->rotate(270);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$numberChanged++;
|
2016-09-07 21:44:28 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:00:37 +01:00
|
|
|
$request->session()->flash('success', trans_choice('admin.bulk_photos_changed', $numberChanged, ['number' => $numberChanged]));
|
|
|
|
|
2016-09-07 21:44:28 +01:00
|
|
|
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
|
|
|
}
|