Skip to content

A free open source IT asset/license management system

Fixed FD-57828 / w742-vf26 - multi-file import leaves unreachable storage objects

snipe6cc6cd44fdef
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php
index 4dd0f689e3..5a2e7c8d79 100644
--- a/app/Http/Controllers/Api/ImportController.php
+++ b/app/Http/Controllers/Api/ImportController.php
@@ -55,7 +55,6 @@ class ImportController extends Controller
// storage/private_uploads/imports, and under s3_private it lands at <bucket>/private_uploads/imports
$diskPath = 'private_uploads/imports';
$results = [];
- $import = new Import;
$detector = new EncodingDetector;
// No file uploaded
@@ -64,6 +63,8 @@ class ImportController extends Controller
}
foreach ($files as $file) {
+ // Fresh model per file.
+ $import = new Import;
// Reject phantoms and fail early if the file is invalid (e.g. exceeds the server upload limit).
// The CSV reader below will reject anything that isn't actually parseable with a more precise error.
if (! $file instanceof UploadedFile || ! $file->isValid()) {
diff --git a/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php b/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php
new file mode 100644
index 0000000000..512a4beac7
--- /dev/null
+++ b/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace Tests\Feature\Importing\Api;
+
+use App\Models\Import;
+use App\Models\User;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * `POST /api/v1/imports` accepts a `files[]` array. Before the fix,
+ * `ImportController::store` instantiated one `new Import` above the
+ * foreach loop and reused the same model instance for every file.
+ * Each iteration overwrote the row's `file_path` and called `save()`,
+ * so after N files the DB held one row referencing the Nth file while
+ * N-1 earlier files sat on disk with no row referencing them. Those
+ * objects could not be removed through `destroy()`, which deletes by
+ * `file_path` on the stored row.
+ */
+class ImportStoreMultiFileRowPerFileTest extends TestCase
+{
+ public function test_multi_file_upload_creates_one_row_per_file_and_leaves_no_orphans(): void
+ {
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $firstCsv = "asset tag,item name\nORPHAN-A,File One\n";
+ $secondCsv = "asset tag,item name\nORPHAN-B,File Two\n";
+
+ $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('cand-14-first.csv', $firstCsv),
+ $this->fakeCsvUpload('cand-14-second.csv', $secondCsv),
+ ],
+ ])
+ ->assertSuccessful();
+
+ $imports = Import::query()->where('created_by', $importer->id)->get();
+ $this->assertCount(2, $imports, 'Each uploaded file must get its own Import row.');
+
+ $storedPaths = $imports->pluck('file_path')->all();
+ $this->assertCount(2, array_unique($storedPaths), 'The two rows must reference distinct storage paths.');
+
+ $storedFiles = Storage::disk()->files('private_uploads/imports');
+ $this->assertCount(2, $storedFiles, 'Exactly two storage objects should exist for two uploads.');
+
+ foreach ($storedPaths as $path) {
+ $this->assertTrue(
+ Storage::disk()->exists('private_uploads/imports/'.$path),
+ "Row's file_path must reference a real object on disk ({$path}).",
+ );
+ }
+
+ $referenced = array_map(fn ($p) => 'private_uploads/imports/'.$p, $storedPaths);
+ $orphans = array_diff($storedFiles, $referenced);
+ $this->assertEmpty(
+ $orphans,
+ 'No storage object may be left without a DB row referencing it. Orphans: '.implode(', ', $orphans),
+ );
+ }
+
+ public function test_single_file_upload_still_creates_one_row(): void
+ {
+ Storage::fake();
+
+ $importer = User::factory()->canImport()->create();
+
+ $this->actingAsForApi($importer)
+ ->postJson(route('api.imports.store'), [
+ 'files' => [
+ $this->fakeCsvUpload('solo.csv', "asset tag,item name\nSOLO-001,Solo\n"),
+ ],
+ ])
+ ->assertSuccessful();
+
+ $this->assertSame(
+ 1,
+ Import::query()->where('created_by', $importer->id)->count(),
+ 'The single-file happy path must remain one row per file.',
+ );
+ }
+
+ private function fakeCsvUpload(string $name, string $contents): UploadedFile
+ {
+ $path = tempnam(sys_get_temp_dir(), 'csv');
+ file_put_contents($path, $contents);
+
+ return new UploadedFile($path, $name, 'text/csv', null, true);
+ }
+}