Fixes to file uploading; include disk name, remove clunky redirect code
This commit is contained in:
@@ -16,6 +16,7 @@ class CreateFilesTable extends Migration
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('description')->nullable();
|
||||
$table->string('disk')->nullable();
|
||||
$table->string('path');
|
||||
$table->boolean('public')->default(true);
|
||||
$table->string('ref_model', 50)->nullable();
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Requests\CreateFilesRequest;
|
||||
use App\Interfaces\Controller;
|
||||
use App\Models\File;
|
||||
use App\Services\FileService;
|
||||
use Flash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Input;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Log;
|
||||
use Validator;
|
||||
|
||||
/**
|
||||
* Class FilesController
|
||||
@@ -16,29 +18,42 @@ use Log;
|
||||
*/
|
||||
class FilesController extends Controller
|
||||
{
|
||||
private $fileSvc;
|
||||
|
||||
public function __construct(FileService $fileSvc)
|
||||
{
|
||||
$this->fileSvc = $fileSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly file
|
||||
* @param CreateFilesRequest $request
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function store(CreateFilesRequest $request)
|
||||
public function store(Request $request)
|
||||
{
|
||||
$attrs = $request->post();
|
||||
|
||||
// Not using a form validation here because when it redirects,
|
||||
// it leaves the parent forms all blank, even though it goes
|
||||
// back to the right place. So just manually validate
|
||||
$validator = Validator::make($request->all(), [
|
||||
'filename' => 'required',
|
||||
'file' => 'required|file'
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return redirect()->back()->withInput(Input::all())->withErrors($validator);
|
||||
}
|
||||
|
||||
Log::info('Uploading files', $attrs);
|
||||
|
||||
/**
|
||||
* @var $file \Illuminate\Http\UploadedFile
|
||||
*/
|
||||
$file = $request->file('file');
|
||||
$file_path = $file->storeAs(
|
||||
'files',
|
||||
$file->getClientOriginalName(),
|
||||
config('filesystems.public_files')
|
||||
);
|
||||
$file_path = $this->fileSvc->saveFile($file, 'files');
|
||||
|
||||
$asset = new File();
|
||||
$asset->name = $attrs['name'];
|
||||
$asset->name = $attrs['filename'];
|
||||
$asset->disk = config('filesystems.public_files');
|
||||
$asset->path = $file_path;
|
||||
$asset->public = true;
|
||||
$asset->ref_model = $attrs['ref_model'];
|
||||
@@ -46,43 +61,27 @@ class FilesController extends Controller
|
||||
|
||||
$asset->save();
|
||||
|
||||
/*foreach($files as $file) {
|
||||
$file_path = $file->store('files', $file->getClientOriginalName(), 'public');
|
||||
}*/
|
||||
|
||||
# Where do we go now? OooOOoOoOo sweet child
|
||||
$redirect = $attrs['redirect'];
|
||||
if(!$redirect) {
|
||||
$redirect = route('admin.dashboard.index');
|
||||
}
|
||||
|
||||
Flash::success('Files uploaded successfully.');
|
||||
return redirect($redirect);
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the file from storage.
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete($id, Request $request)
|
||||
public function destroy($id)
|
||||
{
|
||||
$redirect = $request->post('redirect');
|
||||
if (!$redirect) {
|
||||
$redirect = route('admin.dashboard.index');
|
||||
}
|
||||
|
||||
$file = File::find($id);
|
||||
if (!$file) {
|
||||
Flash::error('File doesn\'t exist');
|
||||
return redirect($redirect);
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
Storage::disk(config('filesystems.public_files'))->delete($file->path);
|
||||
$file->delete();
|
||||
|
||||
Flash::success('File deleted successfully.');
|
||||
return redirect($redirect);
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,6 @@ class UserController extends Controller
|
||||
$user = $this->userRepo->create($input);
|
||||
|
||||
Flash::success('User saved successfully.');
|
||||
|
||||
return redirect(route('admin.users.index'));
|
||||
}
|
||||
|
||||
@@ -210,6 +209,21 @@ class UserController extends Controller
|
||||
$req_data['password'] = Hash::make($req_data['password']);
|
||||
}
|
||||
|
||||
if ($request->filled('avatar_upload')) {
|
||||
/**
|
||||
* @var $file \Illuminate\Http\UploadedFile
|
||||
*/
|
||||
$file = $request->file('avatar_upload');
|
||||
$file_path = $file->storeAs(
|
||||
'avatars',
|
||||
str_slug($file->getClientOriginalName()),
|
||||
config('filesystems.public_files')
|
||||
);
|
||||
|
||||
$user->avatar = $file_path;
|
||||
}
|
||||
|
||||
|
||||
$original_user_state = $user->state;
|
||||
|
||||
$user = $this->userRepo->update($req_data, $id);
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* Class CreateFilesRequest
|
||||
* @package App\Http\Requests
|
||||
*/
|
||||
class CreateFilesRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'file' => 'required|file',
|
||||
|
||||
#'files.*' => 'required|file',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Storage;
|
||||
* File property
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $disk
|
||||
* @property string $path
|
||||
* @property boolean $public
|
||||
* @package App\Models
|
||||
@@ -24,6 +25,7 @@ class File extends Model
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'disk',
|
||||
'path',
|
||||
'public',
|
||||
'ref_model',
|
||||
@@ -40,14 +42,16 @@ class File extends Model
|
||||
*/
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
$disk = config('filesystems.public_files');
|
||||
$disk = $this->disk ?? config('filesystems.public_files');
|
||||
|
||||
// If the disk isn't stored in public (S3 or something),
|
||||
// just pass through the URL call
|
||||
if ($disk !== 'public') {
|
||||
return Storage::disk(config('filesystems.public_files'))
|
||||
->url($this->path);
|
||||
}
|
||||
|
||||
return public_asset(Storage::disk('public')
|
||||
->url($this->path)
|
||||
);
|
||||
// Otherwise, figure out the public URL and save there
|
||||
return public_asset(Storage::disk('public')->url($this->path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ Route::group([
|
||||
|
||||
# files
|
||||
Route::post('files', 'FilesController@store')->name('files.store');
|
||||
Route::delete('files/{id}/delete', 'FilesController@delete')->name('files.delete');
|
||||
Route::delete('files/{id}', 'FilesController@destroy')->name('files.delete');
|
||||
|
||||
# finances
|
||||
Route::resource('finances', 'FinanceController');
|
||||
|
||||
30
app/Services/FileService.php
Normal file
30
app/Services/FileService.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\Service;
|
||||
|
||||
/**
|
||||
* Class FileService
|
||||
* @package App\Services
|
||||
*/
|
||||
class FileService extends Service
|
||||
{
|
||||
/**
|
||||
* Save a file to disk and return the path
|
||||
* @param \Illuminate\Http\UploadedFile $file
|
||||
* @param string $folder
|
||||
* @param string|null $disk
|
||||
* @return string
|
||||
*/
|
||||
public function saveFile($file, $folder, $disk = null): string
|
||||
{
|
||||
if (!$disk) {
|
||||
$disk = config('filesystems.public_files');
|
||||
}
|
||||
|
||||
$path_info = pathinfo($file->getClientOriginalName());
|
||||
$filename = str_slug($path_info['filename']).'.'.$path_info['extension'];
|
||||
return $file->storeAs($folder, $filename, $disk);
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,7 @@
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.common.file_upload', [
|
||||
'model' => $aircraft,
|
||||
'redirect' => route('admin.aircraft.edit', ['id' => $aircraft->id])
|
||||
])
|
||||
@include('admin.common.file_upload', ['model' => $aircraft])
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@@ -21,10 +21,7 @@
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.common.file_upload', [
|
||||
'model' => $airport,
|
||||
'redirect' => route('admin.airports.edit', ['id' => $airport->id])
|
||||
])
|
||||
@include('admin.common.file_upload', ['model' => $airport])
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@@ -5,7 +5,7 @@ Pass in:
|
||||
$redirect - Where to go to
|
||||
|
||||
--}}
|
||||
<div id="airport-files-wrapper">
|
||||
<div id="airport-files-wrapper" class="col-12">
|
||||
<div class="header">
|
||||
<h3>files</h3>
|
||||
</div>
|
||||
@@ -29,7 +29,6 @@ Pass in:
|
||||
<td class="text-right">
|
||||
{{ Form::open(['route' => ['admin.files.delete', $file->id], 'method' => 'delete']) }}
|
||||
{{ Form::hidden('id', $file->id) }}
|
||||
{{ Form::hidden('redirect', $redirect) }}
|
||||
{{ Form::button('<i class="fa fa-times"></i>', [
|
||||
'type' => 'submit',
|
||||
'class' => 'btn btn-sm btn-danger btn-icon',
|
||||
@@ -43,7 +42,7 @@ Pass in:
|
||||
</table>
|
||||
<hr>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="col-sm-12">
|
||||
<div class="text-right">
|
||||
{{ Form::open([
|
||||
'url' => route('admin.files.store'),
|
||||
@@ -56,16 +55,17 @@ Pass in:
|
||||
{{-- Fields for the model --}}
|
||||
{{ Form::hidden('ref_model', get_class($model)) }}
|
||||
{{ Form::hidden('ref_model_id', $model->id) }}
|
||||
{{ Form::hidden('redirect', $redirect) }}
|
||||
|
||||
{{ Form::label('name', 'Name:') }} <span class="required">*</span>
|
||||
{{ Form::text('name', null, ['class' => 'form-control']) }}
|
||||
{{ Form::label('filename', 'Name:') }} <span class="required">*</span>
|
||||
{{ Form::text('filename', null, ['class' => 'form-control']) }}
|
||||
|
||||
{{ Form::file('file', ['class' => 'form-control']) }}
|
||||
|
||||
{{ Form::submit('Upload', ['class' => 'btn btn-success']) }}
|
||||
<p class="text-danger">{{ $errors->first('name') }}</p>
|
||||
<p class="text-danger">{{ $errors->first('file') }}</p>
|
||||
<div class="text-danger" style="padding-top: 10px;">
|
||||
<span>{{ $errors->first('filename') }}</span>
|
||||
<span>{{ $errors->first('file') }}</span>
|
||||
</div>
|
||||
|
||||
{{ Form::close() }}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user