diff options
Diffstat (limited to 'src/tools/base64-file-converter/base64-file-converter.vue')
-rw-r--r-- | src/tools/base64-file-converter/base64-file-converter.vue | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/tools/base64-file-converter/base64-file-converter.vue b/src/tools/base64-file-converter/base64-file-converter.vue index fcb6870..4cdff0f 100644 --- a/src/tools/base64-file-converter/base64-file-converter.vue +++ b/src/tools/base64-file-converter/base64-file-converter.vue @@ -1,8 +1,16 @@ <template> <n-card title="Base64 to file"> - <n-input v-model:value="base64Input" type="textarea" placeholder="Put your base64 file string here..." rows="5" /> + <n-form-item + :feedback="base64InputValidation.message" + :validation-status="base64InputValidation.status" + :show-label="false" + > + <n-input v-model:value="base64Input" type="textarea" placeholder="Put your base64 file string here..." rows="5" /> + </n-form-item> <n-space justify="center"> - <n-button secondary @click="download()"> Download file </n-button> + <n-button :disabled="base64Input === '' || !base64InputValidation.isValid" secondary @click="downloadFile()"> + Download file + </n-button> </n-space> </n-card> @@ -26,6 +34,7 @@ <script setup lang="ts"> import { useCopy } from '@/composable/copy'; import { useDownloadFileFromBase64 } from '@/composable/downloadBase64'; +import { useValidation } from '@/composable/validation'; import { Upload } from '@vicons/tabler'; import { useBase64 } from '@vueuse/core'; import type { UploadFileInfo } from 'naive-ui'; @@ -33,6 +42,25 @@ import { ref, type Ref } from 'vue'; const base64Input = ref(''); const { download } = useDownloadFileFromBase64({ source: base64Input }); +const base64InputValidation = useValidation({ + source: base64Input, + rules: [ + { + message: 'Invalid base 64 string', + validator: (value) => window.atob(value.replace(/^data:.*?;base64,/, '')), + }, + ], +}); + +function downloadFile() { + if (!base64InputValidation.isValid) return; + + try { + download(); + } catch (_) { + // + } +} const fileList = ref(); const fileInput = ref() as Ref<File>; |