2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Licenses;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Requests\AssetFileRequest;
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\License;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
2018-07-25 09:48:50 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2021-03-15 12:26:39 -07:00
|
|
|
use App\Helpers\StorageHelper;
|
2021-10-06 12:26:45 -07:00
|
|
|
use enshrined\svgSanitize\Sanitizer;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class LicenseFilesController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and stores files associated with a license.
|
|
|
|
*
|
|
|
|
* @todo Switch to using the AssetFileRequest form request validator.
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param AssetFileRequest $request
|
|
|
|
* @param int $licenseId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function store(AssetFileRequest $request, $licenseId = null)
|
|
|
|
{
|
|
|
|
$license = License::find($licenseId);
|
|
|
|
|
|
|
|
if (isset($license->id)) {
|
|
|
|
$this->authorize('update', $license);
|
|
|
|
|
2020-05-23 08:36:16 -07:00
|
|
|
if ($request->hasFile('file')) {
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
if (!Storage::exists('private_uploads/licenses')) Storage::makeDirectory('private_uploads/licenses', 775);
|
|
|
|
|
2020-05-23 08:36:16 -07:00
|
|
|
foreach ($request->file('file') as $file) {
|
2018-07-25 09:48:50 -07:00
|
|
|
|
2021-10-06 12:26:45 -07:00
|
|
|
$extension = $file->getClientOriginalExtension();
|
|
|
|
$file_name = 'license-'.$license->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension;
|
2020-11-12 15:13:45 -08:00
|
|
|
|
2020-05-28 01:59:01 -07:00
|
|
|
|
2021-10-06 12:26:45 -07:00
|
|
|
// Check for SVG and sanitize it
|
|
|
|
if ($extension == 'svg') {
|
|
|
|
\Log::error('This is an SVG');
|
|
|
|
\Log::error($file_name);
|
2020-05-28 01:59:01 -07:00
|
|
|
|
2021-10-06 12:26:45 -07:00
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$dirtySVG = file_get_contents($file->getRealPath());
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Storage::put('private_uploads/licenses/'.$file_name, $cleanSVG);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::error('Upload no workie :( ');
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Storage::put('private_uploads/licenses/'.$file_name, file_get_contents($file));
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
//Log the upload to the log
|
2018-09-29 21:33:52 -07:00
|
|
|
$license->logUpload($file_name, e($request->input('notes')));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2021-10-06 12:26:45 -07:00
|
|
|
|
|
|
|
\Log::error('yay!');
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('licenses.show', $license->id)->with('success', trans('admin/licenses/message.upload.success'));
|
2021-10-06 12:26:45 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
return redirect()->route('licenses.show', $license->id)->with('error', trans('admin/licenses/message.upload.nofiles'));
|
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
return redirect()->route('licenses.index')
|
|
|
|
->with('error', trans('admin/licenses/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the selected license file.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $licenseId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($licenseId = null, $fileId = null)
|
|
|
|
{
|
|
|
|
$license = License::find($licenseId);
|
|
|
|
|
2018-07-25 11:57:49 -07:00
|
|
|
// the asset is valid
|
2018-07-24 19:35:26 -07:00
|
|
|
if (isset($license->id)) {
|
2018-07-25 11:57:49 -07:00
|
|
|
$this->authorize('update', $license);
|
2018-07-24 19:35:26 -07:00
|
|
|
$log = Actionlog::find($fileId);
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
// Remove the file if one exists
|
|
|
|
if (Storage::exists('licenses/'.$log->filename)) {
|
|
|
|
try {
|
|
|
|
Storage::delete('licenses/'.$log->filename);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$log->delete();
|
2018-07-25 11:57:49 -07:00
|
|
|
return redirect()->back()
|
|
|
|
->with('success', trans('admin/hardware/message.deletefile.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the licence management page
|
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows the selected file to be viewed.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.4]
|
|
|
|
* @param int $licenseId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function show($licenseId = null, $fileId = null, $download = true)
|
|
|
|
{
|
|
|
|
|
2020-05-28 01:59:01 -07:00
|
|
|
\Log::info('Private filesystem is: '.config('filesystems.default') );
|
2018-07-24 19:35:26 -07:00
|
|
|
$license = License::find($licenseId);
|
|
|
|
|
|
|
|
// the license is valid
|
|
|
|
if (isset($license->id)) {
|
|
|
|
$this->authorize('view', $license);
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
if (!$log = Actionlog::find($fileId)) {
|
|
|
|
return response('No matching record for that asset/file', 500)
|
2018-07-24 19:35:26 -07:00
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
$file = 'private_uploads/licenses/'.$log->filename;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
|
2020-05-28 01:59:01 -07:00
|
|
|
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
|
2021-03-15 12:26:39 -07:00
|
|
|
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
|
|
|
|
return StorageHelper::downloader($file);
|
2020-05-28 01:59:01 -07:00
|
|
|
} else {
|
|
|
|
if ($download != 'true') {
|
2020-10-21 13:32:46 -07:00
|
|
|
\Log::debug('display the file');
|
2021-03-15 12:26:39 -07:00
|
|
|
if ($contents = file_get_contents(Storage::url($file))) { // TODO - this will fail on private S3 files or large public ones
|
2020-05-28 01:59:01 -07:00
|
|
|
return Response::make(Storage::url($file)->header('Content-Type', mime_content_type($file)));
|
|
|
|
}
|
|
|
|
return JsonResponse::create(["error" => "Failed validation: "], 500);
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:26:39 -07:00
|
|
|
return StorageHelper::downloader($file);
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2020-05-28 01:59:01 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2020-05-28 01:59:01 -07:00
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2020-05-28 01:59:01 -07:00
|
|
|
return redirect()->route('license.index')->with('error', trans('admin/licenses/message.does_not_exist', ['id' => $fileId]));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|