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

View File

@@ -948,6 +948,6 @@ return [
'remote_links_help' => 'For every line paste one link',
'paste_remote_links_here' => 'Paste your remote links here...',
'remote_download_submitted' => 'Your links will be downloaded as soon as possible',
'remote_download_processed' => 'Your links were processed successfully',
'remote_download_processed' => 'Links was added to the queue and will be downloaded soon',
],
];

View File

@@ -1,7 +1,7 @@
<?php
return [
'version' => '2.0.19',
'version' => '2.1.0',
'is_demo' => env('APP_DEMO', false),

View File

@@ -1,27 +1,27 @@
{
"/js/main.js": "/js/main.js",
"/chunks/request.js": "/chunks/request.js?id=b493de3b8cf4a8f2",
"/chunks/request-upload.js": "/chunks/request-upload.js?id=efb89edeaed37404",
"/chunks/request.js": "/chunks/request.js?id=3904f27eff940fdc",
"/chunks/request-upload.js": "/chunks/request-upload.js?id=3bc3423d2305cd30",
"/chunks/setup-wizard.js": "/chunks/setup-wizard.js?id=19a0784e59d768ec",
"/chunks/status-check.js": "/chunks/status-check.js?id=f40938d1fb43820f",
"/chunks/status-check.js": "/chunks/status-check.js?id=9239a586761b912d",
"/chunks/purchase-code.js": "/chunks/purchase-code.js?id=ba76b9a8adbfdc0b",
"/chunks/database.js": "/chunks/database.js?id=5113b0d4284f764f",
"/chunks/environment.js": "/chunks/environment.js?id=784c2442268b36dc",
"/chunks/environment.js": "/chunks/environment.js?id=e4fdb87ff173d48a",
"/chunks/app-setup.js": "/chunks/app-setup.js?id=cbe7bfed06400736",
"/chunks/admin-account.js": "/chunks/admin-account.js?id=78d257775f5fc485",
"/chunks/shared.js": "/chunks/shared.js?id=54733651dfb0af9f",
"/chunks/shared/browser.js": "/chunks/shared/browser.js?id=d2fff07a2bc7af3f",
"/chunks/shared.js": "/chunks/shared.js?id=1d98332d75f89854",
"/chunks/shared/browser.js": "/chunks/shared/browser.js?id=3fd9baddb7333c24",
"/chunks/shared/single-file.js": "/chunks/shared/single-file.js?id=a6063bed9be75a09",
"/chunks/shared/authenticate.js": "/chunks/shared/authenticate.js?id=b5519d193bce2339",
"/chunks/not-found.js": "/chunks/not-found.js?id=d31bd699138cf828",
"/chunks/temporary-unavailable.js": "/chunks/temporary-unavailable.js?id=26798085f527d955",
"/chunks/admin.js": "/chunks/admin.js?id=780ebe14261335f6",
"/chunks/admin.js": "/chunks/admin.js?id=70e9dc019ac56c11",
"/chunks/dashboard.js": "/chunks/dashboard.js?id=5ab55a12214433c8",
"/chunks/invoices.js": "/chunks/invoices.js?id=799928609f57ca10",
"/chunks/subscriptions.js": "/chunks/subscriptions.js?id=a0c4f59d0ec4aee0",
"/chunks/pages.js": "/chunks/pages.js?id=bb5cc8327ca846a3",
"/chunks/page-edit.js": "/chunks/page-edit.js?id=0bdc8a5935fd2197",
"/chunks/plans.js": "/chunks/plans.js?id=1d04fbdca84afd42",
"/chunks/plans.js": "/chunks/plans.js?id=76398906ad873fc3",
"/chunks/users.js": "/chunks/users.js?id=b6629338e26b8313",
"/chunks/user-create.js": "/chunks/user-create.js?id=40254ae98547761e",
"/chunks/plan-create/fixed.js": "/chunks/plan-create/fixed.js?id=18f9d1ab17996507",
@@ -42,7 +42,7 @@
"/chunks/app-settings.js": "/chunks/app-settings.js?id=b0d1082fdcbbd17c",
"/chunks/app-appearance.js": "/chunks/app-appearance.js?id=8ba3feb2cc81a2c3",
"/chunks/app-index.js": "/chunks/app-index.js?id=0c50096e8de09288",
"/chunks/app-environment.js": "/chunks/app-environment.js?id=f5cdd68ebc6a6b54",
"/chunks/app-environment.js": "/chunks/app-environment.js?id=3436286fd625f8b7",
"/chunks/app-others.js": "/chunks/app-others.js?id=dd23507db4551d0a",
"/chunks/app-sign-in-out.js": "/chunks/app-sign-in-out.js?id=f79027ce1f1f4c4b",
"/chunks/app-adsense.js": "/chunks/app-adsense.js?id=c7e7dc2975317062",
@@ -53,22 +53,22 @@
"/chunks/contact-us.js": "/chunks/contact-us.js?id=ea99d85aa3500595",
"/chunks/successfully-email-verified.js": "/chunks/successfully-email-verified.js?id=c26cb144101e7c79",
"/chunks/successfully-email-send.js": "/chunks/successfully-email-send.js?id=170d814982e1c475",
"/chunks/sign-in.js": "/chunks/sign-in.js?id=5f7111f8ee7639bf",
"/chunks/sign-up.js": "/chunks/sign-up.js?id=80da89f329c514fc",
"/chunks/sign-in.js": "/chunks/sign-in.js?id=c648cb698176a993",
"/chunks/sign-up.js": "/chunks/sign-up.js?id=3d7559511768cd0e",
"/chunks/forgotten-password.js": "/chunks/forgotten-password.js?id=27cda9364b6593d8",
"/chunks/create-new-password.js": "/chunks/create-new-password.js?id=2f0401ee2fc148c4",
"/chunks/settings.js": "/chunks/settings.js?id=c8a4915f96c762d4",
"/chunks/settings.js": "/chunks/settings.js?id=ee47f96c6c7d8a5c",
"/chunks/profile.js": "/chunks/profile.js?id=3e24bb5e1f52d4bb",
"/chunks/settings-password.js": "/chunks/settings-password.js?id=d00bf503d8126dc4",
"/chunks/settings-storage.js": "/chunks/settings-storage.js?id=092e324aad54656b",
"/chunks/billing.js": "/chunks/billing.js?id=115c25478cee576d",
"/chunks/platform.js": "/chunks/platform.js?id=907e530c6cc415bc",
"/chunks/files.js": "/chunks/files.js?id=aaea9173f7697d6e",
"/chunks/recent-uploads.js": "/chunks/recent-uploads.js?id=4bab41df721a6fc6",
"/chunks/my-shared-items.js": "/chunks/my-shared-items.js?id=c62bc3eb07de20df",
"/chunks/trash.js": "/chunks/trash.js?id=31e826373e0d01f8",
"/chunks/team-folders.js": "/chunks/team-folders.js?id=4babe7db8a9a3a16",
"/chunks/shared-with-me.js": "/chunks/shared-with-me.js?id=cfb073052d66d07c",
"/chunks/platform.js": "/chunks/platform.js?id=79cbc00b09b132d7",
"/chunks/files.js": "/chunks/files.js?id=b09c3f8b8b6aabcc",
"/chunks/recent-uploads.js": "/chunks/recent-uploads.js?id=c6ed26eaace65f01",
"/chunks/my-shared-items.js": "/chunks/my-shared-items.js?id=6adbb309c700eaf7",
"/chunks/trash.js": "/chunks/trash.js?id=32b84e2c25561372",
"/chunks/team-folders.js": "/chunks/team-folders.js?id=dc85357a1433f4e4",
"/chunks/shared-with-me.js": "/chunks/shared-with-me.js?id=3168dd6a723f72f1",
"/chunks/invitation.js": "/chunks/invitation.js?id=424b2783d9785a09",
"/css/tailwind.css": "/css/tailwind.css",
"/css/app.css": "/css/app.css"

View File

@@ -93,15 +93,21 @@ export default {
parent_id: parentId,
})
.then(() => {
events.$emit('toaster', {
type: 'success',
message: this.$t('remote_download_processed'),
})
// If broadcasting
if (this.$store.getters.isBroadcasting) {
events.$emit('toaster', {
type: 'success',
message: this.$t('remote_download_processed'),
})
}
// If broadcasting is not set
if (!this.$store.getters.isBroadcasting) {
// Reload data
this.$getDataByLocation()
}
events.$emit('popup:close')
// Reload data
this.$getDataByLocation()
})
.catch((error) => {
if (error.response.status === 422) {
@@ -129,6 +135,8 @@ export default {
this.$nextTick(() => {
setTimeout(() => this.$refs.textarea.focus(), 100)
})
console.log(this.$store.getters.isBroadcasting);
})
},
}

View File

@@ -3,7 +3,7 @@
<!--Overlay component-->
<div
@click.capture="hidePopover"
class="absolute top-12 z-20 w-60 overflow-hidden rounded-xl bg-white shadow-xl dark:bg-dark-foreground"
class="absolute top-12 z-20 w-60 overflow-hidden rounded-xl bg-white shadow-xl dark:bg-dark-foreground select-none"
:class="{ 'right-0': side === 'left', 'left-0': side === 'right' }"
>
<slot />

View File

@@ -1,48 +1,56 @@
import {events} from "../../bus";
import { events } from '../../bus'
const defaultState = {
isRunningConnection: false,
isBroadcasting: false,
}
const actions = {
runConnection: ({ commit, getters, dispatch }) => {
commit('SET_RUNNING_COMMUNICATION')
Echo.private(`App.Users.Models.User.${getters.user.data.id}`)
.notification((notification) => {
Echo.private(`App.Users.Models.User.${getters.user.data.id}`)
.listen('.file.created', (event) => {
// If user is located in same directory as remote upload was called, then show the files
if (
(!getters.currentFolder && !event.file.data.attributes.parent_id) ||
(getters.currentFolder && event.file.data.attributes.parent_id === getters.currentFolder.data.id)
) {
// Add received item into view
commit('ADD_NEW_ITEMS', event.file)
}
})
.notification((notification) => {
// Play audio
new Audio('/audio/blop.wav').play()
// Play audio
new Audio('/audio/blop.wav').play();
// Call toaster notification
events.$emit('notification', {
data: {
type: notification.category,
id: notification.id,
attributes: {
action: notification.action,
description: notification.description,
title: notification.title,
category: notification.category,
},
},
})
// Call toaster notification
events.$emit('notification', {
data: {
type: notification.category,
id: notification.id,
attributes: {
action: notification.action,
description: notification.description,
title: notification.title,
category: notification.category,
},
},
})
// Reload user data to update notifications
dispatch('getAppData')
});
// Reload user data to update notifications
dispatch('getAppData')
})
},
}
const mutations = {
SET_RUNNING_COMMUNICATION(state) {
state.isRunningConnection = true
state.isBroadcasting = true
},
}
const getters = {
isRunningConnection: (state) => state.isRunningConnection,
isBroadcasting: (state) => state.isBroadcasting,
}
export default {

View File

@@ -18,7 +18,7 @@ const actions = {
commit('RETRIEVE_USER', response.data)
commit('UPDATE_NOTIFICATION_COUNT', response.data.data.relationships.unreadNotifications.data.length)
if (! getters.isRunningConnection && getters.config.broadcasting === 'pusher') {
if (! getters.isBroadcasting && getters.config.broadcasting === 'pusher') {
dispatch('runConnection')
}
})

View File

@@ -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());

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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}");
}
}

View File

@@ -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());

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

View File

@@ -1,6 +1,7 @@
<?php
namespace Tests\Domain\Files;
use Event;
use Storage;
use Tests\TestCase;
use App\Users\Models\User;
@@ -10,6 +11,7 @@ use Domain\Folders\Models\Folder;
use Illuminate\Http\UploadedFile;
use Domain\Settings\Models\Setting;
use Illuminate\Support\Facades\Http;
use Domain\Files\Events\NewFileWasStoredEvent;
class FileTest extends TestCase
{
@@ -108,6 +110,10 @@ class FileTest extends TestCase
*/
public function it_remotely_upload_new_file()
{
Event::fake([
NewFileWasStoredEvent::class,
]);
$user = User::factory()
->hasSettings()
->create();
@@ -147,6 +153,8 @@ class FileTest extends TestCase
File::all()
->each(function ($file) {
Event::assertDispatched(fn(NewFileWasStoredEvent $event) => $event->file->id === $file->id);
Storage::assertExists("files/$file->user_id/$file->basename");
});
}