mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-30 15:44:41 +00:00
controller refactoring part 18
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Users\Actions;
|
||||||
|
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class CheckStorageCapacityAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Check if user has enough space to upload file
|
||||||
|
*/
|
||||||
|
public function __invoke(
|
||||||
|
string $user_id,
|
||||||
|
int $file_size,
|
||||||
|
string $temp_filename,
|
||||||
|
): void {
|
||||||
|
// Get user storage percentage and get storage_limitation setting
|
||||||
|
$user_storage_used = user_storage_percentage($user_id, $file_size);
|
||||||
|
|
||||||
|
// Check if user can upload
|
||||||
|
if (get_setting('storage_limitation') && $user_storage_used >= 100) {
|
||||||
|
// Delete file
|
||||||
|
Storage::disk('local')
|
||||||
|
->delete("chunks/$temp_filename");
|
||||||
|
|
||||||
|
// Abort uploading
|
||||||
|
// TODO: test pre exceed storage limit
|
||||||
|
abort(423, 'You exceed your storage limit!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Domain\Files\Actions;
|
||||||
|
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Intervention\Image\ImageManagerStatic as Image;
|
||||||
|
|
||||||
|
class CreateImageThumbnailAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create image thumbnail from gif, jpeg, jpg, png, webp or svg
|
||||||
|
*/
|
||||||
|
public function __invoke(
|
||||||
|
string $file_path,
|
||||||
|
string $filename,
|
||||||
|
string $user_id
|
||||||
|
): string|null {
|
||||||
|
|
||||||
|
$availableFormats = ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||||
|
|
||||||
|
// Create thumbnail from image
|
||||||
|
if (in_array(Storage::disk('local')->mimeType($file_path), $availableFormats)) {
|
||||||
|
// Get thumbnail name
|
||||||
|
$thumbnail = "thumbnail-$filename";
|
||||||
|
|
||||||
|
// Create intervention image
|
||||||
|
$image = Image::make(Storage::disk('local')->path($file_path))
|
||||||
|
->orientate();
|
||||||
|
|
||||||
|
// Resize image
|
||||||
|
$image->resize(512, null, function ($constraint) {
|
||||||
|
$constraint->aspectRatio();
|
||||||
|
})->stream();
|
||||||
|
|
||||||
|
// Store thumbnail to disk
|
||||||
|
Storage::put("files/$user_id/$thumbnail", $image);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return thumbnail as svg file
|
||||||
|
if (Storage::disk('local')->mimeType($file_path) === 'image/svg+xml') {
|
||||||
|
$thumbnail = $filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $thumbnail ?? null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Domain\Files\Actions;
|
||||||
|
|
||||||
|
|
||||||
|
use Aws\Exception\MultipartUploadException;
|
||||||
|
use Aws\S3\MultipartUploader;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
|
||||||
|
class MoveFileToExternalStorageAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Move file to external storage if is set
|
||||||
|
*/
|
||||||
|
public function __invoke(
|
||||||
|
string $file, string $user_id
|
||||||
|
): void {
|
||||||
|
|
||||||
|
$disk_local = \Storage::disk('local');
|
||||||
|
|
||||||
|
// Get file size
|
||||||
|
$filesize = $disk_local->size("files/$user_id/$file");
|
||||||
|
|
||||||
|
// If file is bigger than 5.2MB then run multipart upload
|
||||||
|
if ($filesize > 5242880) {
|
||||||
|
// Get driver
|
||||||
|
$driver = \Storage::getDriver();
|
||||||
|
|
||||||
|
// Get adapter
|
||||||
|
$adapter = $driver->getAdapter();
|
||||||
|
|
||||||
|
// Get client
|
||||||
|
$client = $adapter->getClient();
|
||||||
|
|
||||||
|
// Prepare the upload parameters.
|
||||||
|
// TODO: replace local files with temp folder
|
||||||
|
$uploader = new MultipartUploader($client, config('filesystems.disks.local.root') . "/files/$user_id/$file", [
|
||||||
|
'bucket' => $adapter->getBucket(),
|
||||||
|
'key' => "files/$user_id/$file",
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Upload content
|
||||||
|
$uploader->upload();
|
||||||
|
} catch (MultipartUploadException $e) {
|
||||||
|
// Write error log
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
|
||||||
|
// Delete file after error
|
||||||
|
$disk_local->delete("files/$user_id/$file");
|
||||||
|
|
||||||
|
throw new HttpException(409, $e->getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Stream file object to s3
|
||||||
|
// TODO: replace local files with temp folder
|
||||||
|
Storage::putFileAs("files/$user_id", config('filesystems.disks.local.root') . "/files/$user_id/$file", $file, 'private');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete file after upload
|
||||||
|
$disk_local->delete("files/$user_id/$file");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Domain\Files\Actions;
|
namespace Domain\Files\Actions;
|
||||||
|
|
||||||
|
use App\Users\Actions\CheckStorageCapacityAction;
|
||||||
use Domain\Sharing\Models\Share;
|
use Domain\Sharing\Models\Share;
|
||||||
use Support\Services\HelperService;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
@@ -13,10 +13,11 @@ use Domain\Traffic\Actions\RecordUploadAction;
|
|||||||
class UploadFileAction
|
class UploadFileAction
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public HelperService $helper,
|
|
||||||
public RecordUploadAction $recordUpload,
|
public RecordUploadAction $recordUpload,
|
||||||
) {
|
public CheckStorageCapacityAction $checkStorageCapacity,
|
||||||
}
|
public CreateImageThumbnailAction $createImageThumbnail,
|
||||||
|
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
|
||||||
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Upload new file
|
* Upload new file
|
||||||
@@ -64,17 +65,17 @@ class UploadFileAction
|
|||||||
$file_mimetype = $disk_local->mimeType("chunks/$temp_filename");
|
$file_mimetype = $disk_local->mimeType("chunks/$temp_filename");
|
||||||
|
|
||||||
// Check if user has enough space to upload file
|
// Check if user has enough space to upload file
|
||||||
$this->helper->check_user_storage_capacity($user_id, $file_size, $temp_filename);
|
($this->checkStorageCapacity)($user_id, $file_size, $temp_filename);
|
||||||
|
|
||||||
// Create thumbnail
|
// Create thumbnail
|
||||||
$thumbnail = $this->helper->create_image_thumbnail("chunks/$temp_filename", $disk_file_name, $user_id);
|
$thumbnail = ($this->createImageThumbnail)("chunks/$temp_filename", $disk_file_name, $user_id);
|
||||||
|
|
||||||
// Move finished file from chunk to file-manager directory
|
// Move finished file from chunk to file-manager directory
|
||||||
$disk_local->move("chunks/$temp_filename", "files/$user_id/$disk_file_name");
|
$disk_local->move("chunks/$temp_filename", "files/$user_id/$disk_file_name");
|
||||||
|
|
||||||
// Move files to external storage
|
// Move files to external storage
|
||||||
if (! is_storage_driver(['local'])) {
|
if (! is_storage_driver(['local'])) {
|
||||||
$this->helper->move_file_to_external_storage($disk_file_name, $user_id);
|
($this->moveFileToExternalStorage)($disk_file_name, $user_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store user upload size
|
// Store user upload size
|
||||||
|
|||||||
@@ -68,119 +68,6 @@ class HelperService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if user has enough space to upload file
|
|
||||||
*
|
|
||||||
* @param $user_id
|
|
||||||
* @param int $file_size
|
|
||||||
* @param $temp_filename
|
|
||||||
*/
|
|
||||||
public function check_user_storage_capacity($user_id, int $file_size, $temp_filename): void
|
|
||||||
{
|
|
||||||
// Get user storage percentage and get storage_limitation setting
|
|
||||||
$user_storage_used = user_storage_percentage($user_id, $file_size);
|
|
||||||
|
|
||||||
// Check if user can upload
|
|
||||||
if (get_setting('storage_limitation') && $user_storage_used >= 100) {
|
|
||||||
// Delete file
|
|
||||||
Storage::disk('local')
|
|
||||||
->delete("chunks/$temp_filename");
|
|
||||||
|
|
||||||
// Abort uploading
|
|
||||||
// TODO: test pre exceed storage limit
|
|
||||||
abort(423, 'You exceed your storage limit!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Move file to external storage if is set
|
|
||||||
*
|
|
||||||
* @param string $file
|
|
||||||
* @param string $user_id
|
|
||||||
*/
|
|
||||||
public function move_file_to_external_storage($file, $user_id): void
|
|
||||||
{
|
|
||||||
$disk_local = \Storage::disk('local');
|
|
||||||
|
|
||||||
// Get file size
|
|
||||||
$filesize = $disk_local->size("files/$user_id/$file");
|
|
||||||
|
|
||||||
// If file is bigger than 5.2MB then run multipart upload
|
|
||||||
if ($filesize > 5242880) {
|
|
||||||
// Get driver
|
|
||||||
$driver = \Storage::getDriver();
|
|
||||||
|
|
||||||
// Get adapter
|
|
||||||
$adapter = $driver->getAdapter();
|
|
||||||
|
|
||||||
// Get client
|
|
||||||
$client = $adapter->getClient();
|
|
||||||
|
|
||||||
// Prepare the upload parameters.
|
|
||||||
// TODO: replace local files with temp folder
|
|
||||||
$uploader = new MultipartUploader($client, config('filesystems.disks.local.root') . "/files/$user_id/$file", [
|
|
||||||
'bucket' => $adapter->getBucket(),
|
|
||||||
'key' => "files/$user_id/$file",
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Upload content
|
|
||||||
$uploader->upload();
|
|
||||||
} catch (MultipartUploadException $e) {
|
|
||||||
// Write error log
|
|
||||||
Log::error($e->getMessage());
|
|
||||||
|
|
||||||
// Delete file after error
|
|
||||||
$disk_local->delete("files/$user_id/$file");
|
|
||||||
|
|
||||||
throw new HttpException(409, $e->getMessage());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Stream file object to s3
|
|
||||||
// TODO: replace local files with temp folder
|
|
||||||
Storage::putFileAs("files/$user_id", config('filesystems.disks.local.root') . "/files/$user_id/$file", $file, 'private');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete file after upload
|
|
||||||
$disk_local->delete("files/$user_id/$file");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create image thumbnail from gif, jpeg, jpg, png, webp or svg
|
|
||||||
*
|
|
||||||
* @param string $file_path
|
|
||||||
* @param string $filename
|
|
||||||
* @param string $user_id
|
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function create_image_thumbnail($file_path, $filename, $user_id)
|
|
||||||
{
|
|
||||||
// Create thumbnail from image
|
|
||||||
if (in_array(Storage::disk('local')->mimeType($file_path), ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'])) {
|
|
||||||
// Get thumbnail name
|
|
||||||
$thumbnail = "thumbnail-$filename";
|
|
||||||
|
|
||||||
// Create intervention image
|
|
||||||
$image = Image::make(Storage::disk('local')->path($file_path))
|
|
||||||
->orientate();
|
|
||||||
|
|
||||||
// Resize image
|
|
||||||
$image->resize(512, null, function ($constraint) {
|
|
||||||
$constraint->aspectRatio();
|
|
||||||
})->stream();
|
|
||||||
|
|
||||||
// Store thumbnail to disk
|
|
||||||
Storage::put("files/$user_id/$thumbnail", $image);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return thumbnail as svg file
|
|
||||||
if (Storage::disk('local')->mimeType($file_path) === 'image/svg+xml') {
|
|
||||||
$thumbnail = $filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $thumbnail ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call and download file
|
* Call and download file
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user