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-21 17:12:20 +0300
committerJulien Veyssier <eneiluj@posteo.net>2022-01-03 12:27:34 +0300
commita103b9834596abaecf21584ff08543a9ee4315db (patch)
tree8df0eae96882d656c41d9d6bb00831fa625326db /lib
parentcb463ead759e40311b7b4154996a2790ea692db1 (diff)
upload image file via Text API call for more flexibility in saved file location
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ImageController.php25
-rw-r--r--lib/Service/ImageService.php26
2 files changed, 48 insertions, 3 deletions
diff --git a/lib/Controller/ImageController.php b/lib/Controller/ImageController.php
index d1371e464..942967415 100644
--- a/lib/Controller/ImageController.php
+++ b/lib/Controller/ImageController.php
@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\Text\Controller;
+use Exception;
use OCP\AppFramework\Http;
use OCA\Text\Service\ImageService;
use OCP\AppFramework\Controller;
@@ -49,17 +50,37 @@ class ImageController extends Controller {
parent::__construct($appName, $request);
$this->userId = $userId;
$this->imageService = $imageService;
+ $this->request = $request;
}
/**
* @NoAdminRequired
*/
- public function downloadImageLink(string $link): DataResponse {
- $downloadResult = $this->imageService->downloadImageLink($link, $this->userId);
+ public function insertImageLink(string $link): DataResponse {
+ $downloadResult = $this->imageService->insertImageLink($link, $this->userId);
if (isset($downloadResult['error'])) {
return new DataResponse($downloadResult, Http::STATUS_BAD_REQUEST);
} else {
return new DataResponse($downloadResult);
}
}
+
+ /**
+ * @NoAdminRequired
+ */
+ public function uploadImage(string $textFilePath): 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);
+ return new DataResponse($uploadResult);
+ } else {
+ return new DataResponse(['error' => 'No uploaded file'], Http::STATUS_BAD_REQUEST);
+ }
+ } catch (Exception $e) {
+ return new DataResponse(['error' => 'Upload error'], Http::STATUS_BAD_REQUEST);
+ }
+ }
}
diff --git a/lib/Service/ImageService.php b/lib/Service/ImageService.php
index c261b4925..f0a4a6fe2 100644
--- a/lib/Service/ImageService.php
+++ b/lib/Service/ImageService.php
@@ -70,10 +70,34 @@ class ImageService {
}
/**
+ * @param string $textFilePath
+ * @param string $newFileName
+ * @param string $newFileContent
+ * @param string $userId
+ * @return array
+ */
+ public function uploadImage(string $textFilePath, string $newFileName, string $newFileContent, string $userId): array {
+ $fileName = (string) time() . '-' . $newFileName;
+ $saveDir = $this->getOrCreateTextDirectory($userId);
+ if ($saveDir !== null) {
+ $savedFile = $saveDir->newFile($fileName, $newFileContent);
+ $path = preg_replace('/^files/', '', $savedFile->getInternalPath());
+ return [
+ 'name' => $fileName,
+ 'path' => $path,
+ ];
+ } else {
+ return [
+ 'error' => 'Impossible to create /Text directory',
+ ];
+ }
+ }
+
+ /**
* @param string $link
* @return array
*/
- public function downloadImageLink(string $link, string $userId): array {
+ public function insertImageLink(string $link, string $userId): array {
$fileName = (string) time();
$saveDir = $this->getOrCreateTextDirectory($userId);
if ($saveDir !== null) {