it-tools/src/tools/base64-file-converter/base64-file-converter.vue

94 lines
2.4 KiB
Vue
Raw Normal View History

2022-04-14 02:30:25 +02:00
<script setup lang="ts">
import { Upload } from '@vicons/tabler';
import { useBase64 } from '@vueuse/core';
import type { UploadFileInfo } from 'naive-ui';
import type { Ref } from 'vue';
2022-04-22 23:31:40 +02:00
import { useCopy } from '@/composable/copy';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
import { useValidation } from '@/composable/validation';
import { isValidBase64 } from '@/utils/base64';
2022-04-14 02:30:25 +02:00
const base64Input = ref('');
const { download } = useDownloadFileFromBase64({ source: base64Input });
const base64InputValidation = useValidation({
source: base64Input,
rules: [
{
message: 'Invalid base 64 string',
validator: value => isValidBase64(value.trim()),
},
],
});
function downloadFile() {
if (!base64InputValidation.isValid) {
return;
}
try {
download();
}
catch (_) {
//
}
}
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
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>
<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>