|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Accessories; |
| 4 | + |
| 5 | +use App\Helpers\StorageHelper; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | +use App\Http\Requests\AssetFileRequest; |
| 8 | +use App\Models\Actionlog; |
| 9 | +use App\Models\Accessory; |
| 10 | +use Illuminate\Support\Facades\Response; |
| 11 | +use Illuminate\Support\Facades\Storage; |
| 12 | +use Symfony\Accessory\HttpFoundation\JsonResponse; |
| 13 | +use enshrined\svgSanitize\Sanitizer; |
| 14 | + |
| 15 | +class AccessoriesFilesController extends Controller |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Validates and stores files associated with a accessory. |
| 19 | + * |
| 20 | + * @todo Switch to using the AssetFileRequest form request validator. |
| 21 | + * @author [A. Gianotto] [<[email protected]>] |
| 22 | + * @since [v1.0] |
| 23 | + * @param AssetFileRequest $request |
| 24 | + * @param int $accessoryId |
| 25 | + * @return \Illuminate\Http\RedirectResponse |
| 26 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 27 | + */ |
| 28 | + public function store(AssetFileRequest $request, $accessoryId = null) |
| 29 | + { |
| 30 | + $accessory = Accessory::find($accessoryId); |
| 31 | + |
| 32 | + if (isset($accessory->id)) { |
| 33 | + $this->authorize('update', $accessory); |
| 34 | + |
| 35 | + if ($request->hasFile('file')) { |
| 36 | + if (! Storage::exists('private_uploads/accessories')) { |
| 37 | + Storage::makeDirectory('private_uploads/accessories', 775); |
| 38 | + } |
| 39 | + |
| 40 | + foreach ($request->file('file') as $file) { |
| 41 | + |
| 42 | + $extension = $file->getClientOriginalExtension(); |
| 43 | + $file_name = 'accessory-'.$accessory->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension; |
| 44 | + |
| 45 | + |
| 46 | + // Check for SVG and sanitize it |
| 47 | + if ($extension == 'svg') { |
| 48 | + \Log::debug('This is an SVG'); |
| 49 | + \Log::debug($file_name); |
| 50 | + |
| 51 | + $sanitizer = new Sanitizer(); |
| 52 | + $dirtySVG = file_get_contents($file->getRealPath()); |
| 53 | + $cleanSVG = $sanitizer->sanitize($dirtySVG); |
| 54 | + |
| 55 | + try { |
| 56 | + Storage::put('private_uploads/accessories/'.$file_name, $cleanSVG); |
| 57 | + } catch (\Exception $e) { |
| 58 | + \Log::debug('Upload no workie :( '); |
| 59 | + \Log::debug($e); |
| 60 | + } |
| 61 | + |
| 62 | + } else { |
| 63 | + Storage::put('private_uploads/accessories/'.$file_name, file_get_contents($file)); |
| 64 | + } |
| 65 | + |
| 66 | + //Log the upload to the log |
| 67 | + $accessory->logUpload($file_name, e($request->input('notes'))); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + return redirect()->route('accessories.show', $accessory->id)->with('success', trans('general.file_upload_success')); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + return redirect()->route('accessories.show', $accessory->id)->with('error', trans('general.no_files_uploaded')); |
| 76 | + } |
| 77 | + // Prepare the error message |
| 78 | + return redirect()->route('accessories.index') |
| 79 | + ->with('error', trans('general.file_does_not_exist')); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Deletes the selected accessory file. |
| 84 | + * |
| 85 | + * @author [A. Gianotto] [<[email protected]>] |
| 86 | + * @since [v1.0] |
| 87 | + * @param int $accessoryId |
| 88 | + * @param int $fileId |
| 89 | + * @return \Illuminate\Http\RedirectResponse |
| 90 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 91 | + */ |
| 92 | + public function destroy($accessoryId = null, $fileId = null) |
| 93 | + { |
| 94 | + $accessory = Accessory::find($accessoryId); |
| 95 | + |
| 96 | + // the asset is valid |
| 97 | + if (isset($accessory->id)) { |
| 98 | + $this->authorize('update', $accessory); |
| 99 | + $log = Actionlog::find($fileId); |
| 100 | + |
| 101 | + // Remove the file if one exists |
| 102 | + if (Storage::exists('accessories/'.$log->filename)) { |
| 103 | + try { |
| 104 | + Storage::delete('accessories/'.$log->filename); |
| 105 | + } catch (\Exception $e) { |
| 106 | + \Log::debug($e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $log->delete(); |
| 111 | + |
| 112 | + return redirect()->back() |
| 113 | + ->with('success', trans('admin/hardware/message.deletefile.success')); |
| 114 | + } |
| 115 | + |
| 116 | + // Redirect to the licence management page |
| 117 | + return redirect()->route('accessories.index')->with('error', trans('general.file_does_not_exist')); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Allows the selected file to be viewed. |
| 122 | + * |
| 123 | + * @author [A. Gianotto] [<[email protected]>] |
| 124 | + * @since [v1.4] |
| 125 | + * @param int $accessoryId |
| 126 | + * @param int $fileId |
| 127 | + * @return \Symfony\Accessory\HttpFoundation\Response |
| 128 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 129 | + */ |
| 130 | + public function show($accessoryId = null, $fileId = null, $download = true) |
| 131 | + { |
| 132 | + \Log::debug('Private filesystem is: '.config('filesystems.default')); |
| 133 | + $accessory = Accessory::find($accessoryId); |
| 134 | + |
| 135 | + // the accessory is valid |
| 136 | + if (isset($accessory->id)) { |
| 137 | + $this->authorize('view', $accessory); |
| 138 | + $this->authorize('accessories.files', $accessory); |
| 139 | + |
| 140 | + if (! $log = Actionlog::find($fileId)) { |
| 141 | + return response('No matching record for that asset/file', 500) |
| 142 | + ->header('Content-Type', 'text/plain'); |
| 143 | + } |
| 144 | + |
| 145 | + $file = 'private_uploads/accessories/'.$log->filename; |
| 146 | + |
| 147 | + if (Storage::missing($file)) { |
| 148 | + \Log::debug('FILE DOES NOT EXISTS for '.$file); |
| 149 | + \Log::debug('URL should be '.Storage::url($file)); |
| 150 | + |
| 151 | + return response('File '.$file.' ('.Storage::url($file).') not found on server', 404) |
| 152 | + ->header('Content-Type', 'text/plain'); |
| 153 | + } else { |
| 154 | + |
| 155 | + // We have to override the URL stuff here, since local defaults in Laravel's Flysystem |
| 156 | + // won't work, as they're not accessible via the web |
| 157 | + if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer? |
| 158 | + return StorageHelper::downloader($file); |
| 159 | + } else { |
| 160 | + if ($download != 'true') { |
| 161 | + \Log::debug('display the file'); |
| 162 | + if ($contents = file_get_contents(Storage::url($file))) { // TODO - this will fail on private S3 files or large public ones |
| 163 | + return Response::make(Storage::url($file)->header('Content-Type', mime_content_type($file))); |
| 164 | + } |
| 165 | + |
| 166 | + return JsonResponse::create(['error' => 'Failed validation: '], 500); |
| 167 | + } |
| 168 | + |
| 169 | + return StorageHelper::downloader($file); |
| 170 | + |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return redirect()->route('accessories.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId])); |
| 176 | + } |
| 177 | +} |
0 commit comments