Created my own file-system abstraction layer for album storage

This commit is contained in:
Andy Heathershaw 2016-09-02 22:00:42 +01:00
parent 9360d8bbbe
commit 6e04389e07
7 changed files with 100 additions and 18 deletions

2
.idea/webServers.xml generated
View File

@ -3,7 +3,7 @@
<component name="WebServers">
<option name="servers">
<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 dataProtectionLevel="Private" />
</advancedOptions>

View File

@ -2,6 +2,8 @@
namespace App;
use App\AlbumSources\IAlbumSource;
use App\AlbumSources\LocalFilesystemSource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Notifications\Notifiable;
@ -41,9 +43,12 @@ class Album extends Model
$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
return 'local';
// TODO allow albums to specify different storage locations - e.g. Amazon S3, SFTP/FTP, OpenStack
return new LocalFilesystemSource(dirname(__DIR__) . '/storage/app');
}
}

View 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);
}

View 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);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Console\Commands;
use App\Album;
use App\AlbumSources\IAlbumSource;
use App\Photo;
use App\Upload;
use App\UploadPhoto;
@ -70,10 +71,11 @@ class ProcessUploadCommand extends Command
$photo = $uploadPhoto->photo;
$this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name));
/** @var Album $album */
$album = $photo->album;
$albumSource = $album->getAlbumSource();
$photoFile = Storage::path(sprintf('albums/%s/%s', $album->url_alias, $photo->file_name), $album->getUploadDisk());
dump($photoFile);
$photoFile = $albumSource->getPathToPhoto($album, $photo);
dump(@exif_read_data($photoFile));
}

View 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;
}
}

View File

@ -2,17 +2,13 @@
namespace App\Http\Controllers\Admin;
use App\Album;
use App\Photo;
use App\Upload;
use App\UploadPhoto;
use Illuminate\Http\File;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\File;
class PhotoController extends Controller
{
@ -52,17 +48,15 @@ class PhotoController extends Controller
// Load the linked album
$album = AlbumController::loadAlbum($request->get('album_id'));
$storageLocation = sprintf('albums/%s', $album->url_alias);
/** @var \SplFileInfo $savedFile */
$savedFilePath = $photoFile->store($storageLocation, $album->getUploadDisk());
/** @var File $savedFile */
$savedFile = $album->getAlbumSource()->saveUploadedPhoto($album, $photoFile);
$photo = new Photo();
$photo->album_id = $album->id;
$photo->name = $photoFile->getClientOriginalName();
$photo->file_name = basename($savedFilePath);
$photo->mime_type = $photoFile->getClientMimeType();
$photo->file_size = $photoFile->getSize();
$photo->file_name = $savedFile->getFilename();
$photo->mime_type = $savedFile->getMimeType();
$photo->file_size = $savedFile->getSize();
$photo->save();
$upload = new Upload();