2018-07-16 17:44:03 -07:00
|
|
|
<?php
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
namespace App\Http\Controllers\Assets;
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
use App\Helpers\StorageHelper;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Http\Requests\AssetFileRequest;
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
2018-07-25 09:48:50 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2021-10-06 12:26:45 -07:00
|
|
|
use enshrined\svgSanitize\Sanitizer;
|
2018-07-16 17:44:03 -07:00
|
|
|
|
|
|
|
class AssetFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Upload a file to the server.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param AssetFileRequest $request
|
|
|
|
* @param int $assetId
|
|
|
|
* @return Redirect
|
|
|
|
* @since [v1.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2018-07-16 17:44:03 -07:00
|
|
|
*/
|
|
|
|
public function store(AssetFileRequest $request, $assetId = null)
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $asset = Asset::find($assetId)) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('update', $asset);
|
|
|
|
|
2018-07-16 20:09:53 -07:00
|
|
|
if ($request->hasFile('file')) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Storage::exists('private_uploads/assets')) {
|
|
|
|
Storage::makeDirectory('private_uploads/assets', 775);
|
|
|
|
}
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2018-07-16 20:09:53 -07:00
|
|
|
foreach ($request->file('file') as $file) {
|
2021-10-06 12:26:45 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
$extension = $file->getClientOriginalExtension();
|
2018-09-29 21:33:52 -07:00
|
|
|
$file_name = 'hardware-'.$asset->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension;
|
2021-10-06 12:26:45 -07:00
|
|
|
|
|
|
|
// Check for SVG and sanitize it
|
|
|
|
if ($extension=='svg') {
|
|
|
|
\Log::debug('This is an SVG');
|
|
|
|
|
|
|
|
$sanitizer = new Sanitizer();
|
|
|
|
$dirtySVG = file_get_contents($file->getRealPath());
|
|
|
|
$cleanSVG = $sanitizer->sanitize($dirtySVG);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Storage::put('private_uploads/assets/'.$file_name, $cleanSVG);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug('Upload no workie :( ');
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-05 12:08:18 -08:00
|
|
|
Storage::put('private_uploads/assets/'.$file_name, file_get_contents($file));
|
2021-10-06 12:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
$asset->logUpload($file_name, e($request->get('notes')));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->back()->with('success', trans('admin/hardware/message.upload.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()->with('error', trans('admin/hardware/message.upload.nofiles'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Check for permissions and display the file.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $assetId
|
|
|
|
* @param int $fileId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2023-09-05 10:28:01 -07:00
|
|
|
public function show($assetId = null, $fileId = null)
|
2018-07-16 17:44:03 -07:00
|
|
|
{
|
|
|
|
$asset = Asset::find($assetId);
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($asset->id)) {
|
|
|
|
$this->authorize('view', $asset);
|
|
|
|
|
2023-10-09 08:43:14 -07:00
|
|
|
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return response('No matching record for that asset/file', 500)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
$file = 'private_uploads/assets/'.$log->filename;
|
|
|
|
\Log::debug('Checking for '.$file);
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($log->action_type == 'audit') {
|
2018-09-29 21:33:52 -07:00
|
|
|
$file = 'private_uploads/audits/'.$log->filename;
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! Storage::exists($file)) {
|
2018-07-16 17:44:03 -07:00
|
|
|
return response('File '.$file.' not found on server', 404)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
if (request('inline') == 'true') {
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Content-Disposition' => 'inline',
|
|
|
|
];
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
return Storage::download($file, $log->filename, $headers);
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2021-03-15 12:26:39 -07:00
|
|
|
return StorageHelper::downloader($file);
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
$error = trans('admin/hardware/message.does_not_exist', ['id' => $fileId]);
|
|
|
|
|
|
|
|
// Redirect to the hardware management page
|
|
|
|
return redirect()->route('hardware.index')->with('error', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Delete the associated file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $assetId
|
|
|
|
* @param int $fileId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2018-07-16 17:44:03 -07:00
|
|
|
public function destroy($assetId = null, $fileId = null)
|
|
|
|
{
|
|
|
|
$asset = Asset::find($assetId);
|
|
|
|
$this->authorize('update', $asset);
|
2021-05-04 22:18:22 -07:00
|
|
|
$rel_path = 'private_uploads/assets';
|
2018-07-16 17:44:03 -07:00
|
|
|
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($asset->id)) {
|
|
|
|
$this->authorize('update', $asset);
|
|
|
|
$log = Actionlog::find($fileId);
|
2019-05-24 15:28:53 -07:00
|
|
|
if ($log) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if (Storage::exists($rel_path.'/'.$log->filename)) {
|
|
|
|
Storage::delete($rel_path.'/'.$log->filename);
|
2019-05-24 15:28:53 -07:00
|
|
|
}
|
|
|
|
$log->delete();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-05-24 15:28:53 -07:00
|
|
|
return redirect()->back()->with('success', trans('admin/hardware/message.deletefile.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-25 11:57:49 -07:00
|
|
|
return redirect()->back()
|
|
|
|
->with('success', trans('admin/hardware/message.deletefile.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the hardware management page
|
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
}
|