blue-twilight/app/Http/Controllers/Admin/PhotoController.php
2016-09-05 14:06:41 +01:00

214 lines
5.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Helpers\MiscHelper;
use App\Photo;
use App\Upload;
use App\UploadPhoto;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
use Symfony\Component\HttpFoundation\File\File;
class PhotoController extends Controller
{
/**
* 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()
{
//
}
/**
* 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 = AlbumController::loadAlbum($request->get('album_id'));
$upload = new Upload();
$upload->album_id = $album->id;
$upload->is_completed = false;
$upload->is_processing = false;
$upload->is_ready = false;
$upload->number_photos = 0;
$upload->save();
foreach ($photoFiles as $photoFile)
{
$photoFile = UploadedFile::createFromBase($photoFile);
/** @var File $savedFile */
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($album, $photoFile);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->name = $photoFile->getClientOriginalName();
$photo->file_name = $savedFile->getFilename();
$photo->mime_type = $savedFile->getMimeType();
$photo->file_size = $savedFile->getSize();
$photo->save();
$upload->number_photos++;
$uploadPhoto = new UploadPhoto();
$uploadPhoto->upload_id = $upload->id;
$uploadPhoto->photo_id = $photo->id;
$uploadPhoto->save();
}
$upload->is_ready = true;
$upload->save();
return redirect(route('albums.monitorUpload', [
'id' => $album->id,
'upload_id' => $upload->id
]));
}
public function storeBulk(Request $request)
{
$this->authorize('admin-access');
$archiveFile = UploadedFile::createFromBase($request->files->get('archive'));
// Load the linked album
$album = AlbumController::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]));
}
$upload = new Upload();
$upload->album_id = $album->id;
$upload->is_completed = false;
$upload->is_processing = false;
$upload->is_ready = false;
$upload->number_photos = 0;
$upload->save();
$di = new \RecursiveDirectoryIterator($tempFolder, \RecursiveDirectoryIterator::SKIP_DOTS);
$recursive = new \RecursiveIteratorIterator($di);
/** @var \SplFileInfo $fileInfo */
foreach ($recursive as $fileInfo)
{
if ($fileInfo->isDir())
{
continue;
}
$photoFile = new File($fileInfo->getPathname());
/** @var File $savedFile */
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($album, $photoFile);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->name = $photoFile->getFilename();
$photo->file_name = $savedFile->getFilename();
$photo->mime_type = $savedFile->getMimeType();
$photo->file_size = $savedFile->getSize();
$photo->save();
$upload->number_photos++;
$uploadPhoto = new UploadPhoto();
$uploadPhoto->upload_id = $upload->id;
$uploadPhoto->photo_id = $photo->id;
$uploadPhoto->save();
}
$upload->is_ready = true;
$upload->save();
return redirect(route('albums.monitorUpload', [
'id' => $album->id,
'upload_id' => $upload->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)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}