Skip to content

Commit 88d0c2e

Browse files
committed
improve link
1 parent 74141e5 commit 88d0c2e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

app/Http/Controllers/DocumentController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,18 @@ public function store(Request $request)
166166

167167
$newPath = storage_path('docs/' . $doc->id);
168168

169+
$deduplicated = false;
169170
if ($existingDocument !== null) {
170171
// Reuse existing file - create hard link
171172
$existingPath = storage_path('docs/' . $existingDocument->id);
172173

173174
if (file_exists($existingPath)) {
174-
link($existingPath, $newPath);
175+
if (link($existingPath, $newPath)) {
176+
$deduplicated = true;
177+
} else {
178+
// Fallback: hard-link failed (e.g., different filesystem)
179+
$file->move(storage_path('docs'), (string) $doc->id);
180+
}
175181
} else {
176182
// Fallback: if existing file is missing, store the uploaded file
177183
$file->move(storage_path('docs'), (string) $doc->id);
@@ -186,7 +192,7 @@ public function store(Request $request)
186192
[
187193
'success' => $doc->filename,
188194
'id' => $doc->id,
189-
'deduplicated' => $existingDocument !== null,
195+
'deduplicated' => $deduplicated,
190196
]
191197
);
192198
}

0 commit comments

Comments
 (0)