Created my own file-system abstraction layer for album storage
This commit is contained in:
parent
9360d8bbbe
commit
6e04389e07
@ -3,7 +3,7 @@
|
|||||||
<component name="WebServers">
|
<component name="WebServers">
|
||||||
<option name="servers">
|
<option name="servers">
|
||||||
<webServer id="b14a34b0-0127-4886-964a-7be75a2281ac" name="Development" url="http://blue-twilight-dev.andys.eu">
|
<webServer id="b14a34b0-0127-4886-964a-7be75a2281ac" name="Development" url="http://blue-twilight-dev.andys.eu">
|
||||||
<fileTransfer host="andyhaa1.miniserver.com" port="22" privateKey="C:\Users\andyheathershaw\.ssh\id_rsa" rootFolder="/srv/www/blue-twilight-dev" accessType="SFTP" keyPair="true">
|
<fileTransfer host="andyhaa1.miniserver.com" port="22" rootFolder="/srv/www/blue-twilight-dev" accessType="SFTP">
|
||||||
<advancedOptions>
|
<advancedOptions>
|
||||||
<advancedOptions dataProtectionLevel="Private" />
|
<advancedOptions dataProtectionLevel="Private" />
|
||||||
</advancedOptions>
|
</advancedOptions>
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
|
use App\AlbumSources\IAlbumSource;
|
||||||
|
use App\AlbumSources\LocalFilesystemSource;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
@ -41,9 +43,12 @@ class Album extends Model
|
|||||||
$this->url_alias = ucfirst(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
|
$this->url_alias = ucfirst(preg_replace('/[^a-z0-9\-]/', '-', strtolower($this->name)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUploadDisk()
|
/**
|
||||||
|
* @return IAlbumSource
|
||||||
|
*/
|
||||||
|
public function getAlbumSource()
|
||||||
{
|
{
|
||||||
// TODO allow albums to specify a storage location
|
// TODO allow albums to specify different storage locations - e.g. Amazon S3, SFTP/FTP, OpenStack
|
||||||
return 'local';
|
return new LocalFilesystemSource(dirname(__DIR__) . '/storage/app');
|
||||||
}
|
}
|
||||||
}
|
}
|
20
app/AlbumSources/IAlbumSource.php
Normal file
20
app/AlbumSources/IAlbumSource.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\AlbumSources;
|
||||||
|
|
||||||
|
use App\Album;
|
||||||
|
use App\Photo;
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
|
interface IAlbumSource
|
||||||
|
{
|
||||||
|
function getPathToPhoto(Album $album, Photo $photo);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves an uploaded file to the container and returns the filename.
|
||||||
|
* @param Album $album The album containing the photo
|
||||||
|
* @param File $uploadedFile The photo uploaded
|
||||||
|
* @return File
|
||||||
|
*/
|
||||||
|
function saveUploadedPhoto(Album $album, File $uploadedFile);
|
||||||
|
}
|
42
app/AlbumSources/LocalFilesystemSource.php
Normal file
42
app/AlbumSources/LocalFilesystemSource.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\AlbumSources;
|
||||||
|
|
||||||
|
use App\Album;
|
||||||
|
use App\Helpers\MiscHelper;
|
||||||
|
use App\Photo;
|
||||||
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
|
class LocalFilesystemSource implements IAlbumSource
|
||||||
|
{
|
||||||
|
private $parentFolder;
|
||||||
|
|
||||||
|
public function __construct($parentFolder)
|
||||||
|
{
|
||||||
|
$this->parentFolder = $parentFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPathToPhoto(Album $album, Photo $photo)
|
||||||
|
{
|
||||||
|
return sprintf('%s/%s', $this->getPathToAlbum($album), $photo->file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveUploadedPhoto(Album $album, File $uploadedFile)
|
||||||
|
{
|
||||||
|
$tempFilename = sprintf('%s/photo_%s', $this->getPathToAlbum($album), MiscHelper::randomString(20));
|
||||||
|
|
||||||
|
$extension = $uploadedFile->guessExtension();
|
||||||
|
if (!is_null($extension))
|
||||||
|
{
|
||||||
|
$tempFilename .= '.' . $extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uploadedFile->move(dirname($tempFilename), basename($tempFilename));
|
||||||
|
return new File($tempFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPathToAlbum(Album $album)
|
||||||
|
{
|
||||||
|
return sprintf('%s/albums/%s', $this->parentFolder, $album->url_alias);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
use App\Album;
|
use App\Album;
|
||||||
|
use App\AlbumSources\IAlbumSource;
|
||||||
use App\Photo;
|
use App\Photo;
|
||||||
use App\Upload;
|
use App\Upload;
|
||||||
use App\UploadPhoto;
|
use App\UploadPhoto;
|
||||||
@ -70,10 +71,11 @@ class ProcessUploadCommand extends Command
|
|||||||
$photo = $uploadPhoto->photo;
|
$photo = $uploadPhoto->photo;
|
||||||
$this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name));
|
$this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name));
|
||||||
|
|
||||||
|
/** @var Album $album */
|
||||||
$album = $photo->album;
|
$album = $photo->album;
|
||||||
|
$albumSource = $album->getAlbumSource();
|
||||||
|
|
||||||
$photoFile = Storage::path(sprintf('albums/%s/%s', $album->url_alias, $photo->file_name), $album->getUploadDisk());
|
$photoFile = $albumSource->getPathToPhoto($album, $photo);
|
||||||
dump($photoFile);
|
|
||||||
|
|
||||||
dump(@exif_read_data($photoFile));
|
dump(@exif_read_data($photoFile));
|
||||||
}
|
}
|
||||||
|
19
app/Helpers/MiscHelper.php
Normal file
19
app/Helpers/MiscHelper.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Helpers;
|
||||||
|
|
||||||
|
class MiscHelper
|
||||||
|
{
|
||||||
|
public static function randomString($length = 10)
|
||||||
|
{
|
||||||
|
$seed = 'abcdefghijklmnopqrstuvwxyz01234567890';
|
||||||
|
$string = '';
|
||||||
|
|
||||||
|
while (strlen($string) < $length)
|
||||||
|
{
|
||||||
|
$string .= substr($seed, rand(0, strlen($seed) - 1), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
}
|
@ -2,17 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
use App\Album;
|
|
||||||
use App\Photo;
|
use App\Photo;
|
||||||
use App\Upload;
|
use App\Upload;
|
||||||
use App\UploadPhoto;
|
use App\UploadPhoto;
|
||||||
use Illuminate\Http\File;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
use App\Http\Requests;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
class PhotoController extends Controller
|
class PhotoController extends Controller
|
||||||
{
|
{
|
||||||
@ -52,17 +48,15 @@ class PhotoController extends Controller
|
|||||||
// Load the linked album
|
// Load the linked album
|
||||||
$album = AlbumController::loadAlbum($request->get('album_id'));
|
$album = AlbumController::loadAlbum($request->get('album_id'));
|
||||||
|
|
||||||
$storageLocation = sprintf('albums/%s', $album->url_alias);
|
/** @var File $savedFile */
|
||||||
|
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($album, $photoFile);
|
||||||
/** @var \SplFileInfo $savedFile */
|
|
||||||
$savedFilePath = $photoFile->store($storageLocation, $album->getUploadDisk());
|
|
||||||
|
|
||||||
$photo = new Photo();
|
$photo = new Photo();
|
||||||
$photo->album_id = $album->id;
|
$photo->album_id = $album->id;
|
||||||
$photo->name = $photoFile->getClientOriginalName();
|
$photo->name = $photoFile->getClientOriginalName();
|
||||||
$photo->file_name = basename($savedFilePath);
|
$photo->file_name = $savedFile->getFilename();
|
||||||
$photo->mime_type = $photoFile->getClientMimeType();
|
$photo->mime_type = $savedFile->getMimeType();
|
||||||
$photo->file_size = $photoFile->getSize();
|
$photo->file_size = $savedFile->getSize();
|
||||||
$photo->save();
|
$photo->save();
|
||||||
|
|
||||||
$upload = new Upload();
|
$upload = new Upload();
|
||||||
|
Loading…
Reference in New Issue
Block a user