-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12057 from snipe/features/add_uploads_to_components
Added uploads to components
- Loading branch information
Showing
8 changed files
with
386 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
app/Http/Controllers/Consumables/ConsumablesFilesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Consumables; | ||
|
||
use App\Helpers\StorageHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\AssetFileRequest; | ||
use App\Models\Actionlog; | ||
use App\Models\Consumable; | ||
use Illuminate\Support\Facades\Response; | ||
use Illuminate\Support\Facades\Storage; | ||
use Symfony\Consumable\HttpFoundation\JsonResponse; | ||
use enshrined\svgSanitize\Sanitizer; | ||
|
||
class ConsumablesFilesController extends Controller | ||
{ | ||
/** | ||
* Validates and stores files associated with a consumable. | ||
* | ||
* @todo Switch to using the AssetFileRequest form request validator. | ||
* @author [A. Gianotto] [<[email protected]>] | ||
* @since [v1.0] | ||
* @param AssetFileRequest $request | ||
* @param int $consumableId | ||
* @return \Illuminate\Http\RedirectResponse | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function store(AssetFileRequest $request, $consumableId = null) | ||
{ | ||
$consumable = Consumable::find($consumableId); | ||
|
||
if (isset($consumable->id)) { | ||
$this->authorize('update', $consumable); | ||
|
||
if ($request->hasFile('file')) { | ||
if (! Storage::exists('private_uploads/consumables')) { | ||
Storage::makeDirectory('private_uploads/consumables', 775); | ||
} | ||
|
||
foreach ($request->file('file') as $file) { | ||
|
||
$extension = $file->getClientOriginalExtension(); | ||
$file_name = 'consumable-'.$consumable->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/consumables/'.$file_name, $cleanSVG); | ||
} catch (\Exception $e) { | ||
\Log::debug('Upload no workie :( '); | ||
\Log::debug($e); | ||
} | ||
|
||
} else { | ||
Storage::put('private_uploads/consumables/'.$file_name, file_get_contents($file)); | ||
} | ||
|
||
//Log the upload to the log | ||
$consumable->logUpload($file_name, e($request->input('notes'))); | ||
} | ||
|
||
|
||
return redirect()->route('consumables.show', $consumable->id)->with('success', trans('admin/consumables/message.upload.success')); | ||
|
||
} | ||
|
||
return redirect()->route('consumables.show', $consumable->id)->with('error', trans('admin/consumables/message.upload.nofiles')); | ||
} | ||
// Prepare the error message | ||
return redirect()->route('consumables.index') | ||
->with('error', trans('admin/consumables/message.does_not_exist')); | ||
} | ||
|
||
/** | ||
* Deletes the selected consumable file. | ||
* | ||
* @author [A. Gianotto] [<[email protected]>] | ||
* @since [v1.0] | ||
* @param int $consumableId | ||
* @param int $fileId | ||
* @return \Illuminate\Http\RedirectResponse | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function destroy($consumableId = null, $fileId = null) | ||
{ | ||
$consumable = Consumable::find($consumableId); | ||
|
||
// the asset is valid | ||
if (isset($consumable->id)) { | ||
$this->authorize('update', $consumable); | ||
$log = Actionlog::find($fileId); | ||
|
||
// Remove the file if one exists | ||
if (Storage::exists('consumables/'.$log->filename)) { | ||
try { | ||
Storage::delete('consumables/'.$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('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist')); | ||
} | ||
|
||
/** | ||
* Allows the selected file to be viewed. | ||
* | ||
* @author [A. Gianotto] [<[email protected]>] | ||
* @since [v1.4] | ||
* @param int $consumableId | ||
* @param int $fileId | ||
* @return \Symfony\Consumable\HttpFoundation\Response | ||
* @throws \Illuminate\Auth\Access\AuthorizationException | ||
*/ | ||
public function show($consumableId = null, $fileId = null, $download = true) | ||
{ | ||
$consumable = Consumable::find($consumableId); | ||
|
||
// the consumable is valid | ||
if (isset($consumable->id)) { | ||
$this->authorize('view', $consumable); | ||
$this->authorize('consumables.files', $consumable); | ||
|
||
if (! $log = Actionlog::find($fileId)) { | ||
return response('No matching record for that asset/file', 500) | ||
->header('Content-Type', 'text/plain'); | ||
} | ||
|
||
$file = 'private_uploads/consumables/'.$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('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist', ['id' => $fileId])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,24 @@ class Consumable extends SnipeModel | |
'manufacturer' => ['name'], | ||
]; | ||
|
||
|
||
/** | ||
* Establishes the components -> action logs -> uploads relationship | ||
* | ||
* @author A. Gianotto <[email protected]> | ||
* @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'); | ||
} | ||
|
||
|
||
/** | ||
* Sets the attribute of whether or not the consumable is requestable | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.