A free open source IT asset/license management system
Cache files so that S3-backed importer files don’t lag on interactions
snipeb90eba6de7cd
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.phpindex af4b49394c..4992a665dd 100644--- a/app/Livewire/Importer.php+++ b/app/Livewire/Importer.php@@ -354,6 +354,7 @@ class Importer extends Componentpublic function mount(){$this->authorize('import');+ $this->refreshExistingImportFiles();$this->importTypes = ['accessory' => trans('general.accessories'),'asset' => trans('general.assets'),@@ -865,8 +866,11 @@ class Importer extends Componentreturn;}- $path = config('app.private_uploads').'/imports/'.$this->activeFile->file_path;- if (! is_file($path)) {+ // Existence via Storage so the check routes to whichever+ // driver PRIVATE_FILESYSTEM_DISK resolves to, instead of only+ // looking at the local filesystem.+ $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;+ if (! Storage::exists($storedPath)) {$this->message = trans('admin/hardware/message.import.file_missing_on_disk');$this->message_type = 'danger';@@ -1119,13 +1123,19 @@ class Importer extends Componentreturn [];}- $path = config('app.private_uploads').'/imports/'.$this->activeFile->file_path;- if (! is_file($path)) {+ // Reader::createFromStream so League CSV reads through the+ // Storage abstraction. On local this is effectively the same+ // as the old createFromPath. On s3_private the stream pulls+ // bytes directly from S3 via the SDK so we never need the+ // file to touch local disk.+ $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;+ $stream = Storage::readStream($storedPath);+ if ($stream === null) {return [];}try {- $reader = Reader::createFromPath($path);+ $reader = Reader::createFromStream($stream);$reader->setHeaderOffset(0);$rows = [];@@ -1142,6 +1152,10 @@ class Importer extends Componentreturn $rows;} catch (\Throwable $e) {return [];+ } finally {+ if (is_resource($stream)) {+ fclose($stream);+ }}}@@ -1158,13 +1172,15 @@ class Importer extends Componentreturn 0;}- $path = config('app.private_uploads').'/imports/'.$this->activeFile->file_path;- if (! is_file($path)) {+ // Same disk-aware read path as loadPreviewRows.+ $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;+ $stream = Storage::readStream($storedPath);+ if ($stream === null) {return 0;}try {- $reader = Reader::createFromPath($path);+ $reader = Reader::createFromStream($stream);$reader->setHeaderOffset(0);$count = 0;@@ -1177,6 +1193,10 @@ class Importer extends Componentreturn $count;} catch (\Throwable $e) {return 0;+ } finally {+ if (is_resource($stream)) {+ fclose($stream);+ }}}@@ -1234,6 +1254,7 @@ class Importer extends Component$this->message_type = 'success';unset($this->files);+ unset($this->existingImportFiles[$import->file_path]);return;}@@ -1280,6 +1301,10 @@ class Importer extends Component}unset($this->files);+ // New uploads land during this cycle. Repopulate the set so+ // fileMissingOnDisk sees them without waiting for the next+ // mount.+ $this->refreshExistingImportFiles();// Fire-and-forget signal to the JS side to schedule the// clear-highlights timeout. Doing the delay client-side keeps@@ -1399,7 +1424,43 @@ class Importer extends Component*/public function fileMissingOnDisk(Import $import): bool{- return ! is_file(config('app.private_uploads').'/imports/'.$import->file_path);+ // Read against the cached set of file basenames rather than+ // firing an S3 request per render. The set is populated once+ // at mount and refreshed only when this component causes a+ // mutation (uploadSucceeded, destroy, bulkDestroy). Every+ // other re-render (selectAll toggle, individual row check,+ // pagination, wizard step change) reads the cached array+ // without any S3 traffic, which was previously stretching a+ // simple checkbox click into a multi-second round-trip on+ // buckets with a few thousand import files.+ return ! isset($this->existingImportFiles[$import->file_path]);+ }++ /**+ * Basename-keyed set of files currently in+ * private_uploads/imports on the configured private disk.+ * Serialized as component state so subsequent renders don't need+ * to re-list the disk. Callers that mutate the imports dir+ * (upload success + delete paths) invoke refreshExistingImportFiles+ * to keep this in sync.+ *+ * @var array<string, true>+ */+ public array $existingImportFiles = [];++ /**+ * Re-scan the imports dir on the configured private disk and+ * rebuild the basename set. Called from mount() so the initial+ * render has a fresh view, and from uploadSucceeded / destroy /+ * bulkDestroy so post-mutation renders reflect the change.+ */+ public function refreshExistingImportFiles(): void+ {+ $set = [];+ foreach (Storage::files('private_uploads/imports') as $path) {+ $set[basename($path)] = true;+ }+ $this->existingImportFiles = $set;}public function canDeleteFile(Import $import): bool@@ -1476,6 +1537,7 @@ class Importer extends ComponentStorage::delete('private_uploads/imports/'.$import->file_path);$import->delete();+ unset($this->existingImportFiles[$import->file_path]);$deleted++;}diff --git a/resources/views/livewire/importer.blade.php b/resources/views/livewire/importer.blade.phpindex 17483082af..857f88902d 100644--- a/resources/views/livewire/importer.blade.php+++ b/resources/views/livewire/importer.blade.php@@ -93,20 +93,18 @@</div>@else- @if (count($selectedIds) > 0)<div class="row" style="padding-bottom: 10px;"><div class="col-md-12"><button type="button"class="btn btn-danger"data-toggle="modal"data-target="#bulkDeleteImportsModal"- @disabled(config('app.lock_passwords'))>+ @disabled(config('app.lock_passwords') || count($selectedIds) === 0)><i class="fas fa-trash" aria-hidden="true"></i>{{ trans('admin/hardware/message.import.bulk_delete.button', ['count' => count($selectedIds)]) }}</button></div></div>- @endif<table data-id-table="upload-table"data-side-pagination="client"@@ -143,7 +141,7 @@@foreach($this->files as $currentFile)- <tr style="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? 'font-weight: bold' : '' }}">+ <tr wire:key="import-row-{{ $currentFile->id }}" style="{{ ($this->activeFile && ($currentFile->id == $this->activeFile->id)) ? 'font-weight: bold' : '' }}"><td><label class="sr-only" for="import-row-{{ $currentFile->id }}">{{ trans('admin/hardware/message.import.bulk_delete.select_row', ['file' => $currentFile->file_path]) }}@@ -206,8 +204,9 @@@if (((auth()->user()->id == $currentFile->adminuser?->id) || (auth()->user()->isSuperUser())) && ! config('app.lock_passwords'))<a href="#" wire:click.prevent="$set('activeFileId',null)" data-tooltip="true" data-title="{{ trans('general.delete') }}">- <button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})">- <i class="fas fa-trash icon-white" aria-hidden="true"></i>+ <button class="btn btn-sm btn-danger" wire:click="destroy({{ $currentFile->id }})" wire:loading.attr="disabled" wire:target="destroy({{ $currentFile->id }})">+ <i class="fas fa-trash icon-white" aria-hidden="true" wire:loading.remove wire:target="destroy({{ $currentFile->id }})"></i>+ <i class="fas fa-spinner fa-spin icon-white" aria-hidden="true" wire:loading wire:target="destroy({{ $currentFile->id }})"></i><span class="sr-only">{{ trans('general.delete') }}</span></button></a>