-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Refactor documents related components #904
- Loading branch information
Showing
6 changed files
with
259 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<template> | ||
<q-file | ||
v-model="files" | ||
:filter="checkFilesSize" | ||
:label="$t('KUploader.ADD_FILES')" | ||
:disable="uploading" | ||
rounded | ||
outlined | ||
use-chips | ||
multiple | ||
append | ||
clearable | ||
dense | ||
@update:model-value="onUpdated" | ||
/> | ||
</template> | ||
|
||
<script setup> | ||
import _ from 'lodash' | ||
import logger from 'loglevel' | ||
import { ref } from 'vue' | ||
import { Notify } from 'quasar' | ||
import { Storage } from '../../storage.js' | ||
import { i18n } from '../../i18n.js' | ||
// Props | ||
const props = defineProps({ | ||
path: { | ||
type: String, | ||
default: undefined | ||
}, | ||
fileTypes: { | ||
type: String, | ||
default: undefined | ||
}, | ||
maxFileSize: { | ||
type: Number, | ||
default: 52428800 | ||
} | ||
}) | ||
// Emits | ||
const emit = defineEmits(['files-updated', 'files-uploaded']) | ||
// Data | ||
const files = ref([]) | ||
const uploading = ref(false) | ||
const bToMb = 0.00000095367432 | ||
// Functions | ||
function checkFilesSize (files) { | ||
return _.filter(files, file => { | ||
if (file.size === 0) { | ||
Notify.create({ type: 'negative', message: i18n.t('KUploader.EMPTY_FILE', { file: file.name })}) | ||
return false | ||
} | ||
if (file.size > props.maxFileSize) { | ||
Notify.create({ type: 'negative', message: i18n.t('KUploader.FILE_TOO_LARGE', { file: file.name, size: Math.trunc(props.maxFileSize * bToMb) }) }) | ||
return false | ||
} | ||
return true | ||
}) | ||
} | ||
async function upload (path) { | ||
uploading.value = true | ||
// compute the path: first use path argument, second use path prop | ||
if (!path) path = _.isEmpty(props.path) ? '' : `${props.path}` | ||
// upload the files | ||
for (const file of files.value) { | ||
const key = _.isEmpty(path) ? file.name : `${path}/${file.name}` | ||
logger.debug(`[KDK] Uploading file ${file.name} with key ${key}`) | ||
Storage.upload({ | ||
file: file.name, | ||
type: file.type, | ||
key, | ||
blob: file | ||
}) | ||
.then(() => { | ||
Notify.create({ | ||
type: 'positive', | ||
message: i18n.t('KUploader.UPLOAD_FILE_SUCCEEDED', | ||
{ file: file.name }) | ||
}) | ||
}) | ||
.catch(error => { | ||
logger.error('[KDK] Uploading file ${file.name} failed:', error) | ||
Notify.create({ | ||
type: 'negative', | ||
message: i18n.t('KUploader.UPLOAD_FILE_ERRORED', | ||
{ file: file.name }) | ||
}) | ||
}) | ||
} | ||
uploading.value = false | ||
emit('files-uploaded', files.value) | ||
// clear the files | ||
files.value = [] | ||
onUpdated([]) | ||
return true | ||
} | ||
function onUpdated (value) { | ||
const files = _.map(value, file => { | ||
return { | ||
name: file.name, | ||
type: file.type | ||
} | ||
}) | ||
emit('files-updated', files) | ||
} | ||
// Expose | ||
defineExpose({ | ||
upload | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.