WIP - this needs refactoring

We have to use Storage::get() if the filesystem is local, since the method does a file_get_contents() and the file isn’t accessible via a URL since it’s private and doesn’t live on the web root. (We do this slightly differently than Laravel out of the box)

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2020-05-28 01:59:01 -07:00
parent 7a785b4952
commit 262eb79471
No known key found for this signature in database
GPG key ID: 10BFFDA3ED34B5AC

View file

@ -38,10 +38,12 @@ class LicenseFilesController extends Controller
$upload_success = false;
foreach ($request->file('file') as $file) {
$extension = $file->getClientOriginalExtension();
$file_name = 'license-'.$license->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension;
$upload_success = Storage::put('private_uploads/licenses/'.$file_name, $file);
$file_name = 'license-'.date('Y-m-d-His').'-'.$file->getBasename().'.'.$file->getClientOriginalExtension();
$upload_success = $file->storeAs('private_uploads/licenses', $file_name);
// $upload_success = $file->storeAs('private_uploads/licenses/'.$file_name, $file);
//Log the upload to the log
$license->logUpload($file_name, e($request->input('notes')));
@ -116,6 +118,7 @@ class LicenseFilesController extends Controller
public function show($licenseId = null, $fileId = null, $download = true)
{
\Log::info('Private filesystem is: '.config('filesystems.default') );
$license = License::find($licenseId);
// the license is valid
@ -128,22 +131,37 @@ class LicenseFilesController extends Controller
}
$file = 'private_uploads/licenses/'.$log->filename;
\Log::debug('Checking for '.$file);
if (!Storage::exists($file)) {
return response('File '.$file.' not found on server', 404)
if (Storage::missing($file)) {
\Log::debug('NOT EXISTS for '.$file);
\Log::debug('NOT EXISTS 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') {
\Log::debug('The private filesystem is local');
return Response::make(Storage::get($file));
} else {
if ($download != 'true') {
if ($contents = file_get_contents(Storage::url($file))) {
return Response::make(Storage::url($file)->header('Content-Type', mime_content_type($file)));
}
return JsonResponse::create(["error" => "Failed validation: "], 500);
}
return Storage::download($file);
}
return redirect()->route('hardware.index')->with('error', trans('admin/licenses/message.does_not_exist', ['id' => $fileId]));
}
}
return redirect()->route('license.index')->with('error', trans('admin/licenses/message.does_not_exist', ['id' => $fileId]));
}