2022-06-28 15:50:07 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Helpers\StorageHelper;
|
2024-02-07 07:21:48 -08:00
|
|
|
use App\Http\Requests\UploadFileRequest;
|
2022-06-28 15:50:07 -07:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class AssetModelsFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Upload a file to the server.
|
|
|
|
*
|
2024-02-07 07:21:48 -08:00
|
|
|
* @param UploadFileRequest $request
|
2022-06-28 15:50:07 -07:00
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2024-02-07 07:21:48 -08:00
|
|
|
*@since [v1.0]
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2022-06-28 15:50:07 -07:00
|
|
|
*/
|
2024-02-07 07:21:48 -08:00
|
|
|
public function store(UploadFileRequest $request, $modelId = null)
|
2022-06-28 15:50:07 -07:00
|
|
|
{
|
|
|
|
if (! $model = AssetModel::find($modelId)) {
|
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('update', $model);
|
|
|
|
|
|
|
|
if ($request->hasFile('file')) {
|
|
|
|
if (! Storage::exists('private_uploads/assetmodels')) {
|
|
|
|
Storage::makeDirectory('private_uploads/assetmodels', 775);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($request->file('file') as $file) {
|
|
|
|
|
2024-02-07 07:21:48 -08:00
|
|
|
$file_name = $request->handleFile('private_uploads/assetmodels/','model-'.$model->id,$file);
|
2022-06-28 15:50:07 -07:00
|
|
|
|
|
|
|
$model->logUpload($file_name, e($request->get('notes')));
|
|
|
|
}
|
|
|
|
|
2022-11-03 13:57:50 -07:00
|
|
|
return redirect()->back()->with('success', trans('general.file_upload_success'));
|
2022-06-28 15:50:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()->with('error', trans('admin/hardware/message.upload.nofiles'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for permissions and display the file.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $modelId
|
|
|
|
* @param int $fileId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2023-09-05 10:28:01 -07:00
|
|
|
public function show($modelId = null, $fileId = null)
|
2022-06-28 15:50:07 -07:00
|
|
|
{
|
|
|
|
$model = AssetModel::find($modelId);
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($model->id)) {
|
|
|
|
$this->authorize('view', $model);
|
|
|
|
|
|
|
|
if (! $log = Actionlog::find($fileId)) {
|
|
|
|
return response('No matching record for that model/file', 500)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = 'private_uploads/assetmodels/'.$log->filename;
|
|
|
|
\Log::debug('Checking for '.$file);
|
|
|
|
|
|
|
|
|
|
|
|
if (! Storage::exists($file)) {
|
|
|
|
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',
|
|
|
|
];
|
2022-06-28 15:50:07 -07:00
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
return Storage::download($file, $log->filename, $headers);
|
2022-06-28 15:50:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return StorageHelper::downloader($file);
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the associated file
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $modelId
|
|
|
|
* @param int $fileId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($modelId = null, $fileId = null)
|
|
|
|
{
|
|
|
|
$model = AssetModel::find($modelId);
|
|
|
|
$this->authorize('update', $model);
|
|
|
|
$rel_path = 'private_uploads/assetmodels';
|
|
|
|
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($model->id)) {
|
|
|
|
$this->authorize('update', $model);
|
|
|
|
$log = Actionlog::find($fileId);
|
|
|
|
if ($log) {
|
|
|
|
if (Storage::exists($rel_path.'/'.$log->filename)) {
|
|
|
|
Storage::delete($rel_path.'/'.$log->filename);
|
|
|
|
}
|
|
|
|
$log->delete();
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', trans('admin/hardware/message.deletefile.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->with('success', trans('admin/hardware/message.deletefile.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the hardware management page
|
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
}
|