diff options
-rw-r--r-- | lib/Controller/ImageController.php | 107 | ||||
-rw-r--r-- | src/components/EditorWrapper.vue | 1 | ||||
-rw-r--r-- | src/components/MenuBar.vue | 41 | ||||
-rw-r--r-- | src/services/SyncService.js | 38 |
4 files changed, 95 insertions, 92 deletions
diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index b3aa3f111..86111846a 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace OCA\Text\Controller; use Exception; +use OCA\Text\Service\SessionService; use OCP\AppFramework\Http; use OCA\Text\Service\ImageService; use OCP\AppFramework\Controller; @@ -59,29 +60,44 @@ class ImageController extends Controller { * @var LoggerInterface */ private $logger; + /** + * @var SessionService + */ + private $sessionService; public function __construct(string $appName, IRequest $request, LoggerInterface $logger, ImageService $imageService, + SessionService $sessionService, ?string $userId) { parent::__construct($appName, $request); $this->userId = $userId; $this->imageService = $imageService; $this->request = $request; $this->logger = $logger; + $this->sessionService = $sessionService; } /** * @NoAdminRequired + * @PublicPage * - * @param int $textFileId + * @param int $documentId + * @param int $sessionId + * @param string $sessionToken * @param string $imagePath * @return DataResponse */ - public function insertImageFile(int $textFileId, string $imagePath): DataResponse { + public function insertImageFile(int $documentId, int $sessionId, string $sessionToken, string $imagePath): DataResponse { + if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) { + return new DataResponse([], 500); + } + $session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken); + $userId = $session->getUserId(); + try { - $insertResult = $this->imageService->insertImageFile($textFileId, $imagePath, $this->userId); + $insertResult = $this->imageService->insertImageFile($documentId, $imagePath, $userId); if (isset($insertResult['error'])) { return new DataResponse($insertResult, Http::STATUS_BAD_REQUEST); } else { @@ -95,14 +111,28 @@ class ImageController extends Controller { /** * @NoAdminRequired + * @PublicPage * - * @param int $textFileId * @param string $link + * @param int $documentId + * @param int $sessionId + * @param string $sessionToken + * @param string|null $shareToken * @return DataResponse */ - public function insertImageLink(int $textFileId, string $link): DataResponse { + public function insertImageLink(string $link, int $documentId, int $sessionId, string $sessionToken, ?string $shareToken = null): DataResponse { + if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) { + return new DataResponse([], 500); + } + try { - $downloadResult = $this->imageService->insertImageLink($textFileId, $link, $this->userId); + if ($shareToken) { + $downloadResult = $this->imageService->insertImageLinkPublic($documentId, $link, $shareToken); + } else { + $session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken); + $userId = $session->getUserId(); + $downloadResult = $this->imageService->insertImageLink($documentId, $link, $userId); + } if (isset($downloadResult['error'])) { return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST); } else { @@ -118,32 +148,17 @@ class ImageController extends Controller { * @NoAdminRequired * @PublicPage * - * @param int|null $textFileId can be null with public file share - * @param string $link - * @param string $shareToken + * @param int $documentId + * @param int $sessionId + * @param string $sessionToken + * @param string|null $shareToken * @return DataResponse */ - public function insertImageLinkPublic(?int $textFileId, string $link, string $shareToken): DataResponse { - try { - $downloadResult = $this->imageService->insertImageLinkPublic($textFileId, $link, $shareToken); - if (isset($downloadResult['error'])) { - return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST); - } else { - return new DataResponse($downloadResult); - } - } catch (Exception $e) { - $this->logger->error('Link insertion error', ['exception' => $e]); - return new DataResponse(['error' => 'Link insertion error'], Http::STATUS_BAD_REQUEST); + public function uploadImage(int $documentId, int $sessionId, string $sessionToken, ?string $shareToken = null): DataResponse { + if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) { + return new DataResponse([], 500); } - } - /** - * @NoAdminRequired - * - * @param int $textFileId - * @return DataResponse - */ - public function uploadImage(int $textFileId): DataResponse { try { $file = $this->request->getUploadedFile('image'); if ($file !== null && isset($file['tmp_name'], $file['name'], $file['type'])) { @@ -152,39 +167,13 @@ class ImageController extends Controller { } $newFileContent = file_get_contents($file['tmp_name']); $newFileName = $file['name']; - $uploadResult = $this->imageService->uploadImage($textFileId, $newFileName, $newFileContent, $this->userId); - if (isset($uploadResult['error'])) { - return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST); + if ($shareToken) { + $uploadResult = $this->imageService->uploadImagePublic($documentId, $newFileName, $newFileContent, $shareToken); } else { - return new DataResponse($uploadResult); - } - } else { - return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST); - } - } catch (Exception $e) { - $this->logger->error('Upload error', ['exception' => $e]); - return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST); - } - } - - /** - * @NoAdminRequired - * @PublicPage - * - * @param int|null $textFileId can be null with public file share - * @param string $shareToken - * @return DataResponse - */ - public function uploadImagePublic(?int $textFileId, string $shareToken): DataResponse { - try { - $file = $this->request->getUploadedFile('image'); - if ($file !== null && isset($file['tmp_name'], $file['name'], $file['type'])) { - if (!in_array($file['type'], self::IMAGE_MIME_TYPES)) { - return new DataResponse(['error' => 'Image type not supported'], Http::STATUS_BAD_REQUEST); + $session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken); + $userId = $session->getUserId(); + $uploadResult = $this->imageService->uploadImage($documentId, $newFileName, $newFileContent, $userId); } - $newFileContent = file_get_contents($file['tmp_name']); - $newFileName = $file['name']; - $uploadResult = $this->imageService->uploadImagePublic($textFileId, $newFileName, $newFileContent, $shareToken); if (isset($uploadResult['error'])) { return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST); } else { diff --git a/src/components/EditorWrapper.vue b/src/components/EditorWrapper.vue index 9438a439a..a19e29b7e 100644 --- a/src/components/EditorWrapper.vue +++ b/src/components/EditorWrapper.vue @@ -38,6 +38,7 @@ <MenuBar v-if="!syncError && !readOnly" ref="menubar" :editor="tiptap" + :sync-service="syncService" :file-path="relativePath" :file-id="fileId" :is-rich-editor="isRichEditor" diff --git a/src/components/MenuBar.vue b/src/components/MenuBar.vue index 32ddce390..88fee8746 100644 --- a/src/components/MenuBar.vue +++ b/src/components/MenuBar.vue @@ -141,8 +141,6 @@ import PopoverMenu from '@nextcloud/vue/dist/Components/PopoverMenu' import EmojiPicker from '@nextcloud/vue/dist/Components/EmojiPicker' import ClickOutside from 'vue-click-outside' import { getCurrentUser } from '@nextcloud/auth' -import axios from '@nextcloud/axios' -import { generateUrl } from '@nextcloud/router' import { showError } from '@nextcloud/dialogs' const imageMimes = [ @@ -180,6 +178,11 @@ export default { required: false, default: null, }, + syncService: { + type: Object, + required: false, + default: null, + }, isRichEditor: { type: Boolean, default: true, @@ -377,20 +380,7 @@ export default { // the same file is picked again. event.target.value = '' - const formData = new FormData() - formData.append('image', image) - formData.append('textFileId', this.fileId) - if (this.isPublic) { - formData.append('shareToken', this.sharingToken) - } - const url = this.isPublic - ? generateUrl('/apps/text/public/image/upload') - : generateUrl('/apps/text/image/upload') - axios.post(url, formData, { - headers: { - 'Content-Type': 'multipart/form-data', - }, - }).then((response) => { + this.syncService.uploadImage(image).then((response) => { this.insertAttachmentImage(response.data?.name, response.data?.id, this.imageCommand, response.data?.textFileId) }).catch((error) => { console.error(error) @@ -409,17 +399,7 @@ export default { this.showImageLinkPrompt = false this.$refs.imageActions[0].closeMenu() - const params = { - textFileId: this.fileId, - link: this.imageLink, - } - if (this.isPublic) { - params.shareToken = this.sharingToken - } - const url = this.isPublic - ? generateUrl('/apps/text/public/image/link') - : generateUrl('/apps/text/image/link') - axios.post(url, params).then((response) => { + this.syncService.insertImageLink(this.imageLink).then((response) => { this.insertAttachmentImage(response.data?.name, response.data?.id, command, response.data?.textFileId) }).catch((error) => { console.error(error) @@ -433,12 +413,7 @@ export default { this.uploadingImage = true this.$refs.imageActions[0].closeMenu() - const params = { - textFileId: this.fileId, - imagePath, - } - const url = generateUrl('/apps/text/image/filepath') - axios.post(url, params).then((response) => { + this.syncService.insertImageFile(imagePath).then((response) => { this.insertAttachmentImage(response.data?.name, response.data?.id, command, response.data?.textFileId) }).catch((error) => { console.error(error) diff --git a/src/services/SyncService.js b/src/services/SyncService.js index a533f6b7b..651a5ee2b 100644 --- a/src/services/SyncService.js +++ b/src/services/SyncService.js @@ -287,6 +287,44 @@ class SyncService { }) } + uploadImage(image) { + const formData = new FormData() + formData.append('image', image) + formData.append('documentId', this.document.id) + formData.append('sessionId', this.session.id) + formData.append('sessionToken', this.session.token) + formData.append('shareToken', this.options.shareToken || '') + const url = endpointUrl('image/upload') + return axios.post(url, formData, { + headers: { + 'Content-Type': 'multipart/form-data', + }, + }) + } + + insertImageLink(imageLink) { + const params = { + documentId: this.document.id, + sessionId: this.session.id, + sessionToken: this.session.token, + shareToken: this.options.shareToken || '', + link: imageLink, + } + const url = endpointUrl('image/link') + return axios.post(url, params) + } + + insertImageFile(imagePath) { + const params = { + documentId: this.document.id, + sessionId: this.session.id, + sessionToken: this.session.token, + imagePath, + } + const url = endpointUrl('image/filepath') + return axios.post(url, params) + } + on(event, callback, _this) { this.eventHandlers[event].push(callback.bind(_this)) return this |