2022-04-14 02:30:25 +02:00
|
|
|
<script setup lang="ts">
|
2023-05-28 23:13:24 +02:00
|
|
|
import { Upload } from '@vicons/tabler';
|
|
|
|
import { useBase64 } from '@vueuse/core';
|
|
|
|
import type { UploadFileInfo } from 'naive-ui';
|
2023-06-10 17:14:50 +02:00
|
|
|
import type { Ref } from 'vue';
|
2022-04-22 23:31:40 +02:00
|
|
|
import { useCopy } from '@/composable/copy';
|
2022-07-25 23:21:42 +02:00
|
|
|
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
|
2022-07-29 10:56:04 +02:00
|
|
|
import { useValidation } from '@/composable/validation';
|
2022-08-04 12:09:32 +02:00
|
|
|
import { isValidBase64 } from '@/utils/base64';
|
2022-04-14 02:30:25 +02:00
|
|
|
|
2022-07-25 23:21:42 +02:00
|
|
|
const base64Input = ref('');
|
|
|
|
const { download } = useDownloadFileFromBase64({ source: base64Input });
|
2022-07-29 10:56:04 +02:00
|
|
|
const base64InputValidation = useValidation({
|
|
|
|
source: base64Input,
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
message: 'Invalid base 64 string',
|
2023-05-28 23:13:24 +02:00
|
|
|
validator: value => isValidBase64(value.trim()),
|
2022-07-29 10:56:04 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
function downloadFile() {
|
2023-05-28 23:13:24 +02:00
|
|
|
if (!base64InputValidation.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-29 10:56:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
download();
|
2023-05-28 23:13:24 +02:00
|
|
|
}
|
|
|
|
catch (_) {
|
2022-07-29 10:56:04 +02:00
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 02:30:25 +02:00
|
|
|
|
2022-04-22 23:31:40 +02:00
|
|
|
const fileList = ref();
|
|
|
|
const fileInput = ref() as Ref<File>;
|
|
|
|
const { base64: fileBase64 } = useBase64(fileInput);
|
|
|
|
const { copy: copyFileBase64 } = useCopy({ source: fileBase64, text: 'Base64 string copied to the clipboard' });
|
2022-04-14 02:30:25 +02:00
|
|
|
|
2022-04-24 22:33:15 +02:00
|
|
|
async function onUpload({ file: { file } }: { file: UploadFileInfo }) {
|
2022-04-14 02:30:25 +02:00
|
|
|
if (file) {
|
2022-04-22 23:31:40 +02:00
|
|
|
fileList.value = [];
|
|
|
|
fileInput.value = file;
|
2022-04-14 02:30:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2023-05-28 23:13:24 +02:00
|
|
|
<template>
|
|
|
|
<c-card title="Base64 to file">
|
|
|
|
<c-input-text
|
|
|
|
v-model:value="base64Input"
|
|
|
|
multiline
|
|
|
|
placeholder="Put your base64 file string here..."
|
|
|
|
rows="5"
|
|
|
|
:validation="base64InputValidation"
|
|
|
|
mb-2
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div flex justify-center>
|
|
|
|
<c-button :disabled="base64Input === '' || !base64InputValidation.isValid" @click="downloadFile()">
|
|
|
|
Download file
|
|
|
|
</c-button>
|
|
|
|
</div>
|
|
|
|
</c-card>
|
|
|
|
|
|
|
|
<c-card title="File to base64">
|
|
|
|
<n-upload v-model:file-list="fileList" :show-file-list="true" :on-before-upload="onUpload" list-type="image">
|
|
|
|
<n-upload-dragger>
|
|
|
|
<div mb-2>
|
|
|
|
<n-icon size="35" :depth="3" :component="Upload" />
|
|
|
|
</div>
|
|
|
|
<n-text style="font-size: 14px">
|
|
|
|
Click or drag a file to this area to upload
|
|
|
|
</n-text>
|
|
|
|
</n-upload-dragger>
|
|
|
|
</n-upload>
|
|
|
|
|
|
|
|
<c-input-text :value="fileBase64" multiline readonly placeholder="File in base64 will be here" rows="5" mb-2 />
|
|
|
|
|
|
|
|
<div flex justify-center>
|
|
|
|
<c-button @click="copyFileBase64()">
|
|
|
|
Copy
|
|
|
|
</c-button>
|
|
|
|
</div>
|
|
|
|
</c-card>
|
|
|
|
</template>
|
|
|
|
|
2022-04-14 02:30:25 +02:00
|
|
|
<style lang="less" scoped>
|
|
|
|
::v-deep(.n-upload-trigger) {
|
|
|
|
width: 100%;
|
|
|
|
}
|
2022-04-22 23:31:40 +02:00
|
|
|
</style>
|