From 0be69f57acef0fdf42f6283a89c12cf335a4b214 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 2 May 2018 14:13:06 -0700 Subject: [PATCH] Improved files display --- app/Helpers/Helper.php | 60 +++++++++++++++++++ .../Controllers/Api/ReportsController.php | 5 +- app/Http/Controllers/AssetsController.php | 10 ++-- app/Http/Controllers/LicensesController.php | 30 ++++++++-- app/Http/Controllers/ReportsController.php | 2 +- app/Http/Requests/AssetFileRequest.php | 3 +- .../Transformers/ActionlogsTransformer.php | 18 ++++-- app/Models/Actionlog.php | 7 ++- resources/lang/en/admin/hardware/general.php | 1 - resources/lang/en/general.php | 1 + resources/views/hardware/view.blade.php | 55 +++++++++++------ resources/views/licenses/view.blade.php | 19 +++--- .../views/partials/bootstrap-table.blade.php | 16 +++++ routes/web.php | 2 +- routes/web/licenses.php | 4 +- 15 files changed, 184 insertions(+), 49 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 6b0f0f98a3..93d198cd4f 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -787,6 +787,66 @@ class Helper } + public static function filetype_icon($filename) { + + $extension = substr(strrchr($filename,'.'),1); + + if ($extension) { + switch ($extension) { + case 'jpg': + case 'jpeg': + case 'gif': + case 'png': + return "fa fa-file-image-o"; + break; + case 'doc': + case 'docx': + return "fa fa-file-word-o"; + break; + case 'xls': + case 'xlsx': + return "fa fa-file-excel-o"; + break; + case 'zip': + case 'rar': + return "fa fa-file-archive-o"; + break; + case 'pdf': + return "fa fa-file-pdf-o"; + break; + case 'txt': + return "fa fa-file-text-o"; + break; + case 'lic': + return "fa fa-floppy-o"; + break; + default: + return "fa fa-file-o"; + } + } + return "fa fa-file-o"; + } + + public static function show_file_inline($filename) { + + $extension = substr(strrchr($filename,'.'),1); + + if ($extension) { + switch ($extension) { + case 'jpg': + case 'jpeg': + case 'gif': + case 'png': + return true; + break; + default: + return false; + } + } + return false; + } + + } diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.php index ca461398e8..7bc974f9c9 100644 --- a/app/Http/Controllers/Api/ReportsController.php +++ b/app/Http/Controllers/Api/ReportsController.php @@ -28,7 +28,6 @@ class ReportsController extends Controller if (($request->has('target_type')) && ($request->has('target_id'))) { $actionlogs = $actionlogs->where('target_id','=',$request->input('target_id')) ->where('target_type','=',"App\\Models\\".ucwords($request->input('target_type'))); - } if (($request->has('item_type')) && ($request->has('item_id'))) { @@ -40,6 +39,10 @@ class ReportsController extends Controller $actionlogs = $actionlogs->where('action_type','=',$request->input('action_type'))->orderBy('created_at', 'desc'); } + if ($request->has('uploads')) { + $actionlogs = $actionlogs->whereNotNull('filename')->orderBy('created_at', 'desc'); + } + $allowed_columns = [ 'id', 'created_at', diff --git a/app/Http/Controllers/AssetsController.php b/app/Http/Controllers/AssetsController.php index 05a38623cf..9007d8a4bc 100755 --- a/app/Http/Controllers/AssetsController.php +++ b/app/Http/Controllers/AssetsController.php @@ -1004,17 +1004,18 @@ class AssetsController extends Controller * @since [v1.0] * @return View */ - public function displayFile($assetId = null, $fileId = null) + public function displayFile($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'); @@ -1023,14 +1024,13 @@ class AssetsController extends Controller $file = $log->get_src('audits'); } - $filetype = Helper::checkUploadIsImage($file); if (!file_exists($file)) { return response('File '.$file.' not found on server', 404) ->header('Content-Type', 'text/plain'); } - if ($filetype) { + if ($download != 'true') { if ($contents = file_get_contents($file)) { return Response::make($contents)->header('Content-Type', $filetype); } diff --git a/app/Http/Controllers/LicensesController.php b/app/Http/Controllers/LicensesController.php index 5abd75a923..e4b00a94d8 100755 --- a/app/Http/Controllers/LicensesController.php +++ b/app/Http/Controllers/LicensesController.php @@ -508,7 +508,7 @@ class LicensesController extends Controller foreach (Input::file('licensefile') as $file) { $rules = array( - 'licensefile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar,rtf,xml,lic|max:2000' + 'licensefile' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar,rtf,xml,lic' ); $validator = Validator::make(array('licensefile'=> $file), $rules); @@ -516,8 +516,7 @@ class LicensesController extends Controller return redirect()->back()->with('error', trans('admin/licenses/message.upload.invalidfiles')); } $extension = $file->getClientOriginalExtension(); - $filename = 'license-'.$license->id.'-'.str_random(8); - $filename .= '-'.str_slug($file->getClientOriginalName()).'.'.$extension; + $filename = 'license-'.$license->id.'-'.str_random(8).'-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension; $upload_success = $file->move($destinationPath, $filename); //Log the upload to the log @@ -583,7 +582,7 @@ class LicensesController extends Controller * @param int $fileId * @return \Symfony\Component\HttpFoundation\BinaryFileResponse */ - public function displayFile($licenseId = null, $fileId = null) + public function displayFile($licenseId = null, $fileId = null, $download = true) { $license = License::find($licenseId); @@ -593,8 +592,31 @@ class LicensesController extends Controller $this->authorize('view', $license); $log = Actionlog::find($fileId); $file = $log->get_src('licenses'); + + + if ($file =='') { + return response('File not found on server', 404) + ->header('Content-Type', 'text/plain'); + } + + $mimetype = \File::mimeType($file); + + + 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', $mimetype); + } + return JsonResponse::create(["error" => "Failed validation: "], 500); + } return Response::download($file); } + + return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.does_not_exist', compact('id'))); } diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 7b54ae0fa6..a76729b380 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -81,7 +81,7 @@ class ReportsController extends Controller return $response; } - + /** * Show depreciation report for assets. * diff --git a/app/Http/Requests/AssetFileRequest.php b/app/Http/Requests/AssetFileRequest.php index 2c28b03147..a32a5475df 100644 --- a/app/Http/Requests/AssetFileRequest.php +++ b/app/Http/Requests/AssetFileRequest.php @@ -23,8 +23,9 @@ class AssetFileRequest extends Request */ public function rules() { + $max_file_size = \App\Helpers\Helper::file_upload_max_size(); return [ - 'file.*' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:2000' + 'file.*' => 'required|mimes:png,gif,jpg,jpeg,doc,docx,pdf,txt,zip,rar|max:'.$max_file_size, ]; } diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php index 3985507cc3..53ece05583 100644 --- a/app/Http/Transformers/ActionlogsTransformer.php +++ b/app/Http/Transformers/ActionlogsTransformer.php @@ -22,11 +22,20 @@ class ActionlogsTransformer public function transformActionlog (Actionlog $actionlog, $settings = null) { + $icon = $actionlog->present()->icon(); + if ($actionlog->filename!='') { + $icon = e(\App\Helpers\Helper::filetype_icon($actionlog->filename)); + } $array = [ 'id' => (int) $actionlog->id, - 'icon' => $actionlog->present()->icon(), - 'image' => (method_exists($actionlog->item, 'getImageUrl')) ? $actionlog->item->getImageUrl() : null, - 'file' => ($actionlog->filename!='') ? route('show/assetfile', ['assetId' => $actionlog->item->id, 'fileId' => $actionlog->id]) : null, + 'icon' => $icon, + 'file' => ($actionlog->filename!='') ? + [ + 'url' => route('show/assetfile', ['assetId' => $actionlog->item->id, 'fileId' => $actionlog->id]), + 'filename' => $actionlog->filename, + 'inlineable' => (bool) \App\Helpers\Helper::show_file_inline($actionlog->filename), + ] : null, + 'item' => ($actionlog->item) ? [ 'id' => (int) $actionlog->item->id, 'name' => e($actionlog->item->getDisplayNameAttribute()), @@ -60,12 +69,11 @@ class ActionlogsTransformer ]; - - return $array; } + public function transformCheckedoutActionlog (Collection $accessories_users, $total) { diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 4c126bb387..e8e823a488 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -133,8 +133,11 @@ class Actionlog extends SnipeModel **/ public function get_src($type = 'assets', $fieldname = 'filename') { - $file = config('app.private_uploads') . '/' . $type . '/' . $this->{$fieldname}; - return $file; + if ($this->filename!='') { + $file = config('app.private_uploads') . '/' . $type . '/' . $this->{$fieldname}; + return $file; + } + return false; } diff --git a/resources/lang/en/admin/hardware/general.php b/resources/lang/en/admin/hardware/general.php index 767af412c6..f1fcd4731e 100644 --- a/resources/lang/en/admin/hardware/general.php +++ b/resources/lang/en/admin/hardware/general.php @@ -12,7 +12,6 @@ return array( 'deployable' => 'Deployable', 'deleted' => 'This asset has been deleted. Click here to restore it.', 'edit' => 'Edit Asset', - 'filetype_info' => 'Allowed filetypes are png, gif, jpg, jpeg, doc, docx, pdf, txt, zip, and rar.', 'model_deleted' => 'This Assets model has been deleted. You must restore the model before you can restore the Asset.
Click here to restore the model.', 'requestable' => 'Requestable', 'requested' => 'Requested', diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index b0d8869a3b..87ffacd8da 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -206,6 +206,7 @@ 'unknown_admin' => 'Unknown Admin', 'username_format' => 'Username Format', 'update' => 'Update', + 'upload_filetypes_help' => 'Allowed filetypes are png, gif, jpg, jpeg, doc, docx, pdf, txt, zip, and rar. Max upload size allowed is :size.', 'uploaded' => 'Uploaded', 'user' => 'User', 'accepted' => 'accepted', diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index 489707968e..72b624e94b 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -8,6 +8,7 @@ {{-- Right header --}} @section('header_right') + @can('manage', \App\Models\Asset::class)
-

{{ trans('admin/hardware/general.filetype_info') }}

+

{{ trans('general.upload_filetypes_help', ['size' => \App\Helpers\Helper::file_upload_max_size_readable()]) }}


@@ -793,20 +792,38 @@ @endcan
- +
- - - - - + + + + + + @if ($asset->uploads->count() > 0) @foreach ($asset->uploads as $file) + diff --git a/resources/views/licenses/view.blade.php b/resources/views/licenses/view.blade.php index 43156bbe36..3de2f6a6da 100755 --- a/resources/views/licenses/view.blade.php +++ b/resources/views/licenses/view.blade.php @@ -285,9 +285,10 @@ }'> + - + @@ -296,7 +297,11 @@ @if ($license->uploads->count()> 0) @foreach ($license->uploads as $file) - + +
{{ trans('general.notes') }}{{ trans('general.file_name') }}{{ trans('general.notes') }}{{ trans('general.image') }}{{ trans('general.file_name') }}{{ trans('general.download') }}{{ trans('general.actions') }}
@if ($file->note) {{ $file->note }} @@ -822,12 +839,12 @@ @if ($file->filename) - {{ trans('general.download') }} + @endif @can('update', \App\Models\Asset::class) - + @endcan
{{ trans('general.file_name') }} {{ trans('general.notes') }}{{ trans('general.created_at') }}{{ trans('general.created_at') }} Download Delete
{{ $file->filename }} + {{ $file->filename }} + + @if ($file->note) {{ $file->note }} @@ -304,11 +309,10 @@ {{ $file->created_at }} - @if ($file->filename) - - Download - + @if ( \App\Helpers\Helper::checkUploadIsImage($file->get_src('licenses'))) + + @endif @endif @@ -379,8 +383,9 @@ 'method' => 'POST', 'route' => ['upload/license', $license->id], 'files' => true, 'class' => 'form-horizontal' ]) }} +