From 581fa011e3be9af6b8b85ccd8945877a9eed9436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 14 Jun 2018 10:27:02 +0200 Subject: [PATCH] Implement updating files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- appinfo/routes.php | 6 ++++-- js/service/FileService.js | 32 +++++++++++++-------------- lib/Service/AttachmentService.php | 9 ++++++++ lib/Service/FileService.php | 36 ++++++++++++++++++++----------- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index cd0da7724..cdd2b9d79 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -60,9 +60,11 @@ return [ ['name' => 'card#unassignUser', 'url' => '/cards/{cardId}/assign/{userId}', 'verb' => 'DELETE'], ['name' => 'attachment#getAll', 'url' => '/cards/{cardId}/attachments', 'verb' => 'GET'], - ['name' => 'attachment#display', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'GET'], ['name' => 'attachment#create', 'url' => '/cards/{cardId}/attachment', 'verb' => 'POST'], - ['name' => 'attachment#update', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'UPDATE'], + ['name' => 'attachment#display', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'GET'], + ['name' => 'attachment#update', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'PUT'], + // also allow to use POST for updates so we can properly access files when using application/x-www-form-urlencoded + ['name' => 'attachment#update', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'POST'], ['name' => 'attachment#delete', 'url' => '/cards/{cardId}/attachment/{attachmentId}', 'verb' => 'DELETE'], ['name' => 'attachment#restore', 'url' => '/cards/{cardId}/attachment/{attachmentId}/restore', 'verb' => 'GET'], diff --git a/js/service/FileService.js b/js/service/FileService.js index c0b26fd86..7d61b29f0 100644 --- a/js/service/FileService.js +++ b/js/service/FileService.js @@ -35,28 +35,21 @@ export default class FileService { runUpload (fileItem, attachmentId) { fileItem.url = OC.generateUrl('/apps/deck/cards/' + fileItem.cardId + '/attachment'); if (typeof attachmentId !== 'undefined') { - fileItem.method = 'UPDATE'; fileItem.url = OC.generateUrl('/apps/deck/cards/' + fileItem.cardId + '/attachment/' + attachmentId); + } else { + fileItem.formData = [ + { + requesttoken: oc_requesttoken, + type: 'deck_file', + + } + ]; } - fileItem.formData = [ + fileItem.headers = { requesttoken: oc_requesttoken, - type: 'deck_file', + }; - } - ]; - this.uploader.uploadItem(fileItem); - }; - - runUpdate (fileItem) { - fileItem.url = OC.generateUrl('/apps/deck/cards/' + fileItem.cardId + '/attachment'); - fileItem.formData = [ - { - requesttoken: oc_requesttoken, - type: 'deck_file', - - } - ]; this.uploader.uploadItem(fileItem); }; @@ -97,6 +90,11 @@ export default class FileService { }; onSuccessItem(item, response) { + let attachments = this.cardservice.get(item.cardId).attachments; + let index = attachments.indexOf(attachments.find(attachment => attachment.id === response.id)); + if (~index) { + attachments = attachments.splice(index, 1); + } this.cardservice.get(item.cardId).attachments.push(response); }; diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index aa49be651..34207d516 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -201,6 +201,15 @@ class AttachmentService { } catch (InvalidAttachmentType $e) { // just update without further action } + $attachment->setLastModified(time()); + $this->attachmentMapper->update($attachment); + // extend data so the frontend can use it properly after creating + try { + $service = $this->getService($attachment->getType()); + $service->extendData($attachment); + } catch (InvalidAttachmentType $e) { + // just store the data + } return $attachment; } diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index d5f14c4f5..b8928bfdd 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -94,23 +94,22 @@ class FileService implements IAttachmentService { return $attachment; } - public function create(Attachment $attachment) { + private function getUploadedFile () { $file = $this->request->getUploadedFile('file'); - $cardId = $attachment->getCardId(); $error = null; $phpFileUploadErrors = [ - UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), - UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), - UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), - UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), - UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), - UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), - UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), - UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), + UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), + UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), + UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), + UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), + UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), + UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), + UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), + UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), ]; if (empty($file)) { - $error = $this->l10n->t('No file uploaded'); + $error = $this->l10n->t('No file uploaded'); } if (!empty($file) && array_key_exists('error', $file) && $file['error'] !== UPLOAD_ERR_OK) { $error = $phpFileUploadErrors[$file['error']]; @@ -118,7 +117,11 @@ class FileService implements IAttachmentService { if ($error !== null) { throw new \RuntimeException($error); } + return $file; + } + public function create(Attachment $attachment) { + $file = $this->getUploadedFile(); $folder = $this->getFolder($attachment); $fileName = $file['name']; if ($folder->fileExists($fileName)) { @@ -130,9 +133,18 @@ class FileService implements IAttachmentService { $attachment->setData($fileName); } + /** + * This method requires to be used with POST so we can properly get the form data + */ public function update(Attachment $attachment) { - $file = $this->getFileForAttachment($attachment); + $file = $this->getUploadedFile(); + $fileName = $file['name']; + $attachment->setData($fileName); + $target = $this->getFileForAttachment($attachment); + $target->putContent(file_get_contents($file['tmp_name'], 'r')); + + $attachment->setLastModified(time()); } public function delete(Attachment $attachment) {