Skip to content

Commit

Permalink
rename most image stuff to file/media/attachment
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
Julien Veyssier committed Jul 14, 2022
1 parent 778043b commit 01e519c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 123 deletions.
4 changes: 2 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

return [
'routes' => [
['name' => 'Image#insertImageFile', 'url' => '/image/filepath', 'verb' => 'POST'],
['name' => 'Image#uploadImage', 'url' => '/image/upload', 'verb' => 'POST'],
['name' => 'Image#insertAttachmentFile', 'url' => '/attachment/filepath', 'verb' => 'POST'],
['name' => 'Image#uploadAttachment', 'url' => '/attachment/upload', 'verb' => 'POST'],
['name' => 'Image#getImageFile', 'url' => '/image', 'verb' => 'GET'],
['name' => 'Image#getMediaFile', 'url' => '/media', 'verb' => 'GET'],
['name' => 'Image#getMediaFilePreview', 'url' => '/mediaPreview', 'verb' => 'GET'],
Expand Down
19 changes: 7 additions & 12 deletions lib/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ public function __construct(string $appName,
* @param int $documentId
* @param int $sessionId
* @param string $sessionToken
* @param string $imagePath
* @param string $filePath
* @return DataResponse
*/
public function insertImageFile(int $documentId, int $sessionId, string $sessionToken, string $imagePath): DataResponse {
public function insertAttachmentFile(int $documentId, int $sessionId, string $sessionToken, string $filePath): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
$userId = $this->getUserIdFromSession($documentId, $sessionId, $sessionToken);

try {
$insertResult = $this->imageService->insertImageFile($documentId, $imagePath, $userId);
$insertResult = $this->imageService->insertAttachmentFile($documentId, $filePath, $userId);
if (isset($insertResult['error'])) {
return new DataResponse($insertResult, Http::STATUS_BAD_REQUEST);
} else {
Expand All @@ -129,29 +129,24 @@ public function insertImageFile(int $documentId, int $sessionId, string $session
* @param string|null $shareToken
* @return DataResponse
*/
public function uploadImage(int $documentId, int $sessionId, string $sessionToken, ?string $shareToken = null): DataResponse {
public function uploadAttachment(int $documentId, int $sessionId, string $sessionToken, ?string $shareToken = null): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

try {
$file = $this->getUploadedFile('image');
$file = $this->getUploadedFile('file');
if (isset($file['tmp_name'], $file['name'], $file['type'])) {
/*
if (!in_array($file['type'], self::IMAGE_MIME_TYPES, true)) {
return new DataResponse(['error' => 'Image type not supported'], Http::STATUS_BAD_REQUEST);
}
*/
$newFileResource = fopen($file['tmp_name'], 'rb');
if ($newFileResource === false) {
throw new Exception('Could not read file');
}
$newFileName = $file['name'];
if ($shareToken) {
$uploadResult = $this->imageService->uploadImagePublic($documentId, $newFileName, $newFileResource, $shareToken);
$uploadResult = $this->imageService->uploadAttachmentPublic($documentId, $newFileName, $newFileResource, $shareToken);
} else {
$userId = $this->getUserIdFromSession($documentId, $sessionId, $sessionToken);
$uploadResult = $this->imageService->uploadImage($documentId, $newFileName, $newFileResource, $userId);
$uploadResult = $this->imageService->uploadAttachment($documentId, $newFileName, $newFileResource, $userId);
}
if (isset($uploadResult['error'])) {
return new DataResponse($uploadResult, Http::STATUS_BAD_REQUEST);
Expand Down
36 changes: 12 additions & 24 deletions lib/Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace OCA\Text\Service;

use Exception;
use OCA\Text\Controller\ImageController;
use OCP\Constants;
use OCP\Files\Folder;
Expand All @@ -39,13 +38,7 @@
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IShare;
use OCP\Util;
use Throwable;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use OCP\Http\Client\IClientService;
use OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
use OCP\Share\IManager as ShareManager;

class ImageService {
Expand All @@ -58,10 +51,6 @@ class ImageService {
* @var IRootFolder
*/
private $rootFolder;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var IPreview
*/
Expand All @@ -72,13 +61,11 @@ class ImageService {
private $mimeTypeDetector;

public function __construct(IRootFolder $rootFolder,
LoggerInterface $logger,
ShareManager $shareManager,
IPreview $previewManager,
IMimeTypeDetector $mimeTypeDetector) {
$this->rootFolder = $rootFolder;
$this->shareManager = $shareManager;
$this->logger = $logger;
$this->previewManager = $previewManager;
$this->mimeTypeDetector = $mimeTypeDetector;
}
Expand Down Expand Up @@ -298,7 +285,7 @@ private function getMediaFileMetadata(string $mediaFileName, File $textFile): ?a
}

/**
* Save an uploaded image in the attachment folder
* Save an uploaded file in the attachment folder
*
* @param int $documentId
* @param string $newFileName
Expand All @@ -310,7 +297,7 @@ private function getMediaFileMetadata(string $mediaFileName, File $textFile): ?a
* @throws \OCP\Files\InvalidPathException
* @throws \OC\User\NoUserException
*/
public function uploadImage(int $documentId, string $newFileName, $newFileResource, string $userId): array {
public function uploadAttachment(int $documentId, string $newFileName, $newFileResource, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
Expand All @@ -327,7 +314,8 @@ public function uploadImage(int $documentId, string $newFileName, $newFileResour
}

/**
* Save an uploaded image in the attachment folder in a public context
* Save an uploaded file in the attachment folder in a public context
*
* @param int|null $documentId
* @param string $newFileName
* @param string $newFileContent
Expand All @@ -338,7 +326,7 @@ public function uploadImage(int $documentId, string $newFileName, $newFileResour
* @throws \OCP\Files\InvalidPathException
* @throws \OC\User\NoUserException
*/
public function uploadImagePublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
if (!$this->hasUpdatePermissions($shareToken)) {
throw new NotPermittedException('No write permissions');
}
Expand Down Expand Up @@ -366,28 +354,28 @@ public function uploadImagePublic(?int $documentId, string $newFileName, $newFil
* @throws \OCP\Files\InvalidPathException
* @throws \OC\User\NoUserException
*/
public function insertImageFile(int $documentId, string $path, string $userId): array {
public function insertAttachmentFile(int $documentId, string $path, string $userId): array {
$textFile = $this->getTextFile($documentId, $userId);
if (!$textFile->isUpdateable()) {
throw new NotPermittedException('No write permissions');
}
$imageFile = $this->getFileFromPath($path, $userId);
$originalFile = $this->getFileFromPath($path, $userId);
$saveDir = $this->getAttachmentDirectoryForFile($textFile, true);
return $this->copyImageFile($imageFile, $saveDir, $textFile);
return $this->copyFile($originalFile, $saveDir, $textFile);
}

/**
* @param File $imageFile
* @param File $originalFile
* @param Folder $saveDir
* @param File $textFile
* @return array
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
private function copyImageFile(File $imageFile, Folder $saveDir, File $textFile): array {
$fileName = $this->getUniqueFileName($saveDir, $imageFile->getName());
private function copyFile(File $originalFile, Folder $saveDir, File $textFile): array {
$fileName = $this->getUniqueFileName($saveDir, $originalFile->getName());
$targetPath = $saveDir->getPath() . '/' . $fileName;
$targetFile = $imageFile->copy($targetPath);
$targetFile = $originalFile->copy($targetPath);
return [
'name' => $fileName,
'dirname' => $saveDir->getName(),
Expand Down
14 changes: 7 additions & 7 deletions src/components/EditorMediaHandler.provider.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
export const STATE_UPLOADING = Symbol('state:uploading-state')
export const ACTION_IMAGE_PROMPT = Symbol('editor:action:image-prompt')
export const ACTION_CHOOSE_LOCAL_IMAGE = Symbol('editor:action:upload-image')
export const ACTION_ATTACHMENT_PROMPT = Symbol('editor:action:attachment-prompt')
export const ACTION_CHOOSE_LOCAL_ATTACHMENT = Symbol('editor:action:upload-attachment')

export const useUploadingStateMixin = {
inject: {
$uploadingState: {
from: STATE_UPLOADING,
default: {
isUploadingImages: false,
isUploadingAttachments: false,
},
},
},
}

export const useActionImagePromptMixin = {
export const useActionAttachmentPromptMixin = {
inject: {
$callImagePrompt: { from: ACTION_IMAGE_PROMPT, default: () => {} },
$callAttachmentPrompt: { from: ACTION_ATTACHMENT_PROMPT, default: () => {} },
},
}

export const useActionChooseLocalImageMixin = {
export const useActionChooseLocalAttachmentMixin = {
inject: {
$callChooseLocalImage: { from: ACTION_CHOOSE_LOCAL_IMAGE, default: () => {} },
$callChooseLocalAttachment: { from: ACTION_CHOOSE_LOCAL_ATTACHMENT, default: () => {} },
},
}
Loading

0 comments on commit 01e519c

Please sign in to comment.