Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/Wardrobe/Core/Controllers/Api/DropzoneController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Wardrobe\Core\Controllers\Api;

use Wardrobe\Core\Controllers\BaseController;
use Wardrobe\Core\Repositories\FileStorageRepositoryInterface;

use Input, Config, Response, Exception, File;
use Carbon\Carbon;
Expand All @@ -9,16 +10,25 @@

class DropzoneController extends BaseController {

/**
* The file storage driver
*
* @var Wardrobe\Core\Repositories\FileStorageRepositoryInterface
*/
protected $fileStorage;

/**
* Create a new API Dropzone controller.
*
* @return \ApiDropzoneController
*/
public function __construct()
public function __construct(FileStorageRepositoryInterface $fileStorage)
{
parent::__construct();

$this->beforeFilter('wardrobe.auth');

$this->fileStorage = $fileStorage;
}

/**
Expand Down Expand Up @@ -67,29 +77,23 @@ public function postIndex()
public function postImage()
{
$file = Input::file('file');
$imageDir = Config::get('core::wardrobe.image_dir');
$destinationPath = public_path(). "/{$imageDir}/";
$filename = $file->getClientOriginalName();
$resizeEnabled = Config::get('core::wardrobe.image_resize.enabled');

//move the file to tmp location
$file->move(sys_get_temp_dir(), 'Wardrobe_'.$filename);
$tmpPath = sys_get_temp_dir().'/Wardrobe_'.$filename;

if ($resizeEnabled)
{
$resizeWidth = Config::get('core::wardrobe.image_resize.width');
$resizeHeight = Config::get('core::wardrobe.image_resize.height');
$image = Image::make($file->getRealPath())->resize($resizeWidth, $resizeHeight, true);
$image->save($destinationPath.$filename);
}
else
{
$file->move($destinationPath, $filename);
$image = Image::make($tmpPath)->resize($resizeWidth, $resizeHeight, true);
$image->save($tmpPath);
}

if (File::exists($destinationPath.$filename))
{
// @note - Using the absolute url so it loads images when ran in sub folder
// this will make exporting less portable and may need to re-address at a later point.
return Response::json(array('filename' => url("/{$imageDir}/".$filename)));
}
return Response::json(array('error' => 'Upload failed. Please ensure your public/img directory is writable.'));
$result = $this->fileStorage->store(file_get_contents($tmpPath), $filename, "image");

return Response::json($result);
}
}
87 changes: 87 additions & 0 deletions src/Wardrobe/Core/Repositories/FileStorageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php namespace Wardrobe\Core\Repositories;

use Auth, Hash, Validator, Wardrobe, Config;
use Illuminate\Support\Contracts\MessageProviderInterface;

class FileStorageRepository implements FileStorageRepositoryInterface {

/**
* The directory in which to store the files
*
* @var string
*/
protected $storageDirectory;

/**
* The full destination path of the file
*
* @var string
*/
protected $destinationPath;

/**
* The directory in which to store the files
*
* @var string
*/
protected $messsages;

/**
* The array of storage directories keyed by file type in the Wardrobe config
*
* @var array
*/
protected $storageDirectories;

/**
* Constructor
*
* @param Illuminate\Support\Contracts\MessageProviderInterface $messages
*
* @return \Wardrobe\Core\Repositories\FileStorageRepositoryInterface
*/
public function __construct(MessageProviderInterface $messages)
{
$this->messages = $messages;
$this->storageDirectories = Config::get('core::wardrobe.storage_directories');
}

/**
* Saves file in chosen storage
*
* @param string $content
* @param string $last_name
* @param string $type
*
* @return \Illuminate\Support\MessageBag
*/
public function store($content, $filename, $type)
{
//@note - this is meant to be overridden.
return $this->messages->getMessageBag();
}

/**
* Sets destination path based on file type
*
* @param string $type
*
* @return void
*/
protected function setDestinationPath($type)
{
switch($type)
{
case "image":
$this->storageDirectory = $this->storageDirectories['image'];
if(!$this->storageDirectory || $this->storageDirectory == "") $this->storageDirectory = "images";
break;
default:
$this->storageDirectory = $this->storageDirectories['default'];
if(!$this->storageDirectory || $this->storageDirectory == "") $this->storageDirectory = "files";
break;
}

$this->destinationPath = public_path(). "/{$this->storageDirectory}/";
}
}
15 changes: 15 additions & 0 deletions src/Wardrobe/Core/Repositories/FileStorageRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Wardrobe\Core\Repositories;

interface FileStorageRepositoryInterface {

/**
* Saves file in chosen storge
*
* @param string $content
* @param string $last_name
* @param string $type
*
* @return \Illuminate\Support\MessageBag
*/
public function store($content, $filename, $type);
}
29 changes: 29 additions & 0 deletions src/Wardrobe/Core/Repositories/LocalFileStorageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Wardrobe\Core\Repositories;

