Zipping on the fly

This commit is contained in:
Peter Papp
2021-07-28 18:16:20 +02:00
parent 8810880616
commit 71a1eb8e7c
23 changed files with 866 additions and 718 deletions
+22 -48
View File
@@ -1,70 +1,44 @@
<?php
namespace Domain\Zip\Actions;
use Domain\Zip\Models\Zip;
use Illuminate\Support\Str;
use Domain\Sharing\Models\Share;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\FileNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use STS\ZipStream\ZipStreamFacade as Zip;
class ZipFilesAction
{
/**
* Zip selected files, store it in /zip folder and retrieve zip record
* Zip selected files, and download it as response stream
*/
public function __invoke(
File | Collection $files,
?Share $shared = null,
): Zip {
// Local storage instance
$disk_local = Storage::disk('local');
// Move file to local storage from external storage service
if (! is_storage_driver('local')) {
$files->each(function ($file) use ($disk_local) {
try {
$disk_local->put("temp/$file->basename", Storage::get("files/$file->user_id/$file->basename"));
} catch (FileNotFoundException $e) {
throw new HttpException(404, 'File not found');
}
});
}
// Get zip path
$zip_name = Str::random() . '.zip';
): ZipStream {
// Create zip
$zipper = new \Madnest\Madzipper\Madzipper;
$zip = $zipper->make($disk_local->path("zip/$zip_name"));
$zip = Zip::create('files.zip');
// Add files to zip
$files->each(function ($file) use ($zip, $disk_local) {
$file_path = is_storage_driver('local')
? $disk_local->path("files/$file->user_id/$file->basename")
: $disk_local->path("temp/$file->basename");
$files->map(function ($file) use ($zip) {
// get file path
$filePath = "files/$file->user_id/$file->basename";
$zip->addString("$file->name.$file->mimetype", File::get($file_path));
// Add file into zip
if (Storage::exists($filePath)) {
// local disk
if (is_storage_driver('local')) {
$zip->add(storage_path("app/files/$file->user_id/$file->basename"), $file->name);
}
// s3 client
if (is_storage_driver('s3')) {
$bucketName = config('filesystems.disks.s3.bucket');
$zip->add("s3://$bucketName/files/$file->user_id/$file->basename", $file->name);
}
}
});
// Close zip
//$zip->close();
// Delete temporary files
if (! is_storage_driver('local')) {
$files->each(function ($file) use ($disk_local) {
$disk_local->delete("temp/$file->basename");
});
}
// Store zip record
return Zip::create([
'user_id' => $shared->user_id ?? Auth::id(),
'shared_token' => $shared->token ?? null,
'basename' => $zip_name,
]);
return $zip;
}
}
+27 -50
View File
@@ -1,15 +1,12 @@
<?php
namespace Domain\Zip\Actions;
use Domain\Zip\Models\Zip;
use Illuminate\Support\Str;
use STS\ZipStream\ZipStream;
use Domain\Sharing\Models\Share;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\FileNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use STS\ZipStream\ZipStreamFacade as Zip;
class ZipFolderAction
{
@@ -19,63 +16,43 @@ class ZipFolderAction
public function __invoke(
$id,
?Share $shared = null
): Zip {
): ZipStream {
$user_id = Auth::id() ?? $shared->user_id;
// Get folder
$requested_folder = Folder::with(['folders.files', 'files'])
->where('id', $id)
->where('user_id', Auth::id() ?? $shared->user_id)
->where('user_id', $user_id)
->with('folders')
->first();
$files = get_files_for_zip($requested_folder, collect([]));
// Local storage instance
$disk_local = Storage::disk('local');
// Create zip
$zip = Zip::create('files.zip');
// Move file to local storage from external storage service
if (! is_storage_driver('local')) {
foreach ($files as $file) {
try {
$disk_local->put("temp/{$file['basename']}", Storage::get("files/$requested_folder->user_id/{$file['basename']}"));
} catch (FileNotFoundException $e) {
throw new HttpException(404, 'File not found');
foreach ($files as $file) {
// get file path
$filePath = "files/$user_id/{$file['basename']}";
// Add file into zip
if (Storage::exists($filePath)) {
$zipDestination = "{$file['folder_path']}/{$file['name']}";
// local disk
if (is_storage_driver('local')) {
$zip->add(storage_path("app/files/$user_id/{$file['basename']}"), $zipDestination);
}
// s3 client
if (is_storage_driver('s3')) {
$bucketName = config('filesystems.disks.s3.bucket');
$zip->add("s3://$bucketName/files/$user_id/{$file['basename']}", $zipDestination);
}
}
}
// Get zip path
$zip_name = Str::random(16) . '-' . Str::slug($requested_folder->name) . '.zip';
// Create zip
$zipper = new \Madnest\Madzipper\Madzipper;
$zip = $zipper->make($disk_local->path("zip/$zip_name"));
// Add files to zip
foreach ($files as $file) {
$file_path = is_storage_driver('local')
? $disk_local->path("files/$requested_folder->user_id/{$file['basename']}")
: $disk_local->path("temp/{$file['basename']}");
$zip
->folder($file['folder_path'])
->addString("{$file['name']}.{$file['mimetype']}", File::get($file_path));
}
// Close zip
//$zip->close();
// Delete temporary files
if (! is_storage_driver('local')) {
foreach ($files as $file) {
$disk_local->delete('temp/' . $file['basename']);
}
}
// Store zip record
return Zip::create([
'user_id' => $shared->user_id ?? Auth::id(),
'shared_token' => $shared->token ?? null,
'basename' => $zip_name,
]);
return $zip;
}
}