From 651174bda8dd888eb878bc82f73ff2785834ba5c Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Mon, 26 Aug 2019 14:43:50 -0400 Subject: [PATCH] 229 url for downloads (#369) * Allow file uploads for subfleet * Allow URL to be used for a download * Remove old FileUploadRequest * Move file removal logic to service layer * Remove unused import * Remove unused packages --- .../Controllers/Admin/DashboardController.php | 2 - app/Http/Controllers/Admin/FileController.php | 84 ++++++++++++++----- app/Models/File.php | 5 ++ app/Models/Subfleet.php | 2 + app/Services/FileService.php | 31 +++++-- .../views/admin/common/file_upload.blade.php | 21 +++-- .../views/admin/subfleets/edit.blade.php | 6 ++ 7 files changed, 112 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 493c7c55..9d5a0fb7 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -11,8 +11,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Laracasts\Flash\Flash; -use PragmaRX\Version\Package\Facade as Version; -use vierbergenlars\SemVer\version as semver; class DashboardController extends Controller { diff --git a/app/Http/Controllers/Admin/FileController.php b/app/Http/Controllers/Admin/FileController.php index cf59f09d..b878e3fd 100644 --- a/app/Http/Controllers/Admin/FileController.php +++ b/app/Http/Controllers/Admin/FileController.php @@ -5,12 +5,12 @@ namespace App\Http\Controllers\Admin; use App\Contracts\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; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\Rule; +use Laracasts\Flash\Flash; /** * Class FileController @@ -37,30 +37,68 @@ class FileController extends Controller { $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_description' => 'nullable', - 'file' => 'required|file', - ]); + /* + * 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. + * + * The fields are also named file_name and file_description so that if there + * are validation errors, the flash messages doesn't conflict with other + * fields on the page that might have the "name" and "description" fields + * + * Was also going to use the "required_without" rule, but that doesn't appear + * to work properly with a file upload + */ + $validator = Validator::make( + $request->all(), + [ + 'file_name' => 'required', + 'file_description' => 'nullable', + 'file' => [ + Rule::requiredIf(function () { + return !request()->filled('url'); + }), + 'file', + ], + 'url' => [ + Rule::requiredIf(function () { + return !request()->hasFile('file'); + }), + 'url', + ], + ], + [ + 'file.required' => 'File or URL are required', + 'url.required' => 'File or URL are required', + ] + ); if ($validator->fails()) { - return redirect()->back()->withInput(Input::all())->withErrors($validator); + return redirect() + ->back() + ->withErrors($validator) + ->withInput(Input::all()); } Log::info('Uploading files', $attrs); - $file = $request->file('file'); - $this->fileSvc->saveFile($file, 'files', [ - 'name' => $attrs['filename'], - 'description' => $attrs['file_description'], - 'ref_model' => $attrs['ref_model'], - 'ref_model_id' => $attrs['ref_model_id'], - ]); + $attrs['name'] = $attrs['file_name']; + $attrs['description'] = $attrs['file_description']; + + if ($request->hasFile('file')) { + $file = $request->file('file'); + $this->fileSvc->saveFile($file, 'files', $attrs); + } + + // Didn't provide a file to upload, just a URL to a file + // Create the model directly and just associate that + elseif ($request->filled('url')) { + $file = new File($attrs); + $file->path = $attrs['url']; + $file->save(); + } + + Flash::success('Files saved successfully'); - Flash::success('Files uploaded successfully.'); return redirect()->back(); } @@ -81,10 +119,10 @@ class FileController extends Controller return redirect()->back(); } - Storage::disk(config('filesystems.public_files'))->delete($file->path); - $file->delete(); + $this->fileSvc->removeFile($file); Flash::success('File deleted successfully.'); + return redirect()->back(); } } diff --git a/app/Models/File.php b/app/Models/File.php index 013018a4..154e5b3e 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -6,6 +6,7 @@ use App\Contracts\Model; use App\Models\Traits\HashIdTrait; use App\Models\Traits\ReferenceTrait; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; /** * @property string $name @@ -80,6 +81,10 @@ class File extends Model */ public function getUrlAttribute(): string { + if (Str::startsWith($this->path, 'http')) { + return $this->path; + } + $disk = $this->disk ?? config('filesystems.public_files'); // If the disk isn't stored in public (S3 or something), diff --git a/app/Models/Subfleet.php b/app/Models/Subfleet.php index 91999614..4d4d234a 100644 --- a/app/Models/Subfleet.php +++ b/app/Models/Subfleet.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Contracts\Model; use App\Models\Enums\AircraftStatus; use App\Models\Traits\ExpensableTrait; +use App\Models\Traits\FilesTrait; /** * Class Subfleet @@ -21,6 +22,7 @@ use App\Models\Traits\ExpensableTrait; class Subfleet extends Model { use ExpensableTrait; + use FilesTrait; public $table = 'subfleets'; diff --git a/app/Services/FileService.php b/app/Services/FileService.php index b9709a3c..b72fc01a 100644 --- a/app/Services/FileService.php +++ b/app/Services/FileService.php @@ -4,10 +4,10 @@ namespace App\Services; use App\Contracts\Service; use App\Models\File; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; -/** - * Class FileService - */ class FileService extends Service { /** @@ -37,13 +37,11 @@ class FileService extends Service // Create the file, add the ID to the front of the file to account // for any duplicate filenames, but still can be found in an `ls` - - $filename = $id.'_' - .str_slug(trim($path_info['filename'])) - .'.'.$path_info['extension']; - + $filename = $id.'_'.str_slug(trim($path_info['filename'])).'.'.$path_info['extension']; $file_path = $file->storeAs($folder, $filename, $attrs['disk']); + Log::info('File saved to '.$file_path); + $asset = new File($attrs); $asset->id = $id; $asset->path = $file_path; @@ -51,4 +49,21 @@ class FileService extends Service return $asset; } + + /** + * Remove a file, if it exists on disk + * + * @param File $file + * + * @throws \Exception + */ + public function removeFile($file) + { + if (!Str::startsWith($file->path, 'http')) { + Storage::disk(config('filesystems.public_files')) + ->delete($file->path); + } + + $file->delete(); + } } diff --git a/resources/views/admin/common/file_upload.blade.php b/resources/views/admin/common/file_upload.blade.php index 4f3cdc4c..86d95ccd 100644 --- a/resources/views/admin/common/file_upload.blade.php +++ b/resources/views/admin/common/file_upload.blade.php @@ -8,6 +8,9 @@ Pass in:

files

+ @component('admin.components.info') + Add a download link or upload a file to make available + @endcomponent
@if(count($model->files) === 0) @@ -58,18 +61,24 @@ Pass in: ]) }} + * + {{ Form::text('file_name', null, ['class' => 'form-control', 'placeholder' => 'Name']) }} + {{ Form::text('file_description', null, ['class' => 'form-control', 'placeholder' => 'Description']) }} + {{ Form::text('url', null, ['class' => 'form-control', 'placeholder' => 'URL']) }} + {{ Form::file('file', ['class' => 'form-control']) }} + {{-- Fields for the model --}} {{ Form::hidden('ref_model', get_class($model)) }} {{ Form::hidden('ref_model_id', $model->id) }} - * - {{ Form::text('filename', null, ['class' => 'form-control', 'placeholder' => 'Name']) }} - {{ Form::text('file_description', null, ['class' => 'form-control', 'placeholder' => 'Description']) }} - {{ Form::file('file', ['class' => 'form-control']) }} - - {{ Form::submit('Upload', ['class' => 'btn btn-success']) }} + {{ Form::submit('Save', [ + 'id' => 'save_file_upload', + 'class' => 'btn btn-success' + ]) + }}
{{ $errors->first('filename') }} + {{ $errors->first('url') }} {{ $errors->first('file') }}
diff --git a/resources/views/admin/subfleets/edit.blade.php b/resources/views/admin/subfleets/edit.blade.php index c1f38da8..484a2e64 100644 --- a/resources/views/admin/subfleets/edit.blade.php +++ b/resources/views/admin/subfleets/edit.blade.php @@ -27,5 +27,11 @@ @include('admin.subfleets.expenses')
+ +
+
+ @include('admin.common.file_upload', ['model' => $subfleet]) +
+
@endsection @include('admin.subfleets.script')