Skip to content

Commit

Permalink
Fix editor image source save and emit events
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Signed-off-by: nextcloud-command <nextcloud-command@users.noreply.github.com>
  • Loading branch information
skjnldsv authored and nextcloud-command committed Sep 20, 2022
1 parent e91c676 commit 3a5e6e5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions js/viewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-main.js.map

Large diffs are not rendered by default.

50 changes: 34 additions & 16 deletions src/components/ImageEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
<div ref="editor" class="viewer__image-editor" v-bind="themeDataAttr" />
</template>
<script>
import FilerobotImageEditor from 'filerobot-image-editor'
import { basename, dirname, extname, join } from 'path'
import client from '../services/DavClient.js'
import logger from '../services/logger.js'
import { emit } from '@nextcloud/event-bus'
import axios from '@nextcloud/axios'
import FilerobotImageEditor from 'filerobot-image-editor'
import logger from '../services/logger.js'
import translations from '../models/editorTranslations.js'
import { emit } from '@nextcloud/event-bus'
const { TABS, TOOLS } = FilerobotImageEditor
export default {
name: 'ImageEditor',
props: {
filename: {
type: String,
fileid: {
type: [String, Number],
required: true,
},
mime: {
Expand Down Expand Up @@ -142,22 +144,38 @@ export default {
},
async onSave(imageData) {
const filename = join(dirname(this.filename), imageData.fullName)
logger.debug('Saving image...', { src: this.src, filename })
const { origin, pathname } = new URL(this.src)
const putUrl = origin + join(dirname(pathname), imageData.fullName)
logger.debug('Saving image...', { putUrl, src: this.src, fullName: imageData.fullName })
try {
const b64string = imageData.imageBase64.split(';base64,').pop()
const buff = Buffer.from(b64string, 'base64')
await client.putFileContents(filename, buff, {
// @see https://github.com/perry-mitchell/webdav-client#putfilecontents
// https://github.com/perry-mitchell/webdav-client/issues/150
contentLength: false,
})
logger.info('Edited image saved!')
const file = this.dataURLtoFile(imageData.imageBase64, imageData.fullName)
const response = await axios.put(putUrl, file)
logger.info('Edited image saved!', { response })
if (putUrl !== this.src) {
emit('files:file:created', { fileid: parseInt(response?.headers?.['oc-fileid']?.split('oc')[0]) || null })
} else {
emit('files:file:updated', { fileid: this.fileid })
}
} catch (error) {
logger.error('Error saving image', { error })
}
},
dataURLtoFile(dataurl, filename) {
const arr = dataurl.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n) {
u8arr[n - 1] = bstr.charCodeAt(n - 1)
n -= 1
}
return new File([u8arr], filename, { type: mime })
},
/**
* Show warning if unsaved changes
*/
Expand Down
2 changes: 1 addition & 1 deletion src/components/Images.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<ImageEditor v-if="editing"
:mime="mime"
:src="src"
:filename="filename"
:fileid="fileid"
@close="onClose" />

<img v-else
Expand Down

0 comments on commit 3a5e6e5

Please sign in to comment.