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

80 lines
1.9 KiB
Vue
Raw Normal View History

2022-04-14 02:30:25 +02:00
<script setup lang="ts">
import { useBase64 } from '@vueuse/core';
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 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) {
2022-04-14 02:30:25 +02:00
if (file) {
2022-04-22 23:31:40 +02:00
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">
<c-file-upload title="Drag and drop a file here, or click to select a file" @file-upload="onUpload" />
<c-input-text :value="fileBase64" multiline readonly placeholder="File in base64 will be here" rows="5" my-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>