347 lines
8.9 KiB
PHP
347 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Album;
|
|
use App\AlbumSources\IAlbumSource;
|
|
use App\Facade\Image;
|
|
use App\Facade\Theme;
|
|
use App\Helpers\ImageHelper;
|
|
use App\Helpers\MiscHelper;
|
|
use App\Photo;
|
|
use App\Services\PhotoService;
|
|
use App\Upload;
|
|
use App\UploadPhoto;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
class PhotoController extends Controller
|
|
{
|
|
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();
|
|
|
|
// Remove the photo if it cannot be analysed
|
|
$photo->delete();
|
|
}
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
$photoService = new PhotoService($photo);
|
|
$photoService->delete();
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
|
|
$photoFiles = $request->files->get('photo');
|
|
|
|
// Load the linked album
|
|
$album = $this->loadAlbum($request->get('album_id'));
|
|
|
|
foreach ($photoFiles as $photoFile)
|
|
{
|
|
$photoFile = UploadedFile::createFromBase($photoFile);
|
|
|
|
/** @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();
|
|
}
|
|
|
|
return redirect(route('albums.analyse', [
|
|
'id' => $album->id
|
|
]));
|
|
}
|
|
|
|
public function storeBulk(Request $request)
|
|
{
|
|
$this->authorize('admin-access');
|
|
|
|
$archiveFile = UploadedFile::createFromBase($request->files->get('archive'));
|
|
|
|
// Load the linked album
|
|
$album = $this->loadAlbum($request->get('album_id'));
|
|
|
|
// Create a temporary folder to hold the extracted files
|
|
$tempFolder = sprintf('%s/btw_upload_%s', env('TEMP_FOLDER', '/tmp'), MiscHelper::randomString());
|
|
mkdir($tempFolder);
|
|
|
|
$mimeType = strtolower($archiveFile->getMimeType());
|
|
switch ($mimeType)
|
|
{
|
|
case 'application/zip':
|
|
$zip = new \ZipArchive();
|
|
$zip->open($archiveFile->getPathname());
|
|
$zip->extractTo($tempFolder);
|
|
$zip->close();
|
|
break;
|
|
|
|
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]));
|
|
}
|
|
|
|
$di = new \RecursiveDirectoryIterator($tempFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
|
|
$recursive = new \RecursiveIteratorIterator($di);
|
|
|
|
/** @var \SplFileInfo $fileInfo */
|
|
foreach ($recursive as $fileInfo)
|
|
{
|
|
if ($fileInfo->isDir())
|
|
{
|
|
if ($fileInfo->getFilename() == '__MACOSX' || substr($fileInfo->getFilename(), 0, 1) == '.')
|
|
{
|
|
@rmdir($fileInfo->getPathname());
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
$result = getimagesize($fileInfo->getPathname());
|
|
if ($result === false)
|
|
{
|
|
// Not an image file - skip
|
|
@unlink($fileInfo->getPathname());
|
|
continue;
|
|
}
|
|
|
|
$photoFile = new File($fileInfo->getPathname());
|
|
|
|
/** @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->getFilename(), PATHINFO_FILENAME);
|
|
$photo->file_name = $photoFile->getFilename();
|
|
$photo->storage_file_name = $savedFile->getFilename();
|
|
$photo->mime_type = $savedFile->getMimeType();
|
|
$photo->file_size = $savedFile->getSize();
|
|
$photo->is_analysed = false;
|
|
$photo->save();
|
|
}
|
|
|
|
@rmdir($tempFolder);
|
|
|
|
return redirect(route('albums.analyse', [
|
|
'id' => $album->id
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
//
|
|
}
|
|
|
|
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)));
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
}
|