mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-06 02:33:48 +00:00
75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
<?php
|
|
namespace Domain\RemoteUpload\Controllers;
|
|
|
|
use DB;
|
|
use Domain\Folders\Models\Folder;
|
|
use Domain\Files\Requests\RemoteUploadRequest;
|
|
use Domain\UploadRequest\Models\UploadRequest;
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
|
use Domain\RemoteUpload\Actions\GetContentFromExternalSource;
|
|
|
|
class UploadFilesRemotelyForUploadRequestController
|
|
{
|
|
public function __construct(
|
|
private GetContentFromExternalSource $getContentFromExternalSource,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @throws FileNotFoundException
|
|
*/
|
|
public function __invoke(RemoteUploadRequest $request, UploadRequest $uploadRequest)
|
|
{
|
|
// Get upload request root folder query
|
|
$folder = Folder::where('id', $uploadRequest->id);
|
|
|
|
// Create folder if not exist
|
|
if ($folder->doesntExist()) {
|
|
$this->createFolder($uploadRequest);
|
|
}
|
|
|
|
// Set default parent_id for uploaded file
|
|
if (is_null($request->input('parent_id'))) {
|
|
$request->merge(['parent_id' => $uploadRequest->id]);
|
|
}
|
|
|
|
// Get content from external sources
|
|
if (isBroadcasting()) {
|
|
($this->getContentFromExternalSource)
|
|
->onQueue()
|
|
->execute($request->all(), $uploadRequest->user);
|
|
} else {
|
|
($this->getContentFromExternalSource)($request->all(), $uploadRequest->user);
|
|
}
|
|
|
|
// Set timestamp for auto filling
|
|
cache()->set("auto-filling.$uploadRequest->id", now()->toString());
|
|
|
|
return response('Files were successfully added to the upload queue', 201);
|
|
}
|
|
|
|
/**
|
|
* Create root Upload Request folder
|
|
*/
|
|
private function createFolder(UploadRequest $uploadRequest): void
|
|
{
|
|
// Format timestamp
|
|
$timestamp = format_date($uploadRequest->created_at, 'd. M. Y');
|
|
|
|
// Create folder
|
|
DB::table('folders')->insert([
|
|
'id' => $uploadRequest->id,
|
|
'parent_id' => $uploadRequest->folder_id ?? null,
|
|
'user_id' => $uploadRequest->user_id,
|
|
'name' => $uploadRequest->name ?? __t('upload_request_default_folder', ['timestamp' => $timestamp]),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
// Update upload request status
|
|
$uploadRequest->update([
|
|
'status' => 'filling',
|
|
]);
|
|
}
|
|
}
|