|
2 | 2 | <div ref="editor" class="viewer__image-editor" v-bind="themeDataAttr" />
|
3 | 3 | </template>
|
4 | 4 | <script>
|
5 |
| -import FilerobotImageEditor from 'filerobot-image-editor' |
6 | 5 | 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' |
9 | 9 |
|
| 10 | +import logger from '../services/logger.js' |
10 | 11 | import translations from '../models/editorTranslations.js'
|
11 |
| -import { emit } from '@nextcloud/event-bus' |
12 | 12 |
|
13 | 13 | const { TABS, TOOLS } = FilerobotImageEditor
|
14 | 14 |
|
15 | 15 | export default {
|
| 16 | + name: 'ImageEditor', |
| 17 | +
|
16 | 18 | props: {
|
17 |
| - filename: { |
18 |
| - type: String, |
| 19 | + fileid: { |
| 20 | + type: [String, Number], |
19 | 21 | required: true,
|
20 | 22 | },
|
21 | 23 | mime: {
|
@@ -142,22 +144,38 @@ export default {
|
142 | 144 | },
|
143 | 145 |
|
144 | 146 | 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 | +
|
147 | 151 | 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 | + } |
156 | 161 | } catch (error) {
|
157 | 162 | logger.error('Error saving image', { error })
|
158 | 163 | }
|
159 | 164 | },
|
160 | 165 |
|
| 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 | +
|
161 | 179 | /**
|
162 | 180 | * Show warning if unsaved changes
|
163 | 181 | */
|
|
0 commit comments