From 0a9740a36352f01a2c370ef5119e2ffd1d28ad59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=8Carodej?= Date: Sat, 23 Apr 2022 09:44:43 +0200 Subject: [PATCH] broadcast new files to the frontend after file was remotely uploaded --- config/language-translations.php | 2 +- config/vuefilemanager.php | 2 +- public/mix-manifest.json | 38 ++++++------ .../RemoteUpload/RemoteUploadPopup.vue | 22 ++++--- .../js/components/UI/Popover/PopoverItem.vue | 2 +- resources/js/store/modules/broadcasting.js | 60 +++++++++++-------- resources/js/store/modules/userAuth.js | 2 +- .../Actions/GetContentFromExternalSource.php | 5 ++ .../RemoteUploadFileController.php | 10 +++- .../VisitorRemoteUploadFileController.php | 10 +++- .../Files/Events/NewFileWasStoredEvent.php | 40 +++++++++++++ ...ilesRemotelyForUploadRequestController.php | 10 +++- src/Support/helpers.php | 10 ++++ tests/Domain/Files/FileTest.php | 8 +++ 14 files changed, 159 insertions(+), 62 deletions(-) create mode 100644 src/Domain/Files/Events/NewFileWasStoredEvent.php diff --git a/config/language-translations.php b/config/language-translations.php index f6b809f4..17715627 100644 --- a/config/language-translations.php +++ b/config/language-translations.php @@ -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', ], ]; diff --git a/config/vuefilemanager.php b/config/vuefilemanager.php index e2c5ee4d..658f2c5a 100644 --- a/config/vuefilemanager.php +++ b/config/vuefilemanager.php @@ -1,7 +1,7 @@ '2.0.19', + 'version' => '2.1.0', 'is_demo' => env('APP_DEMO', false), diff --git a/public/mix-manifest.json b/public/mix-manifest.json index d042daed..6051c791 100644 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -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" diff --git a/resources/js/components/RemoteUpload/RemoteUploadPopup.vue b/resources/js/components/RemoteUpload/RemoteUploadPopup.vue index d30acfc1..8c827c58 100644 --- a/resources/js/components/RemoteUpload/RemoteUploadPopup.vue +++ b/resources/js/components/RemoteUpload/RemoteUploadPopup.vue @@ -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); }) }, } diff --git a/resources/js/components/UI/Popover/PopoverItem.vue b/resources/js/components/UI/Popover/PopoverItem.vue index e1616a6f..cda24063 100644 --- a/resources/js/components/UI/Popover/PopoverItem.vue +++ b/resources/js/components/UI/Popover/PopoverItem.vue @@ -3,7 +3,7 @@
diff --git a/resources/js/store/modules/broadcasting.js b/resources/js/store/modules/broadcasting.js index 4c30007d..f691ad3a 100644 --- a/resources/js/store/modules/broadcasting.js +++ b/resources/js/store/modules/broadcasting.js @@ -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 { diff --git a/resources/js/store/modules/userAuth.js b/resources/js/store/modules/userAuth.js index ed46b1a3..5ee0dc2a 100644 --- a/resources/js/store/modules/userAuth.js +++ b/resources/js/store/modules/userAuth.js @@ -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') } }) diff --git a/src/Domain/Files/Actions/GetContentFromExternalSource.php b/src/Domain/Files/Actions/GetContentFromExternalSource.php index 6672f0bb..10dccd5a 100644 --- a/src/Domain/Files/Actions/GetContentFromExternalSource.php +++ b/src/Domain/Files/Actions/GetContentFromExternalSource.php @@ -1,6 +1,8 @@ ($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()); diff --git a/src/Domain/Files/Controllers/RemoteUploadFileController.php b/src/Domain/Files/Controllers/RemoteUploadFileController.php index 98460c5a..3be95aee 100644 --- a/src/Domain/Files/Controllers/RemoteUploadFileController.php +++ b/src/Domain/Files/Controllers/RemoteUploadFileController.php @@ -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); } diff --git a/src/Domain/Files/Controllers/VisitorRemoteUploadFileController.php b/src/Domain/Files/Controllers/VisitorRemoteUploadFileController.php index 0c051659..ef5a5ee7 100644 --- a/src/Domain/Files/Controllers/VisitorRemoteUploadFileController.php +++ b/src/Domain/Files/Controllers/VisitorRemoteUploadFileController.php @@ -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); } diff --git a/src/Domain/Files/Events/NewFileWasStoredEvent.php b/src/Domain/Files/Events/NewFileWasStoredEvent.php new file mode 100644 index 00000000..e3fc79b9 --- /dev/null +++ b/src/Domain/Files/Events/NewFileWasStoredEvent.php @@ -0,0 +1,40 @@ +file->user_id}"); + } +} diff --git a/src/Domain/UploadRequest/Controllers/UploadFilesRemotelyForUploadRequestController.php b/src/Domain/UploadRequest/Controllers/UploadFilesRemotelyForUploadRequestController.php index 2688fe9a..b9b9ba22 100644 --- a/src/Domain/UploadRequest/Controllers/UploadFilesRemotelyForUploadRequestController.php +++ b/src/Domain/UploadRequest/Controllers/UploadFilesRemotelyForUploadRequestController.php @@ -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()); diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 1a1dc13e..e1614831 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -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 diff --git a/tests/Domain/Files/FileTest.php b/tests/Domain/Files/FileTest.php index c4d88935..2b2c48fa 100644 --- a/tests/Domain/Files/FileTest.php +++ b/tests/Domain/Files/FileTest.php @@ -1,6 +1,7 @@ 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"); }); }