2016-09-02 21:27:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2016-09-05 12:56:13 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Photo;
|
|
|
|
use App\Upload;
|
|
|
|
use App\UploadPhoto;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
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
|
|
|
|
$album = AlbumController::loadAlbum($request->get('album_id'));
|
|
|
|
|
|
|
|
$upload = new Upload();
|
2016-09-05 12:01:30 +01:00
|
|
|
$upload->album_id = $album->id;
|
2016-09-02 21:27:50 +01:00
|
|
|
$upload->is_completed = false;
|
|
|
|
$upload->is_processing = false;
|
2016-09-05 12:01:30 +01:00
|
|
|
$upload->is_ready = false;
|
|
|
|
$upload->number_photos = 0;
|
2016-09-02 21:27:50 +01:00
|
|
|
$upload->save();
|
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
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;
|
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();
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
$upload->number_photos++;
|
|
|
|
|
|
|
|
$uploadPhoto = new UploadPhoto();
|
|
|
|
$uploadPhoto->upload_id = $upload->id;
|
|
|
|
$uploadPhoto->photo_id = $photo->id;
|
|
|
|
$uploadPhoto->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$upload->is_ready = true;
|
2016-09-05 12:56:13 +01:00
|
|
|
$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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
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())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-05 12:56:13 +01:00
|
|
|
$photoFile = new File($fileInfo->getPathname());
|
|
|
|
|
|
|
|
/** @var File $savedFile */
|
|
|
|
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($album, $photoFile);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$photo->save();
|
|
|
|
|
|
|
|
$upload->number_photos++;
|
|
|
|
|
|
|
|
$uploadPhoto = new UploadPhoto();
|
|
|
|
$uploadPhoto->upload_id = $upload->id;
|
|
|
|
$uploadPhoto->photo_id = $photo->id;
|
|
|
|
$uploadPhoto->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$upload->is_ready = true;
|
2016-09-05 12:01:30 +01:00
|
|
|
$upload->save();
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
return redirect(route('albums.monitorUpload', [
|
|
|
|
'id' => $album->id,
|
|
|
|
'upload_id' => $upload->id
|
|
|
|
]));
|
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)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|