diff --git a/app/Http/Controllers/LocationsFilesController.php b/app/Http/Controllers/LocationsFilesController.php new file mode 100644 index 000000000..41425124c --- /dev/null +++ b/app/Http/Controllers/LocationsFilesController.php @@ -0,0 +1,112 @@ +] + */ + public function store(UploadFileRequest $request, Location $location) : RedirectResponse + { + + $this->authorize('update', $location); + + if ($request->hasFile('file')) { + if (! Storage::exists('private_uploads/locations')) { + Storage::makeDirectory('private_uploads/locations', 775); + } + + foreach ($request->file('file') as $file) { + + $file_name = $request->handleFile('private_uploads/locations/','model-'.$location->id,$file); + $location->logUpload($file_name, $request->get('notes')); + } + + return redirect()->back()->withFragment('files')->with('success', trans('general.file_upload_success')); + } + + return redirect()->back()->withFragment('files')->with('error', trans('admin/hardware/message.upload.nofiles')); + } + + /** + * Check for permissions and display the file. + * + * @author [A. Gianotto] [] + * @param int $modelId + * @param int $fileId + * @since [v1.0] + */ + public function show(Location $location, $fileId = null) : StreamedResponse | Response | RedirectResponse | BinaryFileResponse + { + + $this->authorize('view', $location); + + if (! $log = Actionlog::find($fileId)) { + return redirect()->back()->withFragment('files')->with('error', 'No matching file record'); + } + + $file = 'private_uploads/locations/'.$log->filename; + + if (! Storage::exists($file)) { + return redirect()->back()->withFragment('files')->with('error', 'No matching file on server'); + } + + if (request('inline') == 'true') { + + $headers = [ + 'Content-Disposition' => 'inline', + ]; + + return Storage::download($file, $log->filename, $headers); + } + + return StorageHelper::downloader($file); + } + + /** + * Delete the associated file + * + * @author [A. Gianotto] [] + * @param int $modelId + * @param int $fileId + * @since [v1.0] + */ + public function destroy(Location $location, $fileId = null) : RedirectResponse + { + $rel_path = 'private_uploads/locations'; + $this->authorize('update', $location); + $log = Actionlog::find($fileId); + + if ($log) { + + // This should be moved to purge +// if (Storage::exists($rel_path.'/'.$log->filename)) { +// Storage::delete($rel_path.'/'.$log->filename); +// } + $log->delete(); + + return redirect()->back()->withFragment('files')->with('success', trans('admin/hardware/message.deletefile.success')); + } + + return redirect()->back()->withFragment('files')->with('success', trans('admin/hardware/message.deletefile.success')); + + } +} diff --git a/app/Models/Location.php b/app/Models/Location.php index 69c79cfae..9b1ed02f4 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -20,6 +20,7 @@ class Location extends SnipeModel { use HasFactory; use CompanyableTrait; + use Loggable; protected $presenter = \App\Presenters\LocationPresenter::class; use Presentable; @@ -288,6 +289,23 @@ class Location extends SnipeModel return $this->attributes['ldap_ou'] = empty($ldap_ou) ? null : $ldap_ou; } + /** + * Get uploads for this location + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ + public function uploads() + { + return $this->hasMany('\App\Models\Actionlog', 'item_id') + ->where('item_type', '=', Location::class) + ->where('action_type', '=', 'uploaded') + ->whereNotNull('filename') + ->orderBy('created_at', 'desc'); + } + + /** * Query builder scope to order on parent * diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index d5b10bde3..276aaf1e4 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -45,6 +45,7 @@ class RouteServiceProvider extends ServiceProvider require base_path('routes/web/models.php'); require base_path('routes/web/accessories.php'); require base_path('routes/web/licenses.php'); + require base_path('routes/web/locations.php'); require base_path('routes/web/consumables.php'); require base_path('routes/web/fields.php'); require base_path('routes/web/components.php'); diff --git a/resources/views/locations/view.blade.php b/resources/views/locations/view.blade.php index 3d3b6a524..2f7cb075b 100644 --- a/resources/views/locations/view.blade.php +++ b/resources/views/locations/view.blade.php @@ -144,7 +144,22 @@ @endif @endcan - + + @if ($location->uploads->count() > 0 ) +
  • + + + + + +
  • + @endif +
  • @@ -153,6 +168,15 @@
  • + + @can('update', $location) +
  • + + + {{ trans('button.upload') }} + +
  • + @endcan @@ -375,6 +399,22 @@ +
    + +
    +
    + + + +
    +
    + +
    +

    {{ trans('general.history') }}

    @@ -550,6 +590,10 @@ @section('moar_scripts') + @can('update', Location::class) + @include ('modals.upload-file', ['item_type' => 'locations', 'item_id' => $location->id]) + @endcan +