Skip to content

Commit

Permalink
Fixed file uploading
Browse files Browse the repository at this point in the history
Separate images storage path and files storage path.

issue #78
  • Loading branch information
butschster committed Apr 15, 2016
1 parent cd8f6dd commit a1b5728
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 39 deletions.
8 changes: 8 additions & 0 deletions config/sleeping_owl.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@

'imagesUploadDirectory' => 'images/uploads',

/*
|--------------------------------------------------------------------------
| Directory to upload files to (relative to public directory)
|--------------------------------------------------------------------------
*/

'filesUploadDirectory' => 'files/uploads',

/*
|--------------------------------------------------------------------------
| Template to use
Expand Down
2 changes: 1 addition & 1 deletion resources/views/default/form/element/file.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<span class="text-danger">*</span>
@endif
</label>
<div class="imageUpload" data-target="{{ route('admin.form.element.image.uploadFile') }}" data-token="{{ csrf_token() }}">
<div class="imageUpload" data-target="{{ route('admin.form.element.file.uploadFile') }}" data-token="{{ csrf_token() }}">
<div>
<div class="thumbnail">
<div class="no-value {{ empty($value) ? '' : 'hidden' }}">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/default/form/element/image.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<span class="text-danger">*</span>
@endif
</label>
<div class="imageUpload" data-target="{{ route('admin.form.element.image.uploadImage') }}" data-token="{{ csrf_token() }}">
<div class="imageUpload" data-target="{{ route('admin.form.element.file.uploadImage') }}" data-token="{{ csrf_token() }}">
<div>
<div class="thumbnail">
<img class="no-value {{ empty($value) ? '' : 'hidden' }}" src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" width="200px" height="150px" />
Expand Down
2 changes: 1 addition & 1 deletion resources/views/default/form/element/images.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<span class="text-danger">*</span>
@endif
</label>
<div class="imageUploadMultiple" data-target="{{ route('admin.form.element.image.uploadImage') }}" data-token="{{ csrf_token() }}">
<div class="imageUploadMultiple" data-target="{{ route('admin.form.element.file.uploadImage') }}" data-token="{{ csrf_token() }}">
<div class="row form-group images-group">
@foreach ($value as $image)
<div class="col-xs-6 col-md-3 imageThumbnail">
Expand Down
52 changes: 51 additions & 1 deletion src/Form/Element/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,63 @@

namespace SleepingOwl\Admin\Form\Element;

class File extends Image
use Request;
use Route;
use Response;
use Validator;
use SleepingOwl\Admin\Contracts\WithRoutesInterface;

class File extends NamedFormElement implements WithRoutesInterface
{

/**
* @var string
*/
protected static $route = 'uploadFile';

public static function registerRoutes()
{
Route::post('FormElements/image/'.static::$route, ['as' => 'admin.form.element.file.'.static::$route, function () {
$validator = Validator::make(Request::all(), static::uploadValidationRules());

static::uploadValidationRules($validator);

if ($validator->fails()) {
return Response::make($validator->errors()->get('file'), 400);
}

$file = Request::file('file');
$filename = md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension();
$path = static::getUploadPath();
$fullPath = public_path($path);
$file->move($fullPath, $filename);

$value = $path.'/'.$filename;

return [
'url' => asset($value),
'value' => $value,
];
},
]);
}

/**
* @param \Illuminate\Validation\Validator $validator
*/
protected static function validate(\Illuminate\Validation\Validator $validator)
{

}

/**
* @return string
*/
protected static function getUploadPath()
{
return config('sleeping_owl.filesUploadDirectory', 'files/uploads');
}

/**
* @return array
*/
Expand Down
55 changes: 20 additions & 35 deletions src/Form/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,37 @@

namespace SleepingOwl\Admin\Form\Element;

use Request;
use Route;
use Response;
use Validator;
use SleepingOwl\Admin\Contracts\WithRoutesInterface;

class Image extends NamedFormElement implements WithRoutesInterface
class Image extends File
{

/**
* @var string
*/
protected static $route = 'uploadImage';

public static function registerRoutes()
/**
* @param \Illuminate\Validation\Validator $validator
*/
protected static function validate(\Illuminate\Validation\Validator $validator)
{
Route::post('FormElements/image/'.static::$route, ['as' => 'admin.form.element.image.'.static::$route, function () {
$validator = Validator::make(Request::all(), static::uploadValidationRules());

$validator->after(function ($validator) {

/** @var \Illuminate\Http\UploadedFile $file */
$file = array_get($validator->attributes(), 'file');
$validator->after(function ($validator) {
/** @var \Illuminate\Http\UploadedFile $file */
$file = array_get($validator->attributes(), 'file');

$size = getimagesize($file->getRealPath());
$size = getimagesize($file->getRealPath());

if (! $size) {
$validator->errors()->add('file', trans('sleeping_owl::validation.not_image'));
}
});

if ($validator->fails()) {
return Response::make($validator->errors()->get('file'), 400);
if (! $size) {
$validator->errors()->add('file', trans('sleeping_owl::validation.not_image'));
}
});
}

$file = Request::file('file');
$filename = md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension();
$path = config('sleeping_owl.imagesUploadDirectory');
$fullpath = public_path($path);
$file->move($fullpath, $filename);
$value = $path.'/'.$filename;

return [
'url' => asset($value),
'value' => $value,
];
}]);
/**
* @return string
*/
protected static function getUploadPath()
{
return config('sleeping_owl.imagesUploadDirectory', 'images/uploads');
}

/**
Expand Down

0 comments on commit a1b5728

Please sign in to comment.