Merge remote-tracking branch 'origin/develop'

This commit is contained in:
snipe 2025-04-21 14:59:26 +01:00
commit 1acc452cfe
7 changed files with 234 additions and 42 deletions

View file

@ -0,0 +1,112 @@
<?php
namespace App\Http\Controllers;
use App\Helpers\StorageHelper;
use App\Http\Requests\UploadFileRequest;
use App\Models\Actionlog;
use App\Models\Location;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use \Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class LocationsFilesController extends Controller
{
/**
* Upload a file to the server.
*
* @param UploadFileRequest $request
* @param int $modelId
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*@since [v1.0]
* @author [A. Gianotto] [<snipe@snipe.net>]
*/
public function store(UploadFileRequest $request, Location $location) : RedirectResponse
{
$this->authorize('update', $location);
if ($request->hasFile('file')) {
if (! Storage::exists('private_uploads/locations')) {
Storage::makeDirectory('private_uploads/locations', 775);
}
foreach ($request->file('file') as $file) {
$file_name = $request->handleFile('private_uploads/locations/','model-'.$location->id,$file);
$location->logUpload($file_name, $request->get('notes'));
}
return redirect()->back()->withFragment('files')->with('success', trans('general.file_upload_success'));
}
return redirect()->back()->withFragment('files')->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]
*/
public function show(Location $location, $fileId = null) : StreamedResponse | Response | RedirectResponse | BinaryFileResponse
{
$this->authorize('view', $location);
if (! $log = Actionlog::find($fileId)) {
return redirect()->back()->withFragment('files')->with('error', 'No matching file record');
}
$file = 'private_uploads/locations/'.$log->filename;
if (! Storage::exists($file)) {
return redirect()->back()->withFragment('files')->with('error', 'No matching file on server');
}
if (request('inline') == 'true') {
$headers = [
'Content-Disposition' => 'inline',
];
return Storage::download($file, $log->filename, $headers);
}
return StorageHelper::downloader($file);
}
/**
* Delete the associated file
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @param int $modelId
* @param int $fileId
* @since [v1.0]
*/
public function destroy(Location $location, $fileId = null) : RedirectResponse
{
$rel_path = 'private_uploads/locations';
$this->authorize('update', $location);
$log = Actionlog::find($fileId);
if ($log) {
// This should be moved to purge
// if (Storage::exists($rel_path.'/'.$log->filename)) {
// Storage::delete($rel_path.'/'.$log->filename);
// }
$log->delete();
return redirect()->back()->withFragment('files')->with('success', trans('admin/hardware/message.deletefile.success'));
}
return redirect()->back()->withFragment('files')->with('success', trans('admin/hardware/message.deletefile.success'));
}
}

View file

