ftp implementation

This commit is contained in:
Čarodej
2022-04-11 09:34:25 +02:00
parent 7740e1673f
commit fbc9eed30b
12 changed files with 192 additions and 83 deletions

View File

@@ -42,7 +42,7 @@ class UserSetting extends Model
$link = [];
// Get avatar from external storage
if ($this->attributes['avatar'] && ! isStorageDriver('local')) {
if ($this->attributes['avatar'] && isStorageDriver('s3')) {
foreach (config('vuefilemanager.avatar_sizes') as $item) {
$filePath = "avatars/{$item['name']}-{$this->attributes['avatar']}";

View File

@@ -0,0 +1,22 @@
<?php
namespace Domain\Files\Actions;
use Illuminate\Support\Facades\Storage;
class MoveFileToFTPStorageAction
{
/**
* Move file to external storage if is set
*/
public function __invoke(
string $file,
string $userId
): void {
// Stream file object to ftp
Storage::putFileAs("files/$userId", config('filesystems.disks.local.root') . "/files/$userId/$file", $file, 'private');
// Delete file after upload
Storage::disk('local')->delete("files/$userId/$file");
}
}

View File

@@ -15,10 +15,11 @@ class UploadFileAction
{
public function __construct(
public RecordUploadAction $recordUpload,
public ProcessImageThumbnailAction $createImageThumbnail,
public GetFileParentId $getFileParentId,
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
public StoreFileExifMetadataAction $storeExifMetadata,
public MoveFileToFTPStorageAction $moveFileToFTPStorage,
public ProcessImageThumbnailAction $createImageThumbnail,
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
) {
}
@@ -82,9 +83,10 @@ class UploadFileAction
($this->createImageThumbnail)($fileName, $file, $user->id);
// Move files to external storage
if (! isStorageDriver('local')) {
($this->moveFileToExternalStorage)($fileName, $user->id);
}
match (config('filesystems.default')) {
's3' => ($this->moveFileToExternalStorage)($fileName, $user->id),
'ftp' => ($this->moveFileToFTPStorage)($fileName, $user->id),
};
// Create new file
$item = UserFile::create([

View File

@@ -101,7 +101,7 @@ class File extends Model
->all();
// Generate thumbnail link for external storage service
if ($this->type === 'image' && ! isStorageDriver('local')) {
if ($this->type === 'image' && isStorageDriver('s3')) {
foreach ($thumbnail_sizes as $item) {
$filePath = "files/{$this->user_id}/{$item['name']}-{$this->basename}";

View File

@@ -48,6 +48,11 @@ class ZipAction
$zip->add("s3://$bucketName/$filePath", $file->name);
}
// ftp client
if (isStorageDriver('ftp')) {
$zip->addRaw(Storage::get($filePath), $file->name);
}
}
});
@@ -79,12 +84,15 @@ class ZipAction
$zip->add(Storage::path($filePath), $zipDestination);
}
// s3 client
if (isStorageDriver('s3')) {
$bucketName = config('filesystems.disks.s3.bucket');
$zip->add("s3://$bucketName/$filePath", $zipDestination);
}
if (isStorageDriver('ftp')) {
$zip->addRaw(Storage::get($filePath), $zipDestination);
}
}
}
});

View File

@@ -272,10 +272,8 @@ if (! function_exists('get_storage')) {
if (! function_exists('isStorageDriver')) {
/**
* Check if is running AWS s3 as storage
*
* @return bool
*/
function isStorageDriver($driver)
function isStorageDriver(string|array $driver): bool
{
if (is_array($driver)) {
return in_array(config('filesystems.default'), $driver);