mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-29 03:10:51 +00:00
Zipping on the fly
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,13 @@ namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Domain\Files\Models\File;
|
||||
use Illuminate\Http\Response;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Domain\Zip\Actions\ZipFilesAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
use Domain\Sharing\Actions\ProtectShareRecordAction;
|
||||
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
||||
use STS\ZipStream\ZipStream;
|
||||
|
||||
/**
|
||||
* Guest download multiple files via zip
|
||||
@@ -18,6 +19,7 @@ class VisitorZipFilesController extends Controller
|
||||
public function __construct(
|
||||
private ProtectShareRecordAction $protectShareRecord,
|
||||
private VerifyAccessToItemAction $verifyAccessToItem,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
private ZipFilesAction $zipFiles,
|
||||
) {
|
||||
}
|
||||
@@ -25,7 +27,7 @@ class VisitorZipFilesController extends Controller
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
Share $shared,
|
||||
): Response {
|
||||
): ZipStream {
|
||||
// Check ability to access protected share record
|
||||
($this->protectShareRecord)($shared);
|
||||
|
||||
@@ -46,10 +48,11 @@ class VisitorZipFilesController extends Controller
|
||||
// Create zip
|
||||
$zip = ($this->zipFiles)($files, $shared);
|
||||
|
||||
// Get file
|
||||
return response([
|
||||
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: $shared->user_id,
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Domain\Zip\Actions\ZipFolderAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
use Domain\Sharing\Actions\ProtectShareRecordAction;
|
||||
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
||||
|
||||
@@ -17,6 +18,7 @@ class VisitorZipFolderController extends Controller
|
||||
public function __construct(
|
||||
private ProtectShareRecordAction $protectShareRecord,
|
||||
private VerifyAccessToItemAction $verifyAccessToItem,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
private ZipFolderAction $zipFolder,
|
||||
) {
|
||||
}
|
||||
@@ -24,7 +26,7 @@ class VisitorZipFolderController extends Controller
|
||||
public function __invoke(
|
||||
string $id,
|
||||
Share $shared,
|
||||
): Response {
|
||||
): ZipStream {
|
||||
// Check ability to access protected share record
|
||||
($this->protectShareRecord)($shared);
|
||||
|
||||
@@ -36,16 +38,17 @@ class VisitorZipFolderController extends Controller
|
||||
->where('id', $id);
|
||||
|
||||
if (! $folder->exists()) {
|
||||
abort(404, 'Requested folder doesn\'t exists.');
|
||||
abort(404, "Requested folder doesn't exists.");
|
||||
}
|
||||
|
||||
// Create zip
|
||||
$zip = ($this->zipFolder)($id, $shared);
|
||||
|
||||
// Get file
|
||||
return response([
|
||||
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: $shared->user_id,
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,31 +2,35 @@
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Files\Models\File;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFilesAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
|
||||
class ZipFilesController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFilesAction $zipFiles,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
): Response {
|
||||
): ZipStream {
|
||||
$files = File::whereUserId(Auth::id())
|
||||
->whereIn('id', $request->input('items'))
|
||||
->whereIn('id', explode(',', $request->get('ids')))
|
||||
->get();
|
||||
|
||||
$zip = ($this->zipFiles)($files);
|
||||
|
||||
return response([
|
||||
'url' => route('zip', $zip->id),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: Auth::id(),
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,38 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use STS\ZipStream\ZipStream;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFolderAction;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
|
||||
class ZipFolderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFolderAction $zipFolder,
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
string $id,
|
||||
): Response {
|
||||
): ZipStream {
|
||||
$folder = Folder::whereUserId(Auth::id())
|
||||
->where('id', $id);
|
||||
|
||||
if (! $folder->exists()) {
|
||||
abort(404, "Requested folder doesn't exists.");
|
||||
response("Requested folder doesn't exists.", 404);
|
||||
}
|
||||
|
||||
$zip = ($this->zipFolder)($id);
|
||||
|
||||
return response([
|
||||
'url' => route('zip', $zip->id),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
($this->recordDownload)(
|
||||
file_size: $zip->predictZipSize(),
|
||||
user_id: Auth::id(),
|
||||
);
|
||||
|
||||
return $zip;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user