class LocalFileStorageRepository extends FileStorageRepository {

/**
* Saves file in chosen storge
*
* @param string $content
* @param string $last_name
* @param string $type
*
* @return \Illuminate\Support\MessageBag
*/
public function store($content, $filename, $type)
{
$this->setDestinationPath($type);

if(file_put_contents($this->destinationPath . $filename, $content) !== false)
{
$this->messages->add('filename', url($this->storageDirectory."/".$filename));
}
else
{
$this->messages->add('error', 'Upload failed. Please ensure the ' . $this->destinationPath . ' directory is writable.');
}

return $this->messages->getMessageBag();
}
}
75 changes: 75 additions & 0 deletions src/Wardrobe/Core/Repositories/S3FileStorageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php namespace Wardrobe\Core\Repositories;

use Config, File;
use Illuminate\Support\Contracts\MessageProviderInterface;
use Aws\S3\S3Client;

class S3FileStorageRepository extends FileStorageRepository {

/**
* AWS S3 Client
*
* @var AWS\S3\S3Client
*/
protected $s3Client;

/**
* Constructor
*
* @param Illuminate\Support\Contracts\MessageProviderInterface $messages
*
* @return \Wardrobe\Core\Repositories\FileStorageRepositoryInterface
*/
public function __construct(MessageProviderInterface $messages)
{
parent::__construct($messages);

$this->s3Client = S3Client::factory(array(
'key' => Config::get('core::wardrobe.s3_creds.api_key'),
'secret' => Config::get('core::wardrobe.s3_creds.api_secret')
));
}

/**
* Saves file in chosen storage
*
* @param string $content
* @param string $last_name
* @param string $type
*
* @return \Illuminate\Support\MessageBag
*/
public function store($content, $filename, $type)
{
$this->setDestinationPath($type);

$file = File::put($this->destinationPath.$filename, $content);

if(File::exists($this->destinationPath.$filename))
{
try
{
$result = $this->s3Client->putObject(array(
'Bucket' => Config::get('core::wardrobe.s3_creds.bucket'),
'Key' => $this->storageDirectory."/".$filename,
'Body' => fopen($this->destinationPath.$filename, 'r+'),
'ACL' => 'public-read'
));

$this->messages->add('filename', $result['ObjectURL']);
}
catch(Aws\S3\Exception\S3Exception $e)
{
$this->messages->add('error', 'Upload failed. Please verify your S3 settings in the config.');
}

File::delete($this->destinationPath.$filename);
}
else
{
$this->messages->add('error', 'Upload failed. Please ensure the ' . $this->destinationPath . ' directory is writable.');
}

return $this->messages->getMessageBag();
}
}
13 changes: 13 additions & 0 deletions src/Wardrobe/Core/WardrobeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ protected function bindRepositories()

$this->app->singleton('Wardrobe\Core\Repositories\UserRepositoryInterface', 'Wardrobe\Core\Repositories\DbUserRepository');

switch(strtolower(Config::get('core::wardrobe.file_storage')))
{
case "s3":
$this->app->bind('Wardrobe\Core\Repositories\FileStorageRepositoryInterface', function () {
return new \Wardrobe\Core\Repositories\S3FileStorageRepository(new \Illuminate\Support\MessageBag);
});
break;
default:
$this->app->bind('Wardrobe\Core\Repositories\FileStorageRepositoryInterface', function () {
return new \Wardrobe\Core\Repositories\LocalFileStorageRepository(new \Illuminate\Support\MessageBag);
});
}

$this->app->bind('Wardrobe', function()
{
return new \Wardrobe\Core\Facades\Wardrobe(new Repositories\DbPostRepository);
Expand Down
54 changes: 41 additions & 13 deletions src/config/wardrobe.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,28 @@

/*
|--------------------------------------------------------------------------
| Image Uploads Directory
| File Storage
|--------------------------------------------------------------------------
|
| Set this to the directory where your images will be located in your public
| Set this to indicate how to store uploaded files. Values are 'filesystem'
| or 's3'. If s3 is indicated, set your credentials below.
|
*/
'file_storage' => 'filesystem',

/*
|--------------------------------------------------------------------------
| File Uploads Directory
|--------------------------------------------------------------------------
|
| Set this to the directory where your files will be located in your public
| folder.
|
*/
'image_dir' => 'img',
'storage_directories' => array(
'image' => 'img',
'default' => 'files',
),

/*
|--------------------------------------------------------------------------
Expand All @@ -39,6 +53,20 @@
'height' => '600',
),

/*
|--------------------------------------------------------------------------
| S3 Creds
|--------------------------------------------------------------------------
|
| If using S3 for file storage, specify your S3 credentials here.
|
*/
's3_creds' => array(
'bucket' => 'bucket',
'api_key' => 'key',
'api_secret' => 'secret',
),

/*
|--------------------------------------------------------------------------
| 404 Handling
Expand Down Expand Up @@ -90,16 +118,16 @@
*/
'installed' => true,

/*
|--------------------------------------------------------------------------
| Enable Cache
|--------------------------------------------------------------------------
|
| Set this to true to enable caching. If true it will then use the
| default laravel cache setup.
|
*/
'cache' => null,
/*
|--------------------------------------------------------------------------
| Enable Cache
|--------------------------------------------------------------------------
|
| Set this to true to enable caching. If true it will then use the
| default laravel cache setup.
|
*/
'cache' => null,

/*
|--------------------------------------------------------------------------
Expand Down