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
|
|
|
|
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;
|
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)
|
|
|
|
{
|
|
|
|
if (!$asset = Asset::find($assetId)) {
|
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('update', $asset);
|
|
|
|
|
2018-07-24 22:52:49 -07:00
|
|
|
if ($request->hasFile('file')) {
|
2018-07-16 20:09:53 -07:00
|
|
|
foreach ($request->file('file') as $file) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$extension = $file->getClientOriginalExtension();
|
|
|
|
$filename = 'hardware-'.$asset->id.'-'.str_random(8);
|
|
|
|
$filename .= '-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension;
|
2018-07-25 09:48:50 -07:00
|
|
|
|
|
|
|
$file->storeAs('storage/private_uploads/assets', $filename);
|
2018-07-16 17:44:03 -07:00
|
|
|
$asset->logUpload($filename, e($request->get('notes')));
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2018-07-16 17:44:03 -07:00
|
|
|
public function show($assetId = null, $fileId = null, $download = true)
|
|
|
|
{
|
|
|
|
$asset = Asset::find($assetId);
|
|
|
|
// the asset is valid
|
|
|
|
if (isset($asset->id)) {
|
|
|
|
$this->authorize('view', $asset);
|
|
|
|
|
|
|
|
if (!$log = Actionlog::find($fileId)) {
|
|
|
|
return response('No matching record for that asset/file', 500)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = $log->get_src('assets');
|
|
|
|
|
|
|
|
if ($log->action_type =='audit') {
|
|
|
|
$file = $log->get_src('audits');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
return response('File '.$file.' not found on server', 404)
|
|
|
|
->header('Content-Type', 'text/plain');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($download != 'true') {
|
|
|
|
if ($contents = file_get_contents($file)) {
|
|
|
|
return Response::make($contents)->header('Content-Type', mime_content_type($file));
|
|
|
|
}
|
|
|
|
return JsonResponse::create(["error" => "Failed validation: "], 500);
|
|
|
|
}
|
|
|
|
return Response::download($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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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);
|
2018-07-25 11:57:49 -07:00
|
|
|
$rel_path = 'storage/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);
|
2018-07-25 11:57:49 -07:00
|
|
|
if (file_exists(base_path().'/'.$rel_path.'/'.$log->filename)) {
|
|
|
|
Storage::delete($rel_path.'/'.$log->filename);
|
2018-07-16 17:44:03 -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-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'));
|
|
|
|
}
|
|
|
|
}
|