Files

316 lines
8.6 KiB
Vue
Raw Permalink Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-05-31 00:50:43 +03:00
<div>
<label
2024-07-09 23:47:46 +03:00
id="add_document_button"
2023-05-31 00:50:43 +03:00
:for="inputId"
2025-10-09 16:43:11 +03:00
class="btn btn-outline w-full add-document-button px-0"
2025-05-09 00:01:50 +03:00
:class="{ 'btn-disabled': isLoading }"
2023-05-31 00:50:43 +03:00
>
2025-10-09 16:43:11 +03:00
<div
class="flex items-center justify-between w-full h-full"
>
<span
class="flex items-center space-x-2 w-full justify-center"
:class="{ 'pl-3': withGoogleDrive }"
>
<IconInnerShadowTop
v-if="isLoading"
width="20"
class="animate-spin"
/>
<IconUpload
v-else
width="20"
/>
<span v-if="isLoading">
{{ t('uploading_') }}
</span>
<span
v-else
class="mr-1 whitespace-nowrap truncate"
>
{{ t('add_document') }}
</span>
</span>
<span
v-if="withGoogleDrive"
class="dropdown dropdown-end dropdown-top inline h-full"
style="width: 33px"
>
<label
tabindex="0"
class="flex items-center h-full cursor-pointer"
>
<IconChevronDown class="w-5 h-5 flex-shrink-0" />
</label>
<ul
tabindex="0"
:style="{ backgroundColor }"
class="dropdown-content p-2 mt-2 shadow menu text-base mb-1 rounded-box text-right !text-base-content"
>
<li>
<button
type="button"
@click="openGoogleDriveModal"
>
<IconBrandGoogleDrive class="w-5 h-5 flex-shrink-0" />
<span class="whitespace-nowrap text-sm normal-case font-medium">Google Drive</span>
</button>
</li>
</ul>
</span>
</div>
2023-05-31 00:50:43 +03:00
</label>
2026-06-04 00:35:04 +03:00
<GoogleDrivePickerModal
2025-10-09 16:43:11 +03:00
v-if="showGoogleDriveModal"
2026-06-04 00:35:04 +03:00
v-model:loading="isLoadingGoogleDrive"
:template-id="templateId"
:authenticity-token="authenticityToken"
:modal-container-el="modalContainerEl"
:reopen-after-auth="true"
@close="showGoogleDriveModal = false"
@picked="onGoogleDrivePicked"
/>
<form
ref="form"
2023-05-31 00:50:43 +03:00
class="hidden"
>
2025-10-09 16:43:11 +03:00
<input
v-for="file in googleDriveFiles"
:key="file.id"
name="google_drive_file_ids[]"
:value="file.id"
>
<input
:id="inputId"
ref="input"
name="files[]"
type="file"
2023-09-05 20:33:40 +03:00
:accept="acceptFileTypes"
multiple
2026-06-05 14:45:22 +03:00
@change="upload()"
>
</form>
2023-05-31 00:50:43 +03:00
</div>
2023-05-21 17:59:36 +03:00
</template>
<script>
2025-10-09 16:43:11 +03:00
import { IconUpload, IconInnerShadowTop, IconChevronDown, IconBrandGoogleDrive } from '@tabler/icons-vue'
2026-06-04 00:35:04 +03:00
import GoogleDrivePickerModal from './google_drive_picker_modal'
2023-05-21 17:59:36 +03:00
2026-05-21 14:50:30 +03:00
function convertImage (sourceFile, targetType, quality) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = function (event) {
const img = new Image()
img.onload = function () {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = img.width
canvas.height = img.height
ctx.drawImage(img, 0, 0)
canvas.toBlob(function (blob) {
const ext = targetType === 'image/jpeg' ? '.jpg' : '.png'
const newFile = new File([blob], sourceFile.name.replace(/\.\w+$/, ext), { type: targetType })
resolve(newFile)
}, targetType, quality)
}
img.onerror = () => reject(new Error(`browser cannot decode ${sourceFile.type || sourceFile.name}`))
img.src = event.target.result
}
reader.onerror = reject
reader.readAsDataURL(sourceFile)
})
}
2026-06-10 15:12:16 +03:00
export async function convertUnsupportedImages (files) {
const converted = []
2026-05-21 14:50:30 +03:00
2026-06-10 15:12:16 +03:00
for (const file of Array.from(files)) {
let result = file
2026-05-21 14:50:30 +03:00
try {
2026-05-23 08:25:04 +03:00
if (['image/bmp', 'image/vnd.microsoft.icon', 'image/svg+xml', 'image/gif'].includes(file.type)) {
2026-06-10 15:12:16 +03:00
result = await convertImage(file, 'image/png')
2026-05-23 08:25:04 +03:00
} else if (['image/heic', 'image/heif', 'image/heic-sequence', 'image/heif-sequence', 'image/avif', 'image/avif-sequence', 'image/webp'].includes(file.type)) {
2026-06-10 15:12:16 +03:00
result = await convertImage(file, 'image/jpeg', 0.9)
2026-05-21 14:50:30 +03:00
}
} catch (e) {
alert(e.message)
}
2026-06-10 15:12:16 +03:00
converted.push(result)
2026-05-21 14:50:30 +03:00
}
2026-06-10 15:12:16 +03:00
return converted
}
async function convertImagesInInput (input) {
if (!input.files || input.files.length === 0) return
const originals = Array.from(input.files)
const converted = await convertUnsupportedImages(originals)
if (converted.some((file, index) => file !== originals[index])) {
const dt = new DataTransfer()
converted.forEach((file) => dt.items.add(file))
2026-05-21 14:50:30 +03:00
input.files = dt.files
}
}
2023-05-21 17:59:36 +03:00
export default {
name: 'DocumentsUpload',
2023-05-31 00:50:43 +03:00
components: {
2023-07-01 00:15:15 +03:00
IconUpload,
2025-10-09 16:43:11 +03:00
IconInnerShadowTop,
IconChevronDown,
2026-06-04 00:35:04 +03:00
IconBrandGoogleDrive,
GoogleDrivePickerModal
2023-05-31 00:50:43 +03:00
},
2025-10-09 16:43:11 +03:00
inject: ['baseFetch', 't', 'backgroundColor'],
2023-05-21 17:59:36 +03:00
props: {
2023-05-31 23:19:20 +03:00
templateId: {
2023-05-21 17:59:36 +03:00
type: [Number, String],
required: true
},
2025-10-09 16:43:11 +03:00
authenticityToken: {
type: String,
required: false,
default: ''
},
withGoogleDrive: {
type: Boolean,
required: false,
default: false
},
2023-09-05 20:33:40 +03:00
acceptFileTypes: {
type: String,
required: false,
2026-03-04 12:20:52 +02:00
default: 'image/*, application/pdf, application/zip, application/json'
2023-05-21 17:59:36 +03:00
}
},
2025-04-25 21:19:28 +03:00
emits: ['success', 'error'],
2023-05-31 00:50:43 +03:00
data () {
return {
2025-10-09 16:43:11 +03:00
isLoading: false,
2026-06-04 00:35:04 +03:00
isLoadingGoogleDrive: true,
2025-10-09 16:43:11 +03:00
googleDriveFiles: [],
2026-06-04 00:35:04 +03:00
showGoogleDriveModal: false
2023-05-31 00:50:43 +03:00
}
},
computed: {
inputId () {
return 'el' + Math.random().toString(32).split('.')[1]
2025-04-25 21:19:28 +03:00
},
2025-10-09 16:43:11 +03:00
queryParams () {
return new URLSearchParams(window.location.search)
},
2025-12-26 12:08:29 +02:00
uploadUrl () {
return `/templates/${this.templateId}/documents`
},
2025-10-09 16:43:11 +03:00
modalContainerEl () {
return this.$el.getRootNode().querySelector('#docuseal_modal_container')
2023-05-31 00:50:43 +03:00
}
},
2025-10-09 16:43:11 +03:00
mounted () {
if (this.queryParams.get('google_drive_open') === '1') {
this.openGoogleDriveModal()
window.history.replaceState({}, document.title, window.location.pathname)
}
},
2023-05-21 17:59:36 +03:00
methods: {
2025-10-09 16:43:11 +03:00
openGoogleDriveModal () {
this.showGoogleDriveModal = true
this.isLoadingGoogleDrive = true
},
2026-06-04 00:35:04 +03:00
onGoogleDrivePicked (files) {
this.googleDriveFiles = files
2025-10-09 16:43:11 +03:00
2026-06-04 00:35:04 +03:00
this.$nextTick(() => {
this.upload({ path: `/templates/${this.templateId}/google_drive_documents` }).then((resp) => {
if (resp.ok) {
this.showGoogleDriveModal = false
}
}).finally(() => {
this.isLoadingGoogleDrive = false
this.googleDriveFiles = []
2025-10-09 16:43:11 +03:00
})
2026-06-04 00:35:04 +03:00
})
2025-10-09 16:43:11 +03:00
},
async upload ({ path } = {}) {
2023-05-31 00:50:43 +03:00
this.isLoading = true
2026-05-21 14:50:30 +03:00
if (this.$refs.input) {
await convertImagesInInput(this.$refs.input)
}
2025-12-26 12:08:29 +02:00
return this.baseFetch(path || this.uploadUrl, {
2024-04-06 15:00:30 +03:00
method: 'POST',
2025-04-25 21:19:28 +03:00
headers: { Accept: 'application/json' },
2024-04-06 15:00:30 +03:00
body: new FormData(this.$refs.form)
}).then((resp) => {
if (resp.ok) {
resp.json().then((data) => {
this.$emit('success', data)
2026-01-27 20:09:50 +02:00
if (this.$refs.input) {
this.$refs.input.value = ''
}
2025-05-09 00:01:50 +03:00
this.isLoading = false
2024-04-06 15:00:30 +03:00
})
} else if (resp.status === 422) {
resp.json().then((data) => {
2024-09-18 19:47:47 +03:00
if (data.status === 'pdf_encrypted') {
2024-04-06 15:00:30 +03:00
const formData = new FormData(this.$refs.form)
2023-05-21 17:59:36 +03:00
2024-09-18 19:47:47 +03:00
formData.append('password', prompt(this.t('enter_pdf_password')))
2023-05-21 17:59:36 +03:00
2025-04-25 21:19:28 +03:00
this.baseFetch(this.uploadUrl, {
2024-04-06 15:00:30 +03:00
method: 'POST',
body: formData
}).then(async (resp) => {
if (resp.ok) {
this.$emit('success', await resp.json())
this.$refs.input.value = ''
2025-05-09 00:01:50 +03:00
this.isLoading = false
} else {
2024-09-18 19:47:47 +03:00
alert(this.t('wrong_password'))
2025-04-25 21:19:28 +03:00
this.$emit('error', await resp.json().error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
}
})
2025-10-09 16:43:11 +03:00
} else if (data.status === 'google_drive_file_missing') {
alert(data.error)
this.$emit('error', data.error)
this.isLoading = false
2025-04-25 21:19:28 +03:00
} else {
this.$emit('error', data.error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
2024-04-06 15:00:30 +03:00
}
2023-05-21 17:59:36 +03:00
})
2025-04-25 21:19:28 +03:00
} else {
resp.json().then((data) => {
this.$emit('error', data.error)
2025-05-09 00:01:50 +03:00
this.isLoading = false
2025-04-25 21:19:28 +03:00
})
2024-04-06 15:00:30 +03:00
}
2025-10-09 16:43:11 +03:00
return resp
2025-05-09 00:01:50 +03:00
}).catch(() => {
2024-04-06 15:00:30 +03:00
this.isLoading = false
})
2023-05-21 17:59:36 +03:00
}
}
}
</script>