broadcast new files to the frontend after file was remotely uploaded

This commit is contained in:
Čarodej
2022-04-23 09:44:43 +02:00
parent 4b366747b6
commit 0a9740a363
14 changed files with 159 additions and 62 deletions
@@ -1,6 +1,8 @@
<?php
namespace Domain\Files\Actions;
use Domain\Files\Events\NewFileWasStoredEvent;
use Domain\Files\Resources\FileResource;
use Log;
use Error;
use ErrorException;
@@ -80,6 +82,9 @@ class GetContentFromExternalSource
'ftp', 'azure' => ($this->moveFileToFTPStorage)($basename, $user->id),
default => null
};
// Broadcast new file into the frontend
NewFileWasStoredEvent::dispatch(new FileResource($file));
} catch (ErrorException | Error $e) {
Log::error("Remote upload failed as {$e->getMessage()}");
Log::error($e->getTraceAsString());
@@ -28,8 +28,14 @@ class RemoteUploadFileController extends Controller
->user
: auth()->user();
// Execute job for get content from url and save
($this->getContentFromExternalSource)($request->all(), $user);
// Get content from external sources
if (isBroadcasting()) {
($this->getContentFromExternalSource)
->onQueue()
->execute($request->all(), $user);
} else {
($this->getContentFromExternalSource)($request->all(), $user);
}
return response('Files were successfully added to the upload queue', 201);
}
@@ -31,8 +31,14 @@ class VisitorRemoteUploadFileController extends Controller
// Check access to requested directory
($this->verifyAccessToItem)($request->input('parent_id'), $shared);
// Execute job for get content from url and save
($this->getContentFromExternalSource)($request->all(), $shared->user);
// Get content from external sources
if (isBroadcasting()) {
($this->getContentFromExternalSource)
->onQueue()
->execute($request->all(), $shared->user);
} else {
($this->getContentFromExternalSource)($request->all(), $shared->user);
}
return response('Files were successfully added to the upload queue', 201);
}
@@ -0,0 +1,40 @@
<?php
namespace Domain\Files\Events;
use Domain\Files\Resources\FileResource;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewFileWasStoredEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(
public FileResource $file,
) {}
/**
* The event's broadcast name.
*/
public function broadcastAs(): string
{
return 'file.created';
}
/**
* Get the channels the event should broadcast on.
*/
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel("App.Users.Models.User.{$this->file->user_id}");
}
}
@@ -33,8 +33,14 @@ class UploadFilesRemotelyForUploadRequestController
$request->merge(['parent_id' => $uploadRequest->id]);
}
// Execute job for get content from url and save
($this->getContentFromExternalSource)($request->all(), $uploadRequest->user);
// 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());
+10
View File
@@ -38,6 +38,16 @@ if (! function_exists('getListOfLatestLogs')) {
}
}
if (! function_exists('isBroadcasting')) {
/**
* Check if cron is running
*/
function isBroadcasting(): bool
{
return config('broadcasting.default') === 'pusher';
}
}
if (! function_exists('isRunningCron')) {
/**
* Check if cron is running