Zip files can now be extracted and uploaded

This commit is contained in:
Andy Heathershaw 2016-09-05 12:56:13 +01:00
parent 80dd1e4a40
commit 56701c78d6
3 changed files with 84 additions and 0 deletions

View File

@ -2,12 +2,14 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Helpers\MiscHelper;
use App\Photo; use App\Photo;
use App\Upload; use App\Upload;
use App\UploadPhoto; use App\UploadPhoto;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
class PhotoController extends Controller class PhotoController extends Controller
@ -87,6 +89,72 @@ class PhotoController extends Controller
])); ]));
} }
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);
switch (strtolower($archiveFile->getMimeType()))
{
case 'application/zip':
$zip = new \ZipArchive();
$zip->open($archiveFile->getPathname());
$zip->extractTo($tempFolder);
$zip->close();
break;
}
$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);
/** @var \SplFileInfo $fileInfo */
foreach ($di as $fileInfo)
{
$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. * Display the specified resource.
* *

View File

@ -32,6 +32,21 @@
{!! Form::submit(trans('forms.upload_action'), ['class' => 'btn btn-success']) !!} {!! Form::submit(trans('forms.upload_action'), ['class' => 'btn btn-success']) !!}
</div> </div>
{!! Form::close() !!} {!! Form::close() !!}
<hr/>
<h4>Bulk upload</h4>
{!! Form::open(['route' => 'photos.storeBulk', 'method' => 'POST', 'files' => true]) !!}
{!! Form::hidden('album_id', $album->id) !!}
<div class="form-group">
{!! Form::file('archive', ['class' => 'control-label']) !!}
</div>
<div>
{!! Form::submit(trans('forms.upload_action'), ['class' => 'btn btn-success']) !!}
</div>
{!! Form::close() !!}
</div> </div>
<div role="tabpanel" class="tab-pane" id="settings-tab"> <div role="tabpanel" class="tab-pane" id="settings-tab">

View File

@ -24,6 +24,7 @@ Route::group(['prefix' => 'admin'], function () {
Route::resource('albums', 'Admin\AlbumController'); Route::resource('albums', 'Admin\AlbumController');
// Photo management // Photo management
Route::post('photos/store-bulk', 'Admin\PhotoController@storeBulk')->name('photos.storeBulk');
Route::resource('photos', 'Admin\PhotoController'); Route::resource('photos', 'Admin\PhotoController');
}); });