Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulien Veyssier <eneiluj@posteo.net>2021-10-22 17:07:07 +0300
committerJulien Veyssier <eneiluj@posteo.net>2022-01-03 12:27:34 +0300
commit7267f0b71d931ee9c464c1607cf9f5ed7a1a7e87 (patch)
treeaca70e1ca1ede9880f690a7a99162c65e69c0971 /lib
parenta103b9834596abaecf21584ff08543a9ee4315db (diff)
display/upload/add-link works with attachment folder in user context
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ImageController.php23
-rw-r--r--lib/Service/ImageService.php91
2 files changed, 104 insertions, 10 deletions
diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php
index 942967415..91bafa925 100644
--- a/lib/Controller/ImageController.php
+++ b/lib/Controller/ImageController.php
@@ -29,6 +29,7 @@ use Exception;
use OCP\AppFramework\Http;
use OCA\Text\Service\ImageService;
use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
@@ -56,8 +57,8 @@ class ImageController extends Controller {
/**
* @NoAdminRequired
*/
- public function insertImageLink(string $link): DataResponse {
- $downloadResult = $this->imageService->insertImageLink($link, $this->userId);
+ public function insertImageLink(int $textFileId, string $link): DataResponse {
+ $downloadResult = $this->imageService->insertImageLink($textFileId, $link, $this->userId);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
@@ -68,13 +69,13 @@ class ImageController extends Controller {
/**
* @NoAdminRequired
*/
- public function uploadImage(string $textFilePath): DataResponse {
+ public function uploadImage(int $textFileId): DataResponse {
try {
$file = $this->request->getUploadedFile('image');
if ($file !== null && isset($file['tmp_name'], $file['name'])) {
$newFileContent = file_get_contents($file['tmp_name']);
$newFileName = $file['name'];
- $uploadResult = $this->imageService->uploadImage($textFilePath, $newFileName, $newFileContent, $this->userId);
+ $uploadResult = $this->imageService->uploadImage($textFileId, $newFileName, $newFileContent, $this->userId);
return new DataResponse($uploadResult);
} else {
return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
@@ -83,4 +84,18 @@ class ImageController extends Controller {
return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
}
}
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ */
+ public function getImage(int $textFileId, int $imageFileId): DataDisplayResponse {
+ $imageContent = $this->imageService->getImage($textFileId, $imageFileId, $this->userId);
+ if ($imageContent !== null) {
+ return new DataDisplayResponse($imageContent);
+ } else {
+ error_log('image not found response');
+ return new DataDisplayResponse('', Http::STATUS_NOT_FOUND);
+ }
+ }
}
diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php
index f0a4a6fe2..3075ef5b8 100644
--- a/lib/Service/ImageService.php
+++ b/lib/Service/ImageService.php
@@ -28,6 +28,7 @@ namespace OCA\Text\Service;
use Exception;
use OCP\Files\Folder;
+use OCP\Files\File;
use Throwable;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
@@ -70,36 +71,78 @@ class ImageService {
}
/**
- * @param string $textFilePath
+ * @param int $textFileId
+ * @param int $imageFileId
+ * @param string $userId
+ * @return string|null
+ * @throws \OCP\Files\NotFoundException
+ * @throws \OCP\Files\NotPermittedException
+ * @throws \OCP\Lock\LockedException
+ * @throws \OC\User\NoUserException
+ */
+ public function getImage(int $textFileId, int $imageFileId, string $userId): ?string {
+ error_log('ImageService::getImage');
+ $attachmentFolder = $this->getOrCreateAttachmentDirectory($textFileId, $userId);
+ if ($attachmentFolder !== null) {
+ error_log('ATT OK');
+ $imageFile = $attachmentFolder->getById($imageFileId);
+ if (count($imageFile) > 0) {
+ error_log('IMG file found');
+ $imageFile = $imageFile[0];
+ if ($imageFile instanceof File) {
+ error_log('IMG IS FILE');
+ return $imageFile->getContent();
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @param int $textFileId
* @param string $newFileName
* @param string $newFileContent
* @param string $userId
* @return array
+ * @throws \OCP\Files\InvalidPathException
+ * @throws \OCP\Files\NotFoundException
+ * @throws \OCP\Files\NotPermittedException
+ * @throws \OC\User\NoUserException
*/
- public function uploadImage(string $textFilePath, string $newFileName, string $newFileContent, string $userId): array {
+ public function uploadImage(int $textFileId, string $newFileName, string $newFileContent, string $userId): array {
$fileName = (string) time() . '-' . $newFileName;
- $saveDir = $this->getOrCreateTextDirectory($userId);
+ // $saveDir = $this->getOrCreateTextDirectory($userId);
+ $saveDir = $this->getOrCreateAttachmentDirectory($textFileId, $userId);
if ($saveDir !== null) {
$savedFile = $saveDir->newFile($fileName, $newFileContent);
$path = preg_replace('/^files/', '', $savedFile->getInternalPath());
return [
'name' => $fileName,
'path' => $path,
+ 'id' => $savedFile->getId(),
];
} else {
return [
- 'error' => 'Impossible to create /Text directory',
+ 'error' => 'Impossible to get attachment directory',
];
}
}
/**
+ * @param string $textFilePath
* @param string $link
+ * @param string $userId
* @return array
+ * @throws \OCP\Files\InvalidPathException
+ * @throws \OCP\Files\NotFoundException
+ * @throws \OCP\Files\NotPermittedException
+ * @throws \OCP\Lock\LockedException
+ * @throws \OC\User\NoUserException
*/
- public function insertImageLink(string $link, string $userId): array {
+ public function insertImageLink(int $textFileId, string $link, string $userId): array {
$fileName = (string) time();
- $saveDir = $this->getOrCreateTextDirectory($userId);
+ // $saveDir = $this->getOrCreateTextDirectory($userId);
+ $saveDir = $this->getOrCreateAttachmentDirectory($textFileId, $userId);
if ($saveDir !== null) {
$savedFile = $saveDir->newFile($fileName);
$resource = $savedFile->fopen('w');
@@ -125,6 +168,7 @@ class ImageService {
return [
'name' => $fileName,
'path' => $path,
+ 'id' => $savedFile->getId(),
];
} else {
return $res;
@@ -151,6 +195,41 @@ class ImageService {
}
}
+ /**
+ * @param int $textFileId
+ * @param string $userId
+ * @return Folder|null
+ * @throws \OCP\Files\InvalidPathException
+ * @throws \OCP\Files\NotFoundException
+ * @throws \OCP\Files\NotPermittedException
+ * @throws \OC\User\NoUserException
+ */
+ private function getOrCreateAttachmentDirectory(int $textFileId, string $userId): ?Folder {
+ $userFolder = $this->rootFolder->getUserFolder($userId);
+ $textFile = $userFolder->getById($textFileId);
+ if (count($textFile) > 0 && $textFile[0] instanceof File) {
+ $textFile = $textFile[0];
+ $owner = $textFile->getOwner();
+ $ownerId = $owner->getUID();
+ $ownerUserFolder = $this->rootFolder->getUserFolder($ownerId);
+ $ownerTextFile = $ownerUserFolder->getById($textFile->getId());
+ if (count($ownerTextFile) > 0) {
+ $ownerTextFile = $ownerTextFile[0];
+ $ownerParentFolder = $ownerTextFile->getParent();
+ $attachmentFolderName = '.' . $textFile->getId();
+ if ($ownerParentFolder->nodeExists($attachmentFolderName)) {
+ $attachmentFolder = $ownerParentFolder->get($attachmentFolderName);
+ if ($attachmentFolder instanceof Folder) {
+ return $attachmentFolder;
+ }
+ } else {
+ return $ownerParentFolder->newFolder($attachmentFolderName);
+ }
+ }
+ }
+ return null;
+ }
+
private function simpleDownload(string $url, $resource, array $params = [], string $method = 'GET'): array {
$client = $this->clientService->newClient();
try {