mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 07:11:03 -04:00
fix(base64-file-converter): fix downloading of index.html content without data preambula
This commit is contained in:
parent
ca43a25569
commit
f7f6f5d77f
1 changed files with 10 additions and 9 deletions
|
@ -1,15 +1,13 @@
|
||||||
import { extension as getExtensionFromMime } from 'mime-types';
|
import { extension as getExtensionFromMime } from 'mime-types';
|
||||||
import type { Ref } from 'vue';
|
import type { Ref } from 'vue';
|
||||||
|
|
||||||
function getFileExtensionFromBase64({
|
function getFileExtensionFromMime({
|
||||||
base64String,
|
hasMimeType,
|
||||||
defaultExtension = 'txt',
|
defaultExtension = 'txt',
|
||||||
}: {
|
}: {
|
||||||
base64String: string
|
hasMimeType: string[] | null
|
||||||
defaultExtension?: string
|
defaultExtension?: string
|
||||||
}) {
|
}) {
|
||||||
const hasMimeType = base64String.match(/data:(.*?);base64/i);
|
|
||||||
|
|
||||||
if (hasMimeType) {
|
if (hasMimeType) {
|
||||||
return getExtensionFromMime(hasMimeType[1]) || defaultExtension;
|
return getExtensionFromMime(hasMimeType[1]) || defaultExtension;
|
||||||
}
|
}
|
||||||
|
@ -20,13 +18,16 @@ function getFileExtensionFromBase64({
|
||||||
export function useDownloadFileFromBase64({ source, filename }: { source: Ref<string>; filename?: string }) {
|
export function useDownloadFileFromBase64({ source, filename }: { source: Ref<string>; filename?: string }) {
|
||||||
return {
|
return {
|
||||||
download() {
|
download() {
|
||||||
const base64String = source.value;
|
if (source.value === '') {
|
||||||
|
|
||||||
if (base64String === '') {
|
|
||||||
throw new Error('Base64 string is empty');
|
throw new Error('Base64 string is empty');
|
||||||
}
|
}
|
||||||
|
|
||||||
const cleanFileName = filename ?? `file.${getFileExtensionFromBase64({ base64String })}`;
|
const hasMimeType = source.value.match(/data:(.*?);base64/i);
|
||||||
|
const base64String = hasMimeType
|
||||||
|
? source.value
|
||||||
|
: `data:text/plain;base64,${source.value}`;
|
||||||
|
|
||||||
|
const cleanFileName = filename ?? `file.${getFileExtensionFromMime({ hasMimeType })}`;
|
||||||
|
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
a.href = base64String;
|
a.href = base64String;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue