Make folders selectable and downloadable with item count and sharing status.
Deploy Ladill Transfer / deploy (push) Successful in 35s
Deploy Ladill Transfer / deploy (push) Successful in 35s
Folder rows match the file table layout, support zip download, and participate in bulk selection, share, and copy-link actions. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -63,6 +63,7 @@ class FilesController extends Controller
|
||||
->where('user_id', $account->id)
|
||||
->where('is_folder', true)
|
||||
->where('status', '!=', Transfer::STATUS_DELETED)
|
||||
->with('qrCode')
|
||||
->withCount('files')
|
||||
->when($search !== '', fn ($q) => $q->where('title', 'like', '%'.$search.'%'))
|
||||
->orderBy('title')
|
||||
@@ -111,12 +112,13 @@ class FilesController extends Controller
|
||||
public function bulkDownload(Request $request): StreamedResponse|RedirectResponse
|
||||
{
|
||||
$files = $this->authorizedFiles($request->input('files', []));
|
||||
$folders = $this->authorizedFolders($request->input('folders', []));
|
||||
|
||||
if ($files->isEmpty()) {
|
||||
return back()->with('error', 'Select at least one file to download.');
|
||||
if ($files->isEmpty() && $folders->isEmpty()) {
|
||||
return back()->with('error', 'Select at least one file or folder to download.');
|
||||
}
|
||||
|
||||
if ($files->count() === 1) {
|
||||
if ($files->count() === 1 && $folders->isEmpty()) {
|
||||
$file = $files->first();
|
||||
abort_unless($file->existsOnDisk(), 404);
|
||||
|
||||
@@ -127,38 +129,39 @@ class FilesController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
$filename = 'ladill-files-'.now()->format('Y-m-d-His').'.zip';
|
||||
if ($folders->count() === 1 && $files->isEmpty()) {
|
||||
$folder = $folders->first();
|
||||
|
||||
return response()->streamDownload(function () use ($files) {
|
||||
$zip = new ZipArchive;
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'ladill_zip_');
|
||||
if ($tmp === false || $zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||
throw new RuntimeException('Could not prepare download archive.');
|
||||
return $this->streamFolderZip($folder, $folder->title.'.zip');
|
||||
}
|
||||
|
||||
$entries = $this->zipEntriesForFiles($files);
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
$prefix = $folders->count() > 1 ? $folder->title.'/' : '';
|
||||
foreach ($folder->files as $file) {
|
||||
$entries[] = [
|
||||
'zipPath' => $prefix.$file->original_name,
|
||||
'file' => $file,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$usedNames = [];
|
||||
foreach ($files as $file) {
|
||||
if (! $file->existsOnDisk()) {
|
||||
continue;
|
||||
}
|
||||
return $this->streamZipEntries(
|
||||
$entries,
|
||||
'ladill-files-'.now()->format('Y-m-d-His').'.zip',
|
||||
);
|
||||
}
|
||||
|
||||
$name = $file->original_name;
|
||||
if (isset($usedNames[$name])) {
|
||||
$usedNames[$name]++;
|
||||
$ext = pathinfo($name, PATHINFO_EXTENSION);
|
||||
$base = pathinfo($name, PATHINFO_FILENAME);
|
||||
$name = $base.' ('.$usedNames[$name].')'.($ext !== '' ? '.'.$ext : '');
|
||||
} else {
|
||||
$usedNames[$name] = 0;
|
||||
}
|
||||
public function downloadFolder(Transfer $folder): StreamedResponse|RedirectResponse
|
||||
{
|
||||
$this->authorizeFolder($folder);
|
||||
|
||||
$zip->addFromString($name, Storage::disk($file->disk)->get($file->path));
|
||||
}
|
||||
if ($folder->files()->count() === 0) {
|
||||
return back()->with('error', 'This folder is empty.');
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
readfile($tmp);
|
||||
@unlink($tmp);
|
||||
}, $filename, ['Content-Type' => 'application/zip']);
|
||||
return $this->streamFolderZip($folder, $folder->title.'.zip');
|
||||
}
|
||||
|
||||
public function destroy(TransferFile $file): RedirectResponse
|
||||
@@ -209,13 +212,15 @@ class FilesController extends Controller
|
||||
public function share(Request $request): JsonResponse
|
||||
{
|
||||
$files = $this->authorizedFiles($request->input('files', []));
|
||||
$folders = $this->authorizedFolders($request->input('folders', []));
|
||||
|
||||
if ($files->isEmpty()) {
|
||||
return response()->json(['message' => 'Select at least one file to share.'], 422);
|
||||
if ($files->isEmpty() && $folders->isEmpty()) {
|
||||
return response()->json(['message' => 'Select at least one file or folder to share.'], 422);
|
||||
}
|
||||
|
||||
$links = $files
|
||||
->map(fn (TransferFile $file) => $file->transfer->qrCode?->publicUrl())
|
||||
->merge($folders->map(fn (Transfer $folder) => $folder->qrCode?->publicUrl()))
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
@@ -440,4 +445,100 @@ class FilesController extends Controller
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
private function authorizeFolder(Transfer $folder): void
|
||||
{
|
||||
abort_unless(
|
||||
$folder->user_id === ladill_account()->id
|
||||
&& $folder->is_folder
|
||||
&& $folder->status !== Transfer::STATUS_DELETED,
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<int|string>|null $ids */
|
||||
private function authorizedFolders(?array $ids): Collection
|
||||
{
|
||||
$ids = collect($ids ?? [])
|
||||
->filter(fn ($id) => is_numeric($id))
|
||||
->map(fn ($id) => (int) $id)
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
if ($ids === []) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return Transfer::query()
|
||||
->whereIn('id', $ids)
|
||||
->where('user_id', ladill_account()->id)
|
||||
->where('is_folder', true)
|
||||
->where('status', '!=', Transfer::STATUS_DELETED)
|
||||
->with(['files', 'qrCode'])
|
||||
->get();
|
||||
}
|
||||
|
||||
private function streamFolderZip(Transfer $folder, string $filename): StreamedResponse
|
||||
{
|
||||
$folder->loadMissing('files');
|
||||
|
||||
return $this->streamZipEntries(
|
||||
$this->zipEntriesForFiles($folder->files),
|
||||
$filename,
|
||||
);
|
||||
}
|
||||
|
||||
/** @param Collection<int, TransferFile>|iterable<int, TransferFile> $files
|
||||
* @return list<array{zipPath: string, file: TransferFile}>
|
||||
*/
|
||||
private function zipEntriesForFiles(iterable $files): array
|
||||
{
|
||||
$entries = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$entries[] = [
|
||||
'zipPath' => $file->original_name,
|
||||
'file' => $file,
|
||||
];
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
/** @param list<array{zipPath: string, file: TransferFile}> $entries */
|
||||
private function streamZipEntries(array $entries, string $filename): StreamedResponse
|
||||
{
|
||||
return response()->streamDownload(function () use ($entries) {
|
||||
$zip = new ZipArchive;
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'ladill_zip_');
|
||||
if ($tmp === false || $zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
|
||||
throw new RuntimeException('Could not prepare download archive.');
|
||||
}
|
||||
|
||||
$usedNames = [];
|
||||
foreach ($entries as $entry) {
|
||||
$file = $entry['file'];
|
||||
if (! $file->existsOnDisk()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $entry['zipPath'];
|
||||
if (isset($usedNames[$name])) {
|
||||
$usedNames[$name]++;
|
||||
$ext = pathinfo($name, PATHINFO_EXTENSION);
|
||||
$base = pathinfo($name, PATHINFO_FILENAME);
|
||||
$name = $base.' ('.$usedNames[$name].')'.($ext !== '' ? '.'.$ext : '');
|
||||
} else {
|
||||
$usedNames[$name] = 0;
|
||||
}
|
||||
|
||||
$zip->addFromString($name, Storage::disk($file->disk)->get($file->path));
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
readfile($tmp);
|
||||
@unlink($tmp);
|
||||
}, $filename, ['Content-Type' => 'application/zip']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user