diff --git a/appinfo/routes.php b/appinfo/routes.php index e4f211c..9bb1c32 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -17,6 +17,12 @@ return ['resources' => 'url' => '/share/{noteId}', 'verb' => 'DELETE' ], + // Upload attachments + [ + 'name' => 'AttachmentApi#upload', + 'url' => '/api/v1/attachments', + 'verb' => 'POST' + ], // User Settings [ 'name' => 'settings#setUserValue', diff --git a/lib/Controller/AttachmentApiController.php b/lib/Controller/AttachmentApiController.php new file mode 100644 index 0000000..8450d11 --- /dev/null +++ b/lib/Controller/AttachmentApiController.php @@ -0,0 +1,74 @@ + + * + * @author 2016 Matias De lellis + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\QuickNotes\Controller; + +use OCP\AppFramework\ApiController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\Response; + +use OCP\IRequest; + +use OCA\QuickNotes\Service\FileService; + + +class AttachmentApiController extends ApiController { + + private $fileService; + private $userId; + + public function __construct($AppName, + IRequest $request, + FileService $fileService, + $userId) + { + parent::__construct($AppName, $request); + + $this->fileService = $fileService; + $this->userId = $userId; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function upload() { + $files = $this->request->files; + + if (count($files) !== 1) { + return new JSONResponse([],Http::STATUS_BAD_REQUEST); + } + + $file = array_pop($files); + + $fileId = $this->fileService->upload($file['name'], file_get_contents($file['tmp_name'])); + return new JSONResponse([ + 'file_id' => $fileId, + 'preview_url' => $this->fileService->getPreviewUrl($fileId, 512), + 'redirect_url' => $this->fileService->getRedirectToFileUrl($fileId) + ]); + } + +} diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 489e345..e7666cb 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -23,6 +23,8 @@ namespace OCA\QuickNotes\Service; +use OCP\AppFramework\Utility\ITimeFactory; + use OCP\IURLGenerator; use OCP\Files\File; @@ -31,6 +33,9 @@ use OCP\Files\Node; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; + +use OCA\QuickNotes\Service\SettingsService; + class FileService { /** @var string|null */ @@ -40,15 +45,25 @@ class FileService { private $rootFolder; /** @var IURLGenerator */ - protected $urlGenerator; + private $urlGenerator; + + /** @var ITimeFactory */ + private $timeFactory; + + /** @var SettingsService */ + private SettingsService $settingsService; public function __construct($userId, - IRootFolder $rootFolder, - IURLGenerator $urlGenerator) + IRootFolder $rootFolder, + IURLGenerator $urlGenerator, + ITimeFactory $timeFactory, + SettingsService $settingsService) { - $this->userId = $userId; - $this->rootFolder = $rootFolder; - $this->urlGenerator = $urlGenerator; + $this->userId = $userId; + $this->rootFolder = $rootFolder; + $this->urlGenerator = $urlGenerator; + $this->timeFactory = $timeFactory; + $this->settingsService = $settingsService; } /** @@ -88,4 +103,38 @@ class FileService { return $this->urlGenerator->linkToRoute('files.view.index', $params); } + /** + * Upload attachment and return fileId + */ + public function upload($fileName, $fileContent): int { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + + $ts = $this->timeFactory->getTime(); + $dt = new \DateTime(); + $dt->setTimestamp($ts); + + $secureFileName = $dt->format('YmdHis') . ' - ' . $fileName; + $attachmentsFolder = $this->getAttachmentsFolder(); + + $file = $attachmentsFolder->newFile($secureFileName); + $file->putContent($fileContent); + + return $file->getId(); + } + + /** + * @return \OCP\Files\Folder + */ + private function getAttachmentsFolder() { + $userFolder = $this->rootFolder->getUserFolder($this->userId); + $attachmentsFolder = $this->settingsService->getAttachmentsFolder(); + + try { + $attachmentsFolderNode = $userFolder->get($attachmentsFolder); + } catch (NotFoundException $e) { + $attachmentsFolderNode = $userFolder->newFolder($attachmentsFolder); + } + + return $attachmentsFolderNode; + } } diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index 0df0daf..763847e 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -36,6 +36,9 @@ class SettingsService { const COLOR_FOR_NEW_NOTES_KEY = 'default_color'; const DEFAULT_COLOR_FOR_NEW_NOTES = '#F7EB96'; + const ATTACHMENTS_FOLDER_KEY = 'attachments_folder'; + const DEFAULT_ATTACHMENTS_FOLDER = 'Quicknotes'; + /** @var IConfig Config */ private $config; @@ -62,4 +65,12 @@ class SettingsService { $this->config->setUserValue($this->userId, Application::APP_ID, self::COLOR_FOR_NEW_NOTES_KEY, $color); } + public function getAttachmentsFolder(): string { + return $this->config->getUserValue($this->userId, Application::APP_ID, self::ATTACHMENTS_FOLDER_KEY, self::DEFAULT_ATTACHMENTS_FOLDER); + } + + public function setAttachmentsFolder(string $folder) { + $this->config->setUserValue($this->userId, Application::APP_ID, self::ATTACHMENTS_FOLDER_KEY, $folder); + } + }