Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix editor image source save and emit events #1375

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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