@ -20,6 +20,7 @@ class Location extends SnipeModel
{
use HasFactory;
use CompanyableTrait;
use Loggable;
protected $presenter = \App\Presenters\LocationPresenter::class;
use Presentable;
@ -288,6 +289,23 @@ class Location extends SnipeModel
return $this->attributes['ldap_ou'] = empty($ldap_ou) ? null : $ldap_ou;
}
/**
* Get uploads for this location
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function uploads()
{
return $this->hasMany('\App\Models\Actionlog', 'item_id')
->where('item_type', '=', Location::class)
->where('action_type', '=', 'uploaded')
->whereNotNull('filename')
->orderBy('created_at', 'desc');
}
/**
* Query builder scope to order on parent
*

View file

@ -45,6 +45,7 @@ class RouteServiceProvider extends ServiceProvider
require base_path('routes/web/models.php');
require base_path('routes/web/accessories.php');
require base_path('routes/web/licenses.php');
require base_path('routes/web/locations.php');
require base_path('routes/web/consumables.php');
require base_path('routes/web/fields.php');
require base_path('routes/web/components.php');

View file

@ -144,7 +144,22 @@
</li>
@endif
@endcan
@if ($location->uploads->count() > 0 )
<li>
<a href="#files" data-toggle="tab">
<span class="hidden-lg hidden-md">
<i class="fas fa-barcode fa-2x"></i>
</span>
<span class="hidden-xs hidden-sm">
{{ trans('general.files') }}
{!! ($location->uploads->count() > 0 ) ? '<badge class="badge badge-secondary">'.number_format($location->uploads->count()).'</badge>' : '' !!}
</span>
</a>
</li>
@endif
<li>
<a href="#history" data-toggle="tab" data-toggle="tab" data-tooltip="true" title="{{ trans('general.history') }}">
<i class="fa-solid fa-clock-rotate-left" style="font-size: 17px" aria-hidden="true"></i>
@ -153,6 +168,15 @@
</span>
</a>
</li>
@can('update', $location)
<li class="pull-right">
<a href="#" data-toggle="modal" data-target="#uploadFileModal">
<x-icon type="paperclip" />
{{ trans('button.upload') }}
</a>
</li>
@endcan
</ul>
@ -375,6 +399,22 @@
</table>
</div><!-- /.tab-pane -->
<div class="tab-pane fade" id="files">
<div class="row">
<div class="col-md-12">
<x-filestable
filepath="private_uploads/locations/"
showfile_routename="show/locationsfile"
deletefile_routename="delete/locationsfile"
:object="$location" />
</div> <!-- /.col-md-12 -->
</div> <!-- /.row -->
</div>
<div class="tab-pane" id="history">
<h2 class="box-title">{{ trans('general.history') }}</h2>
<!-- checked out assets table -->
@ -550,6 +590,10 @@
@section('moar_scripts')
@can('update', Location::class)
@include ('modals.upload-file', ['item_type' => 'locations', 'item_id' => $location->id])
@endcan
<script>
$('#dataConfirmModal').on('show.bs.modal', function (event) {
var content = $(event.relatedTarget).data('content');

View file

@ -52,47 +52,6 @@ Route::group(['middleware' => 'auth'], function () {
[LabelsController::class, 'show']
)->where('labelName', '.*')->name('labels.show');
/*
* Locations
*/
Route::group(['prefix' => 'locations', 'middleware' => ['auth']], function () {
Route::post(
'bulkdelete',
[LocationsController::class, 'postBulkDelete']
)->name('locations.bulkdelete.show');
Route::post(
'bulkedit',
[LocationsController::class, 'postBulkDeleteStore']
)->name('locations.bulkdelete.store');
Route::post(
'{location}/restore',
[LocationsController::class, 'postRestore']
)->name('locations.restore');
Route::get('{locationId}/clone',
[LocationsController::class, 'getClone']
)->name('clone/location');
Route::get(
'{locationId}/printassigned',
[LocationsController::class, 'print_assigned']
)->name('locations.print_assigned');
Route::get(
'{locationId}/printallassigned',
[LocationsController::class, 'print_all_assigned']
)->name('locations.print_all_assigned');
});
Route::resource('locations', LocationsController::class, [
'parameters' => ['location' => 'location_id'],
]);
/*
* Manufacturers

56
routes/web/locations.php Normal file
View file

@ -0,0 +1,56 @@
<?php
use App\Http\Controllers\LocationsController;
use App\Http\Controllers\LocationsFilesController;
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'locations', 'middleware' => ['auth']], function () {
Route::post('{location}/upload',
[LocationsFilesController::class, 'store']
)->name('upload/locations')->withTrashed();
Route::get('{location}/showfile/{fileId}/{download?}',
[LocationsFilesController::class, 'show']
)->name('show/locationsfile')->withTrashed();
Route::delete('{location}/showfile/{fileId}/delete',
[LocationsFilesController::class, 'destroy']
)->name('delete/locationsfile')->withTrashed();
Route::post(
'bulkdelete',
[LocationsController::class, 'postBulkDelete']
)->name('locations.bulkdelete.show');
Route::post(
'bulkedit',
[LocationsController::class, 'postBulkDeleteStore']
)->name('locations.bulkdelete.store');
Route::post(
'{location}/restore',
[LocationsController::class, 'postRestore']
)->name('locations.restore');
Route::get('{locationId}/clone',
[LocationsController::class, 'getClone']
)->name('clone/location');
Route::get(
'{locationId}/printassigned',
[LocationsController::class, 'print_assigned']
)->name('locations.print_assigned');
Route::get(
'{locationId}/printallassigned',
[LocationsController::class, 'print_all_assigned']
)->name('locations.print_all_assigned');
});
Route::resource('locations', LocationsController::class, [
'middleware' => ['auth'],
])->withTrashed();

View file

@ -0,0 +1,2 @@
*
!.gitignore