mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-20 17:02:16 +00:00
user zipping
This commit is contained in:
86
src/Domain/Zip/Actions/ZipAction.php
Normal file
86
src/Domain/Zip/Actions/ZipAction.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
use ZipStream\ZipStream;
|
||||
|
||||
class ZipAction
|
||||
{
|
||||
public function __invoke(
|
||||
Collection $folders,
|
||||
Collection $files,
|
||||
?Share $shared = null
|
||||
): ZipStream {
|
||||
|
||||
// Get user id
|
||||
$user_id = Auth::id() ?? $shared->user_id;
|
||||
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
// Zip Files
|
||||
$files->map(function ($file) use ($zip) {
|
||||
// get file path
|
||||
$filePath = "files/$file->user_id/$file->basename";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $file->name);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $file->name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Zip Folders
|
||||
$folders->map(function ($folder) use ($zip, $user_id) {
|
||||
// Get folder
|
||||
$requested_folder = Folder::with(['folders.files', 'files'])
|
||||
->where('id', $folder->id)
|
||||
->where('user_id', $user_id)
|
||||
->with('folders')
|
||||
->first();
|
||||
|
||||
$folderFiles = get_files_for_zip($requested_folder, collect([]));
|
||||
|
||||
foreach ($folderFiles 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($filePath), $zipDestination);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $zipDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
|
||||
class ZipFilesAction
|
||||
{
|
||||
/**
|
||||
* Zip selected files, and download it as response stream
|
||||
*/
|
||||
public function __invoke(
|
||||
File | Collection $files,
|
||||
?Share $shared = null,
|
||||
): ZipStream {
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
$files->map(function ($file) use ($zip) {
|
||||
// get file path
|
||||
$filePath = "files/$file->user_id/$file->basename";
|
||||
|
||||
// Add file into zip
|
||||
if (Storage::exists($filePath)) {
|
||||
// local disk
|
||||
if (is_storage_driver('local')) {
|
||||
$zip->add(Storage::path($filePath), $file->name);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $file->name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use STS\ZipStream\ZipStreamFacade as Zip;
|
||||
|
||||
class ZipFolderAction
|
||||
{
|
||||
/**
|
||||
* Zip requested folder
|
||||
*/
|
||||
public function __invoke(
|
||||
$id,
|
||||
?Share $shared = null
|
||||
): ZipStream {
|
||||
$user_id = Auth::id() ?? $shared->user_id;
|
||||
|
||||
// Get folder
|
||||
$requested_folder = Folder::with(['folders.files', 'files'])
|
||||
->where('id', $id)
|
||||
->where('user_id', $user_id)
|
||||
->with('folders')
|
||||
->first();
|
||||
|
||||
$files = get_files_for_zip($requested_folder, collect([]));
|
||||
|
||||
// Create zip
|
||||
$zip = Zip::create('files.zip');
|
||||
|
||||
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($filePath), $zipDestination);
|
||||
}
|
||||
|
||||
// s3 client
|
||||
if (is_storage_driver('s3')) {
|
||||
$bucketName = config('filesystems.disks.s3.bucket');
|
||||
|
||||
$zip->add("s3://$bucketName/$filePath", $zipDestination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user