From 276f534ded31d497953aae4002b5b3c3cd870993 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:09:21 -0700 Subject: [PATCH 1/9] Updated permissions Signed-off-by: snipe --- config/permissions.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/permissions.php b/config/permissions.php index 64310ac05..00c11356d 100644 --- a/config/permissions.php +++ b/config/permissions.php @@ -270,6 +270,12 @@ return [ 'note' => '', 'display' => true, ], + [ + 'permission' => 'components.files', + 'label' => 'View and Modify Component Files', + 'note' => '', + 'display' => true, + ], ], From 3d3a4b02fc4e51a1a9d257cbf9370f65e8d7443e Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:09:34 -0700 Subject: [PATCH 2/9] Added helper method for uploads Signed-off-by: snipe --- app/Models/Component.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/Models/Component.php b/app/Models/Component.php index 79d2fe55f..dc353d288 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -88,6 +88,24 @@ class Component extends SnipeModel 'location' => ['name'], ]; + + /** + * Establishes the components -> action logs -> uploads relationship + * + * @author A. Gianotto + * @since [v6.1.13] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ + public function uploads() + { + return $this->hasMany(\App\Models\Actionlog::class, 'item_id') + ->where('item_type', '=', self::class) + ->where('action_type', '=', 'uploaded') + ->whereNotNull('filename') + ->orderBy('created_at', 'desc'); + } + + /** * Establishes the component -> location relationship * From f51b312843398d2883d9d551c18288ef9f3f5539 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:09:51 -0700 Subject: [PATCH 3/9] Added component routes for uploads Signed-off-by: snipe --- routes/web/components.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/routes/web/components.php b/routes/web/components.php index 81302df15..778da9e81 100644 --- a/routes/web/components.php +++ b/routes/web/components.php @@ -25,6 +25,21 @@ Route::group(['prefix' => 'components', 'middleware' => ['auth']], function () { [Components\ComponentCheckinController::class, 'store'] )->name('components.checkin.store'); + Route::post( + '{componentId}/upload', + [Components\ComponentFilesController::class, 'store'] + )->name('upload/component'); + + Route::delete( + '{componentId}/deletefile/{fileId}', + [Components\ComponentFilesController::class, 'destroy'] + )->name('delete/componentfile'); + + Route::get( + '{componentId}/showfile/{fileId}/{download?}', + [Components\ComponentFilesController::class, 'show'] + )->name('show.componentfile'); + }); Route::resource('components', Components\ComponentsController::class, [ From 84c0f5026608ca535ef1208c676f729ce2235ede Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:10:04 -0700 Subject: [PATCH 4/9] Added ComponentFilesController Signed-off-by: snipe --- .../Components/ComponentsFilesController.php | 177 ++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 app/Http/Controllers/Components/ComponentsFilesController.php diff --git a/app/Http/Controllers/Components/ComponentsFilesController.php b/app/Http/Controllers/Components/ComponentsFilesController.php new file mode 100644 index 000000000..a02b9eecd --- /dev/null +++ b/app/Http/Controllers/Components/ComponentsFilesController.php @@ -0,0 +1,177 @@ +] + * @since [v1.0] + * @param AssetFileRequest $request + * @param int $componentId + * @return \Illuminate\Http\RedirectResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function store(AssetFileRequest $request, $componentId = null) + { + $component = Component::find($componentId); + + if (isset($component->id)) { + $this->authorize('update', $component); + + if ($request->hasFile('file')) { + if (! Storage::exists('private_uploads/components')) { + Storage::makeDirectory('private_uploads/components', 775); + } + + foreach ($request->file('file') as $file) { + + $extension = $file->getClientOriginalExtension(); + $file_name = 'component-'.$component->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension; + + + // Check for SVG and sanitize it + if ($extension == 'svg') { + \Log::debug('This is an SVG'); + \Log::debug($file_name); + + $sanitizer = new Sanitizer(); + $dirtySVG = file_get_contents($file->getRealPath()); + $cleanSVG = $sanitizer->sanitize($dirtySVG); + + try { + Storage::put('private_uploads/components/'.$file_name, $cleanSVG); + } catch (\Exception $e) { + \Log::debug('Upload no workie :( '); + \Log::debug($e); + } + + } else { + Storage::put('private_uploads/components/'.$file_name, file_get_contents($file)); + } + + //Log the upload to the log + $component->logUpload($file_name, e($request->input('notes'))); + } + + + return redirect()->route('components.show', $component->id)->with('success', trans('admin/components/message.upload.success')); + + } + + return redirect()->route('components.show', $component->id)->with('error', trans('admin/components/message.upload.nofiles')); + } + // Prepare the error message + return redirect()->route('components.index') + ->with('error', trans('admin/components/message.does_not_exist')); + } + + /** + * Deletes the selected component file. + * + * @author [A. Gianotto] [] + * @since [v1.0] + * @param int $componentId + * @param int $fileId + * @return \Illuminate\Http\RedirectResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function destroy($componentId = null, $fileId = null) + { + $component = Component::find($componentId); + + // the asset is valid + if (isset($component->id)) { + $this->authorize('update', $component); + $log = Actionlog::find($fileId); + + // Remove the file if one exists + if (Storage::exists('components/'.$log->filename)) { + try { + Storage::delete('components/'.$log->filename); + } catch (\Exception $e) { + \Log::debug($e); + } + } + + $log->delete(); + + return redirect()->back() + ->with('success', trans('admin/hardware/message.deletefile.success')); + } + + // Redirect to the licence management page + return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist')); + } + + /** + * Allows the selected file to be viewed. + * + * @author [A. Gianotto] [] + * @since [v1.4] + * @param int $componentId + * @param int $fileId + * @return \Symfony\Component\HttpFoundation\Response + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function show($componentId = null, $fileId = null, $download = true) + { + \Log::debug('Private filesystem is: '.config('filesystems.default')); + $component = Component::find($componentId); + + // the component is valid + if (isset($component->id)) { + $this->authorize('view', $component); + $this->authorize('components.files', $component); + + if (! $log = Actionlog::find($fileId)) { + return response('No matching record for that asset/file', 500) + ->header('Content-Type', 'text/plain'); + } + + $file = 'private_uploads/components/'.$log->filename; + + if (Storage::missing($file)) { + \Log::debug('FILE DOES NOT EXISTS for '.$file); + \Log::debug('URL should be '.Storage::url($file)); + + return response('File '.$file.' ('.Storage::url($file).') not found on server', 404) + ->header('Content-Type', 'text/plain'); + } else { + + // We have to override the URL stuff here, since local defaults in Laravel's Flysystem + // won't work, as they're not accessible via the web + if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer? + return StorageHelper::downloader($file); + } else { + if ($download != 'true') { + \Log::debug('display the file'); + if ($contents = file_get_contents(Storage::url($file))) { // TODO - this will fail on private S3 files or large public ones + return Response::make(Storage::url($file)->header('Content-Type', mime_content_type($file))); + } + + return JsonResponse::create(['error' => 'Failed validation: '], 500); + } + + return StorageHelper::downloader($file); + + } + } + } + + return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist', ['id' => $fileId])); + } +} From f1cb7ee410657f5792f2d6cb1c8ff5b64297d63b Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:28:39 -0700 Subject: [PATCH 5/9] Fixed some translations Signed-off-by: snipe --- .../Components/ComponentsFilesController.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Components/ComponentsFilesController.php b/app/Http/Controllers/Components/ComponentsFilesController.php index a02b9eecd..d9f59f1d9 100644 --- a/app/Http/Controllers/Components/ComponentsFilesController.php +++ b/app/Http/Controllers/Components/ComponentsFilesController.php @@ -68,15 +68,15 @@ class ComponentsFilesController extends Controller } - return redirect()->route('components.show', $component->id)->with('success', trans('admin/components/message.upload.success')); + return redirect()->route('components.show', $component->id)->with('success', trans('general.file_upload_success')); } - return redirect()->route('components.show', $component->id)->with('error', trans('admin/components/message.upload.nofiles')); + return redirect()->route('components.show', $component->id)->with('error', trans('general.no_files_uploaded')); } // Prepare the error message return redirect()->route('components.index') - ->with('error', trans('admin/components/message.does_not_exist')); + ->with('error', trans('general.file_does_not_exist')); } /** @@ -114,7 +114,7 @@ class ComponentsFilesController extends Controller } // Redirect to the licence management page - return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist')); + return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist')); } /** @@ -152,8 +152,6 @@ class ComponentsFilesController extends Controller ->header('Content-Type', 'text/plain'); } else { - // We have to override the URL stuff here, since local defaults in Laravel's Flysystem - // won't work, as they're not accessible via the web if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer? return StorageHelper::downloader($file); } else { @@ -172,6 +170,6 @@ class ComponentsFilesController extends Controller } } - return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist', ['id' => $fileId])); + return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId])); } } From def89bfa0ca385bf32c659b96d6cd238800e5ce4 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:28:49 -0700 Subject: [PATCH 6/9] Added some generic file translations Signed-off-by: snipe --- resources/lang/en/general.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index be18b9c0e..bcc08dafd 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -280,6 +280,9 @@ return [ 'yes' => 'Yes', 'zip' => 'Zip', 'noimage' => 'No image uploaded or image not found.', + 'file_does_not_exist' => 'The requested file does not exist on the server.', + 'file_upload_success' => 'File upload success!', + 'no_files_uploaded' => 'File upload success!', 'token_expired' => 'Your form session has expired. Please try again.', 'login_enabled' => 'Login Enabled', 'audit_due' => 'Due for Audit', From 71a7176a6e89bb46354828f6ee55934a4da45c29 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:28:57 -0700 Subject: [PATCH 7/9] Updated component blade Signed-off-by: snipe --- resources/views/components/view.blade.php | 212 +++++++++++++++++----- 1 file changed, 171 insertions(+), 41 deletions(-) diff --git a/resources/views/components/view.blade.php b/resources/views/components/view.blade.php index 3b08cf236..d7b494566 100644 --- a/resources/views/components/view.blade.php +++ b/resources/views/components/view.blade.php @@ -48,61 +48,188 @@ @endcan @stop - {{-- Page content --}} @section('content') - +{{-- Page content --}}
-
-
-
-
-
- + + + + + @can('components.files', $component) +
  • + + + + +
  • + @endcan + + @can('update', Component::class) + +
  • + + {{ trans('button.upload') }} + +
  • + @endcan + + +
    + +
    +
    + +
    + + + + + + + + + +
    + {{ trans('general.asset') }} + + {{ trans('general.qty') }} + + {{ trans('general.notes') }} + + {{ trans('general.date') }} + + {{ trans('general.checkin') }}/{{ trans('general.checkout') }} +
    + +
    +
    + + + @can('components.files', $component) +
    + +
    + - - - - - + + + + + + + + -
    - {{ trans('general.asset') }} - - {{ trans('general.qty') }} - - {{ trans('general.notes') }} - - {{ trans('general.date') }} - - {{ trans('general.checkin') }}/{{ trans('general.checkout') }} - {{trans('general.file_type')}}{{ trans('general.image') }}{{ trans('general.file_name') }}{{ trans('general.filesize') }}{{ trans('general.notes') }}{{ trans('general.download') }}{{ trans('general.created_at') }}{{ trans('table.actions') }}
    + + @if ($component->uploads->count() > 0) + @foreach ($component->uploads as $file) + + + + {{ Helper::filetype_icon($file->filename) }} + + + @if ($file->filename) + @if ( Helper::checkUploadIsImage($file->get_src('components'))) + + @endif + @endif + + + {{ $file->filename }} + + + {{ @Helper::formatFilesizeUnits(Storage::exists('private_uploads/components/'.$file->filename) ? Storage::size('private_uploads/components/'.$file->filename) : '') }} + + + + @if ($file->note) + {{ $file->note }} + @endif + + + @if ($file->filename) + + + {{ trans('general.download') }} + + @endif + + {{ $file->created_at }} + + + + {{ trans('general.delete') }} + + + + @endforeach + @else + + {{ trans('general.no_results') }} + + @endif + +
    -
    -
    +
    + @endcan +
    @@ -156,6 +283,9 @@ +@can('update', Component::class) + @include ('modals.upload-file', ['item_type' => 'component', 'item_id' => $component->id]) +@endcan @stop @section('moar_scripts') From 3d8e0b707e538fd774e78ec75a63a3667dcee4ee Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:29:13 -0700 Subject: [PATCH 8/9] Fixed component file route controller names Signed-off-by: snipe --- routes/web/components.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routes/web/components.php b/routes/web/components.php index 778da9e81..429573b8f 100644 --- a/routes/web/components.php +++ b/routes/web/components.php @@ -27,17 +27,17 @@ Route::group(['prefix' => 'components', 'middleware' => ['auth']], function () { Route::post( '{componentId}/upload', - [Components\ComponentFilesController::class, 'store'] + [Components\ComponentsFilesController::class, 'store'] )->name('upload/component'); Route::delete( '{componentId}/deletefile/{fileId}', - [Components\ComponentFilesController::class, 'destroy'] + [Components\ComponentsFilesController::class, 'destroy'] )->name('delete/componentfile'); Route::get( '{componentId}/showfile/{fileId}/{download?}', - [Components\ComponentFilesController::class, 'show'] + [Components\ComponentsFilesController::class, 'show'] )->name('show.componentfile'); }); From 8887d40b864bfc43a460696dd04ec9356194d951 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 1 Nov 2022 19:29:21 -0700 Subject: [PATCH 9/9] Added gitignore Signed-off-by: snipe --- storage/private_uploads/components/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 storage/private_uploads/components/.gitignore diff --git a/storage/private_uploads/components/.gitignore b/storage/private_uploads/components/.gitignore new file mode 100755 index 000000000..c96a04f00 --- /dev/null +++ b/storage/private_uploads/components/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file