Skip to content

Commit d10ed86

Browse files
authored
Merge pull request #1375 from nextcloud/fix/editor-source-saving
Fix editor image source save and emit events
2 parents e91c676 + 3a5e6e5 commit d10ed86

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

js/viewer-main.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/viewer-main.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/ImageEditor.vue

+34-16
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
<div ref="editor" class="viewer__image-editor" v-bind="themeDataAttr" />
33
</template>
44
<script>
5-
import FilerobotImageEditor from 'filerobot-image-editor'
65
import { basename, dirname, extname, join } from 'path'
7-
import client from '../services/DavClient.js'
8-
import logger from '../services/logger.js'
6+
import { emit } from '@nextcloud/event-bus'
7+
import axios from '@nextcloud/axios'
8+
import FilerobotImageEditor from 'filerobot-image-editor'
99
10+
import logger from '../services/logger.js'
1011
import translations from '../models/editorTranslations.js'
11-
import { emit } from '@nextcloud/event-bus'
1212
1313
const { TABS, TOOLS } = FilerobotImageEditor
1414
1515
export default {
16+
name: 'ImageEditor',
17+
1618
props: {
17-
filename: {
18-
type: String,
19+
fileid: {
20+
type: [String, Number],
1921
required: true,
2022
},
2123
mime: {
@@ -142,22 +144,38 @@ export default {
142144
},
143145
144146
async onSave(imageData) {
145-
const filename = join(dirname(this.filename), imageData.fullName)
146-
logger.debug('Saving image...', { src: this.src, filename })
147+
const { origin, pathname } = new URL(this.src)
148+
const putUrl = origin + join(dirname(pathname), imageData.fullName)
149+
logger.debug('Saving image...', { putUrl, src: this.src, fullName: imageData.fullName })
150+
147151
try {
148-
const b64string = imageData.imageBase64.split(';base64,').pop()
149-
const buff = Buffer.from(b64string, 'base64')
150-
await client.putFileContents(filename, buff, {
151-
// @see https://github.com/perry-mitchell/webdav-client#putfilecontents
152-
// https://github.com/perry-mitchell/webdav-client/issues/150
153-
contentLength: false,
154-
})
155-
logger.info('Edited image saved!')
152+
const file = this.dataURLtoFile(imageData.imageBase64, imageData.fullName)
153+
const response = await axios.put(putUrl, file)
154+
155+
logger.info('Edited image saved!', { response })
156+
if (putUrl !== this.src) {
157+
emit('files:file:created', { fileid: parseInt(response?.headers?.['oc-fileid']?.split('oc')[0]) || null })
158+
} else {
159+
emit('files:file:updated', { fileid: this.fileid })
160+
}
156161
} catch (error) {
157162
logger.error('Error saving image', { error })
158163
}
159164
},
160165
166+
dataURLtoFile(dataurl, filename) {
167+
const arr = dataurl.split(',')
168+
const mime = arr[0].match(/:(.*?);/)[1]
169+
const bstr = atob(arr[1])
170+
let n = bstr.length
171+
const u8arr = new Uint8Array(n)
172+
while (n) {
173+
u8arr[n - 1] = bstr.charCodeAt(n - 1)
174+
n -= 1
175+
}
176+
return new File([u8arr], filename, { type: mime })
177+
},
178+
161179
/**
162180
* Show warning if unsaved changes
163181
*/

src/components/Images.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ImageEditor v-if="editing"
2525
:mime="mime"
2626
:src="src"
27-
:filename="filename"
27+
:fileid="fileid"
2828
@close="onClose" />
2929

3030
<img v-else

0 commit comments

Comments
 (0)