update attachment

Signed-off-by: Jakob Röhrl <jakob.roehrl@web.de>
This commit is contained in:
Jakob Röhrl
2020-02-21 12:08:24 +01:00
committed by Julius Härtl
parent 1e3ff41cb2
commit 725f99d8b8
6 changed files with 95 additions and 10 deletions

View File

@@ -21,16 +21,24 @@
* *
*/ */
namespace OCA\Deck; namespace OCA\Deck\Exceptions;
use OCA\Deck\StatusException;
class ConflictException extends \Exception { class ConflictException extends StatusException {
public function __construct($message) { private $data;
public function __construct($message, $data = null) {
parent::__construct($message); parent::__construct($message);
$this->data = $data;
} }
public function getStatus() { public function getStatus() {
return 409; return 409;
} }
public function getData() {
return $this->data;
}
} }

View File

@@ -25,6 +25,7 @@ namespace OCA\Deck\Middleware;
use OCA\Deck\Controller\PageController; use OCA\Deck\Controller\PageController;
use OCA\Deck\StatusException; use OCA\Deck\StatusException;
use OCA\Deck\Exceptions\ConflictException;
use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Middleware; use OCP\AppFramework\Middleware;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
@@ -63,6 +64,17 @@ class ExceptionMiddleware extends Middleware {
* @throws \Exception * @throws \Exception
*/ */
public function afterException($controller, $methodName, \Exception $exception) { public function afterException($controller, $methodName, \Exception $exception) {
if ($exception instanceof ConflictException) {
if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
$this->logger->logException($exception);
}
return new JSONResponse([
'status' => $exception->getStatus(),
'message' => $exception->getMessage(),
'data' => $exception->getData(),
], $exception->getStatus());
}
if ($exception instanceof StatusException) { if ($exception instanceof StatusException) {
if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
$this->logger->logException($exception); $this->logger->logException($exception);

View File

@@ -26,6 +26,7 @@ namespace OCA\Deck\Service;
use OC\Security\CSP\ContentSecurityPolicyManager; use OC\Security\CSP\ContentSecurityPolicyManager;
use OCA\Deck\Db\Attachment; use OCA\Deck\Db\Attachment;
use OCA\Deck\StatusException; use OCA\Deck\StatusException;
use OCA\Deck\Exceptions\ConflictException;
use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\EmptyContentSecurityPolicy; use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse;
@@ -42,7 +43,7 @@ use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
use OCP\Deck\ConflictException;
class FileService implements IAttachmentService { class FileService implements IAttachmentService {
@@ -154,7 +155,7 @@ class FileService implements IAttachmentService {
$folder = $this->getFolder($attachment); $folder = $this->getFolder($attachment);
$fileName = $file['name']; $fileName = $file['name'];
if ($folder->fileExists($fileName)) { if ($folder->fileExists($fileName)) {
throw new ConflictException('File already exists.'); throw new ConflictException('File already exists.', $attachment);
} }
$target = $folder->newFile($fileName); $target = $folder->newFile($fileName);

View File

@@ -45,17 +45,38 @@
type="file" type="file"
style="display: none;" style="display: none;"
@change="onLocalAttachmentSelected"> @change="onLocalAttachmentSelected">
<Modal v-if="modalShow" title="File already exists" @close="modalShow=false">
<div class="modal__content">
<h2>{{ t('deck', 'File already exists') }}</h2>
<p>
{{ t('deck', 'A file with the name') }}
{{ file.name }}
{{ t('deck', 'already exists.') }}
</p>
<p>
{{ t('deck', 'Do you want to overwrite it?') }}
</p>
<button class="primary" @click="overrideAttachment">
{{ t('deck', 'Yes') }}
</button>
<button @click="modalShow=false">
{{ t('deck', 'No') }}
</button>
</div>
</Modal>
</div> </div>
</template> </template>
<script> <script>
import { Actions, ActionButton } from '@nextcloud/vue' import { Actions, ActionButton, Modal } from '@nextcloud/vue'
export default { export default {
name: 'CardSidebarTabAttachments', name: 'CardSidebarTabAttachments',
components: { components: {
Actions, Actions,
ActionButton, ActionButton,
Modal,
}, },
props: { props: {
card: { card: {
@@ -65,6 +86,9 @@ export default {
}, },
data() { data() {
return { return {
modalShow: false,
file: '',
overrideError: null
} }
}, },
computed: { computed: {
@@ -85,11 +109,12 @@ export default {
bodyFormData.append('cardId', this.card.id) bodyFormData.append('cardId', this.card.id)
bodyFormData.append('type', 'deck_file') bodyFormData.append('type', 'deck_file')
bodyFormData.append('file', e.target.files[0]) bodyFormData.append('file', e.target.files[0])
this.file = e.target.files[0]
try { try {
await this.$store.dispatch('createAttachment', { cardId: this.card.id, formData: bodyFormData }) const data = await this.$store.dispatch('createAttachment', { cardId: this.card.id, formData: bodyFormData })
console.log(data)
} catch (e) { } catch (e) {
console.log("doppelt") this.modalShow = true
console.log(e)
} }
}, },
deleteAttachment(attachment) { deleteAttachment(attachment) {
@@ -105,6 +130,15 @@ export default {
attachmentUrl(attachment) { attachmentUrl(attachment) {
return OC.generateUrl(`/apps/deck/cards/${attachment.cardId}/attachment/${attachment.id}`) return OC.generateUrl(`/apps/deck/cards/${attachment.cardId}/attachment/${attachment.id}`)
}, },
overrideAttachment() {
const bodyFormData = new FormData()
bodyFormData.append('cardId', this.card.id)
bodyFormData.append('type', 'deck_file')
bodyFormData.append('file', this.file)
this.$store.dispatch('updateAttachment', { cardId: this.card.id, attachmentId: 1, formData: bodyFormData })
this.modalShow = false
},
}, },
} }
</script> </script>
@@ -117,4 +151,16 @@ export default {
height: 32px; height: 32px;
background-size: contain; background-size: contain;
} }
.modal__content {
width: 25vw;
min-width: 250px;
height: 120px;
text-align: center;
margin: 20px 20px 60px 20px;
}
.modal__content button {
float: right;
margin: 40px 3px 3px 0;
}
</style> </style>

View File

@@ -38,7 +38,7 @@ export class AttachmentApi {
} }
async createAttachment({ cardId, formData }) { async createAttachment({ cardId, formData }) {
const response = await axios({ const response = await axios({
method: 'POST', method: 'POST',
url: this.url(`/cards/${cardId}/attachment`), url: this.url(`/cards/${cardId}/attachment`),
data: formData, data: formData,
@@ -46,6 +46,15 @@ export class AttachmentApi {
return response.data return response.data
} }
async updateAttachment({ cardId, attachmentId, formData }) {
const response = await axios({
method: 'PUT',
url: this.url(`/cards/${cardId}/attachment/${attachmentId}`),
data: formData,
})
return response.data
}
async deleteAttachment(attachment) { async deleteAttachment(attachment) {
await axios({ await axios({
method: 'DELETE', method: 'DELETE',

View File

@@ -47,6 +47,10 @@ export default {
} }
}, },
updateAttachment(state, { cardId, attachment }) {
Vue.set(state.attachments, cardId, attachment)
},
deleteAttachment(state, deletedAttachment) { deleteAttachment(state, deletedAttachment) {
const existingIndex = state.attachments[deletedAttachment.cardId].findIndex(a => a.id === deletedAttachment.id) const existingIndex = state.attachments[deletedAttachment.cardId].findIndex(a => a.id === deletedAttachment.id)
if (existingIndex !== -1) { if (existingIndex !== -1) {
@@ -67,6 +71,11 @@ export default {
commit('cardIncreaseAttachmentCount', cardId) commit('cardIncreaseAttachmentCount', cardId)
}, },
async updateAttachment({ commit }, { cardId, attachmentId, formData }) {
const attachment = await apiClient.updateAttachment({ cardId, attachmentId, formData })
commit('updateAttachment', { cardId, attachment })
},
async deleteAttachment({ commit }, attachment) { async deleteAttachment({ commit }, attachment) {
await apiClient.deleteAttachment(attachment) await apiClient.deleteAttachment(attachment)
commit('deleteAttachment', attachment) commit('deleteAttachment', attachment)