2022-11-01 19:10:04 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Components;
|
|
|
|
|
|
|
|
use App\Helpers\StorageHelper;
|
|
|
|
use App\Http\Controllers\Controller;
|
2024-02-07 07:21:48 -08:00
|
|
|
use App\Http\Requests\UploadFileRequest;
|
2022-11-01 19:10:04 -07:00
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Component;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
|
|
|
class ComponentsFilesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validates and stores files associated with a component.
|
|
|
|
*
|
2024-02-07 07:21:48 -08:00
|
|
|
* @param UploadFileRequest $request
|
2022-11-01 19:10:04 -07:00
|
|
|
* @param int $componentId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2024-02-07 07:21:48 -08:00
|
|
|
*@author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @todo Switch to using the AssetFileRequest form request validator.
|
2022-11-01 19:10:04 -07:00
|
|
|
*/
|
2024-02-07 07:21:48 -08:00
|
|
|
public function store(UploadFileRequest $request, $componentId = null)
|
2022-11-01 19:10:04 -07:00
|
|
|
{
|
2022-11-03 13:52:23 -07:00
|
|
|
|
|
|
|
if (config('app.lock_passwords')) {
|
|
|
|
return redirect()->route('components.show', ['component'=>$componentId])->with('error', trans('general.feature_disabled'));
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:10:04 -07:00
|
|
|
$component = Component::find($componentId);
|
|
|
|
|
|
|
|
if (isset($component->id)) {
|
|
|
|
$this->authorize('update', $component);
|
|
|
|
|
|
|
|
if ($request->hasFile('file')) {
|
|
|
|
if (! Storage::exists('private_uploads/components')) {
|
|
|
|
Storage::makeDirectory('private_uploads/components', 775);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($request->file('file') as $file) {
|
2024-02-07 07:21:48 -08:00
|
|
|
$file_name = $request->handleFile('private_uploads/components/','component-'.$component->id, $file);
|
2022-11-01 19:10:04 -07:00
|
|
|
|
|
|
|
//Log the upload to the log
|
|
|
|
$component->logUpload($file_name, e($request->input('notes')));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-01 19:28:39 -07:00
|
|
|
return redirect()->route('components.show', $component->id)->with('success', trans('general.file_upload_success'));
|
2022-11-01 19:10:04 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:28:39 -07:00
|
|
|
return redirect()->route('components.show', $component->id)->with('error', trans('general.no_files_uploaded'));
|
2022-11-01 19:10:04 -07:00
|
|
|
}
|
|
|
|
// Prepare the error message
|
|
|
|
return redirect()->route('components.index')
|
2022-11-01 19:28:39 -07:00
|
|
|
->with('error', trans('general.file_does_not_exist'));
|
2022-11-01 19:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the selected component file.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $componentId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($componentId = null, $fileId = null)
|
|
|
|
{
|
|
|
|
$component = Component::find($componentId);
|
|
|
|
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($component->id)) {
|
|
|
|
$this->authorize('update', $component);
|
|
|
|
$log = Actionlog::find($fileId);
|
|
|
|
|
|
|
|
// Remove the file if one exists
|
|
|
|
if (Storage::exists('components/'.$log->filename)) {
|
|
|
|
try {
|
|
|
|
Storage::delete('components/'.$log->filename);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$log->delete();
|
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->with('success', trans('admin/hardware/message.deletefile.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the licence management page
|
2022-11-01 19:28:39 -07:00
|
|
|
return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist'));
|
2022-11-01 19:10:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows the selected file to be viewed.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.4]
|
|
|
|
* @param int $componentId
|
|
|
|
* @param int $fileId
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2023-09-05 10:28:01 -07:00
|
|
|
public function show($componentId = null, $fileId = null)
|
2022-11-01 19:10:04 -07:00
|
|
|
{
|
|
|
|
\Log::debug('Private filesystem is: '.config('filesystems.default'));
|
|
|
|
$component = Component::find($componentId);
|
|
|
|
|
|
|
|
// the component is valid
|
|
|
|
if (isset($component->id)) {
|
|
|
|
$this->authorize('view', $component);
|
|
|
|
$this->authorize('components.files', $component);
|
|
|
|
|
2023-10-09 08:43:14 -07:00
|
|
|
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $component->id)->find($fileId)) {
|
2022-11-01 19:10:04 -07:00
|
|
|
return response('No matching record for that asset/file', 500)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = 'private_uploads/components/'.$log->filename;
|
|
|
|
|
|
|
|
if (Storage::missing($file)) {
|
|
|
|
\Log::debug('FILE DOES NOT EXISTS for '.$file);
|
|
|
|
\Log::debug('URL should be '.Storage::url($file));
|
|
|
|
|
|
|
|
return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
} else {
|
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
// Display the file inline
|
|
|
|
if (request('inline') == 'true') {
|
|
|
|
$headers = [
|
|
|
|
'Content-Disposition' => 'inline',
|
|
|
|
];
|
|
|
|
return Storage::download($file, $log->filename, $headers);
|
|
|
|
}
|
2022-11-01 19:10:04 -07:00
|
|
|
|
2023-09-05 10:28:01 -07:00
|
|
|
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
|
2022-11-01 19:10:04 -07:00
|
|
|
return StorageHelper::downloader($file);
|
2023-09-05 10:28:01 -07:00
|
|
|
}
|
2022-11-01 19:10:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:28:39 -07:00
|
|
|
return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));
|
2022-11-01 19:10:04 -07:00
|
|
|
}
|
|
|
|
}
|