diff options
author | Julien Veyssier <eneiluj@posteo.net> | 2022-03-31 12:18:26 +0300 |
---|---|---|
committer | Julien Veyssier <eneiluj@posteo.net> | 2022-03-31 12:18:46 +0300 |
commit | 6f619c0ce4e346a3535c7c981b38aff79512993a (patch) | |
tree | 58f3a9fb6efdca75294228f589e03f4a5496f3a4 /lib | |
parent | 2942e049c9af66b92effa5f193fca3147b919d34 (diff) |
remove upload by link on server side, fix error management
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ImageController.php | 42 | ||||
-rw-r--r-- | lib/Service/ImageService.php | 100 |
2 files changed, 10 insertions, 132 deletions
diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php index 04c972f68..108a86a10 100644 --- a/lib/Controller/ImageController.php +++ b/lib/Controller/ImageController.php @@ -107,40 +107,14 @@ class ImageController extends Controller { try { $insertResult = $this->imageService->insertImageFile($documentId, $imagePath, $userId); - return new DataResponse($insertResult); - } catch (Exception $e) { - $this->logger->error('File insertion error', ['exception' => $e]); - return new DataResponse(['error' => 'File insertion error'], Http::STATUS_BAD_REQUEST); - } - } - - /** - * @NoAdminRequired - * @PublicPage - * - * @param string $link - * @param int $documentId - * @param int $sessionId - * @param string $sessionToken - * @param string|null $shareToken - * @return 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([], Http::STATUS_FORBIDDEN); - } - - try { - if ($shareToken) { - $downloadResult = $this->imageService->insertImageLinkPublic($documentId, $link, $shareToken); + if (isset($insertResult['error'])) { + return new DataResponse($insertResult, Http::STATUS_BAD_REQUEST); } else { - $userId = $this->getUserIdFromSession($documentId, $sessionId, $sessionToken); - $downloadResult = $this->imageService->insertImageLink($documentId, $link, $userId); + return new DataResponse($insertResult); } - 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); + $this->logger->error('File insertion error', ['exception' => $e]); + return new DataResponse(['error' => 'File insertion error'], Http::STATUS_BAD_REQUEST); } } @@ -176,7 +150,11 @@ class ImageController extends Controller { $userId = $this->getUserIdFromSession($documentId, $sessionId, $sessionToken); $uploadResult = $this->imageService->uploadImage($documentId, $newFileName, $newFileResource, $userId); } - return new DataResponse($uploadResult); + if (isset($insertResult['error'])) { + return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST); + } else { + return new DataResponse($uploadResult); + } } return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST); } catch (Exception $e) { diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php index 31b189a19..3f7b0a76f 100644 --- a/lib/Service/ImageService.php +++ b/lib/Service/ImageService.php @@ -238,50 +238,6 @@ class ImageService { } /** - * Download and save an image from a link in the attachment folder - * - * @param int $documentId - * @param string $link - * @param string $userId - * @return array - * @throws NotFoundException - * @throws NotPermittedException - * @throws \OCP\Files\InvalidPathException - * @throws \OCP\Lock\LockedException - * @throws \OC\User\NoUserException - */ - public function insertImageLink(int $documentId, string $link, string $userId): array { - $textFile = $this->getTextFile($documentId, $userId); - if (!$textFile->isUpdateable()) { - throw new NotPermittedException('No write permissions'); - } - $saveDir = $this->getAttachmentDirectoryForFile($textFile, true); - return $this->downloadLink($saveDir, $link, $textFile); - } - - /** - * Download and save an image from a link in the attachment folder in a public context - * - * @param int|null $documentId - * @param string $link - * @param string $shareToken - * @return array|string[] - * @throws NotFoundException - * @throws NotPermittedException - * @throws \OCP\Files\InvalidPathException - * @throws \OCP\Lock\LockedException - * @throws \OC\User\NoUserException - */ - public function insertImageLinkPublic(?int $documentId, string $link, string $shareToken): array { - if (!$this->hasUpdatePermissions($shareToken)) { - throw new NotPermittedException('No write permissions'); - } - $textFile = $this->getTextFilePublic($documentId, $shareToken); - $saveDir = $this->getAttachmentDirectoryForFile($textFile, true); - return $this->downloadLink($saveDir, $link, $textFile); - } - - /** * Check if the shared access has write permissions * * @param string $shareToken @@ -303,62 +259,6 @@ class ImageService { } /** - * Download an image from a link and place it in a given folder - * @param Folder $saveDir - * @param string $link - * @param File $textFile - * @return array - * @throws NotFoundException - * @throws \OCP\Files\InvalidPathException - * @throws \OCP\Files\NotPermittedException - * @throws \OCP\Lock\LockedException - */ - private function downloadLink(Folder $saveDir, string $link, File $textFile): array { - $fileName = (string) time(); - $savedFile = $saveDir->newFile($fileName); - $resource = $savedFile->fopen('wb'); - $res = $this->simpleDownload($link, $resource); - if (is_resource($resource)) { - fclose($resource); - } - $savedFile->touch(); - if (isset($res['Content-Type'])) { - if (in_array($res['Content-Type'], ImageController::IMAGE_MIME_TYPES, true)) { - if ($res['Content-Type'] === 'image/jpeg') { - $fileName .= '.jpg'; - } elseif ($res['Content-Type'] === 'image/x-xbitmap' || $res['Content-Type'] === 'image/x-ms-bmp') { - $fileName .= '.bmp'; - } elseif ($res['Content-Type'] === 'image/svg+xml') { - $fileName .= '.svg'; - } else { - $ext = preg_replace('/^image\//i', '', $res['Content-Type']); - $fileName .= '.' . $ext; - } - $targetPath = $saveDir->getPath() . '/' . $fileName; - $savedFile->move($targetPath); - // get file type and name - return [ - 'name' => $fileName, - 'id' => $savedFile->getId(), - 'documentId' => $textFile->getId(), - ]; - } - $savedFile->delete(); - return [ - 'error' => 'Unsupported file type', - ]; - } elseif (isset($res['error'])) { - $savedFile->delete(); - return $res; - } else { - $savedFile->delete(); - return [ - 'error' => 'Link download error', - ]; - } - } - - /** * Get or create file-specific attachment folder * * @param File $textFile |