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;
|
2024-02-07 07:21:48 -08:00
|
|
|
use App\Http\Requests\UploadFileRequest;
|
2018-07-16 17:44:03 -07:00
|
|
|
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;
|
2018-07-16 17:44:03 -07:00
|
|
|
|
|
|
|
class AssetFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Upload a file to the server.
|
|
|
|
*
|
2024-02-07 07:21:48 -08:00
|
|
|
* @param UploadFileRequest $request
|
2018-07-16 17:44:03 -07:00
|
|
|
* @param int $assetId
|
|
|
|
* @return Redirect
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2024-02-07 07:21:48 -08:00
|
|
|
*@since [v1.0]
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2018-07-16 17:44:03 -07:00
|
|
|
*/
|
2024-02-07 07:21:48 -08:00
|
|
|
public function store(UploadFileRequest $request, $assetId = null)
|
2018-07-16 17:44:03 -07:00
|
|
|
{
|
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) {
|
2024-02-07 07:21:48 -08:00
|
|
|
$file_name = $request->handleFile('private_uploads/assets/','hardware-'.$asset->id, $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'));
|
|
|
|
}
|
|
|
|
}
|