Implement updating files

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-06-14 10:27:02 +02:00
parent c816b15bfa
commit 581fa011e3
4 changed files with 52 additions and 31 deletions

View File

@@ -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'],

View File

@@ -35,9 +35,8 @@ 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,
@@ -45,18 +44,12 @@ export default class FileService {
}
];
this.uploader.uploadItem(fileItem);
}
fileItem.headers =
{
requesttoken: oc_requesttoken,
};
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);
};

View File

@@ -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;
}

View File

@@ -94,9 +94,8 @@ 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'),
@@ -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) {