mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-28 11:00:39 +00:00
controller refactoring part 25
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Domain\Zip\Models\Zip;
|
||||
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;
|
||||
|
||||
class ZipFilesAction
|
||||
{
|
||||
/**
|
||||
* Zip selected files, store it in /zip folder and retrieve zip record
|
||||
*/
|
||||
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';
|
||||
|
||||
// Create zip
|
||||
$zipper = new \Madnest\Madzipper\Madzipper;
|
||||
$zip = $zipper->make($disk_local->path("zip/$zip_name"));
|
||||
|
||||
// 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");
|
||||
|
||||
$zip->addString("$file->name.$file->mimetype", File::get($file_path));
|
||||
});
|
||||
|
||||
// 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Actions;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Domain\Zip\Models\Zip;
|
||||
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;
|
||||
|
||||
class ZipFolderAction
|
||||
{
|
||||
/**
|
||||
* Zip requested folder
|
||||
*/
|
||||
public function __invoke(
|
||||
$id,
|
||||
?Share $shared = null
|
||||
): Zip {
|
||||
// Get folder
|
||||
$requested_folder = Folder::with(['folders.files', 'files'])
|
||||
->where('id', $id)
|
||||
->where('user_id', Auth::id() ?? $shared->user_id)
|
||||
->with('folders')
|
||||
->first();
|
||||
|
||||
$files = get_files_for_zip($requested_folder, collect([]));
|
||||
|
||||
// Local storage instance
|
||||
$disk_local = Storage::disk('local');
|
||||
|
||||
// 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Domain\Zip\Models\Zip;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class GetZipController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get generated zip for user
|
||||
*/
|
||||
public function __invoke(
|
||||
string $id,
|
||||
): StreamedResponse {
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$zip = Zip::whereId($id)
|
||||
->where('user_id', Auth::id())
|
||||
->firstOrFail();
|
||||
|
||||
// Store user download size
|
||||
($this->recordDownload)(
|
||||
file_size: $disk->size("zip/$zip->basename"),
|
||||
user_id: $zip->user_id,
|
||||
);
|
||||
|
||||
return $disk->download("zip/$zip->basename", $zip->basename, [
|
||||
'Content-Type' => 'application/zip',
|
||||
'Content-Length' => $disk->size("zip/$zip->basename"),
|
||||
'Accept-Ranges' => 'bytes',
|
||||
'Content-Range' => 'bytes 0-600/' . $disk->size("zip/$zip->basename"),
|
||||
'Content-Disposition' => "attachment; filename=$zip->basename",
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Domain\Zip\Models\Zip;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class VisitorGetZipController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private RecordDownloadAction $recordDownload,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get generated zip for visitor
|
||||
*/
|
||||
public function __invoke(
|
||||
$id,
|
||||
$token,
|
||||
): StreamedResponse {
|
||||
$disk = Storage::disk('local');
|
||||
|
||||
$zip = Zip::where('id', $id)
|
||||
->where('shared_token', $token)
|
||||
->first();
|
||||
|
||||
// Store user download size
|
||||
($this->recordDownload)(
|
||||
file_size: $disk->size("zip/$zip->basename"),
|
||||
user_id: $zip->user_id,
|
||||
);
|
||||
|
||||
return $disk->download("zip/$zip->basename", $zip->basename, [
|
||||
'Content-Type' => 'application/zip',
|
||||
'Content-Length' => $disk->size("zip/$zip->basename"),
|
||||
'Accept-Ranges' => 'bytes',
|
||||
'Content-Range' => 'bytes 0-600/' . $disk->size("zip/$zip->basename"),
|
||||
'Content-Disposition' => 'attachment; filename=' . $zip->basename,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
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\Sharing\Actions\ProtectShareRecordAction;
|
||||
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
||||
|
||||
/**
|
||||
* Guest download multiple files via zip
|
||||
*/
|
||||
class VisitorZipFilesController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ProtectShareRecordAction $protectShareRecord,
|
||||
private VerifyAccessToItemAction $verifyAccessToItem,
|
||||
private ZipFilesAction $zipFiles,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
Share $shared,
|
||||
): Response {
|
||||
// Check ability to access protected share record
|
||||
($this->protectShareRecord)($shared);
|
||||
|
||||
$file_parent_folders = File::whereUserId($shared->user_id)
|
||||
->whereIn('id', $request->items)
|
||||
->get()
|
||||
->pluck('folder_id')
|
||||
->toArray();
|
||||
|
||||
// Check access to requested directory
|
||||
($this->verifyAccessToItem)($file_parent_folders, $shared);
|
||||
|
||||
// Get requested files
|
||||
$files = File::whereUserId($shared->user_id)
|
||||
->whereIn('id', $request->items)
|
||||
->get();
|
||||
|
||||
$zip = ($this->zipFiles)($files, $shared);
|
||||
|
||||
// Get file
|
||||
return response([
|
||||
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Domain\Sharing\Models\Share;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Domain\Zip\Actions\ZipFolderAction;
|
||||
use Domain\Sharing\Actions\ProtectShareRecordAction;
|
||||
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
||||
|
||||
/**
|
||||
* Guest download folder via zip
|
||||
*/
|
||||
class VisitorZipFolderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ProtectShareRecordAction $protectShareRecord,
|
||||
private VerifyAccessToItemAction $verifyAccessToItem,
|
||||
private ZipFolderAction $zipFolder,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
string $id,
|
||||
Share $shared,
|
||||
): Response {
|
||||
// Check ability to access protected share record
|
||||
($this->protectShareRecord)($shared);
|
||||
|
||||
// Check access to requested folder
|
||||
($this->verifyAccessToItem)($id, $shared);
|
||||
|
||||
// Get folder
|
||||
$folder = Folder::whereUserId($shared->user_id)
|
||||
->where('id', $id);
|
||||
|
||||
if (! $folder->exists()) {
|
||||
abort(404, 'Requested folder doesn\'t exists.');
|
||||
}
|
||||
|
||||
$zip = ($this->zipFolder)($id, $shared);
|
||||
|
||||
// Get file
|
||||
return response([
|
||||
'url' => url("/zip/{$zip->id}/public/{$shared->token}"),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Domain\Files\Models\File;
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFilesAction;
|
||||
|
||||
class ZipFilesController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFilesAction $zipFiles,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
Request $request,
|
||||
): Response {
|
||||
$files = File::whereUserId(Auth::id())
|
||||
->whereIn('id', $request->input('items'))
|
||||
->get();
|
||||
|
||||
$zip = ($this->zipFiles)($files);
|
||||
|
||||
return response([
|
||||
'url' => route('zip', $zip->id),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Controllers;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Domain\Zip\Actions\ZipFolderAction;
|
||||
|
||||
class ZipFolderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ZipFolderAction $zipFolder,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(
|
||||
string $id,
|
||||
): Response {
|
||||
$folder = Folder::whereUserId(Auth::id())
|
||||
->where('id', $id);
|
||||
|
||||
if (! $folder->exists()) {
|
||||
abort(404, "Requested folder doesn't exists.");
|
||||
}
|
||||
|
||||
$zip = ($this->zipFolder)($id);
|
||||
|
||||
return response([
|
||||
'url' => route('zip', $zip->id),
|
||||
'name' => $zip->basename,
|
||||
], 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace Domain\Zip\Models;
|
||||
|
||||
use App\Users\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Database\Factories\ZipFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
/**
|
||||
* @property mixed basename
|
||||
*/
|
||||
class Zip extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected static function newFactory(): ZipFactory
|
||||
{
|
||||
return ZipFactory::new();
|
||||
}
|
||||
|
||||
public function user(): HasOne
|
||||
{
|
||||
return $this->hasOne(User::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
$model->id = (string) Str::uuid();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user