mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
feat(Base64 String Converter): handle text encoding other than utf8
Fix #1777
This commit is contained in:
1 parent
dfa836b7d4
commit
74604235ad
3 files changed
+131
-18
No files matched your search
@@ -593,6 +593,7 @@ tools:
|
||||
placeholder-the-decoded-string-will-be-here: The decoded string will be here
|
||||
tag-copy-base64: Copy base64
|
||||
tag-copy-decoded-string: Copy decoded string
|
||||
text-encoding: 'Text Encoding:'
|
||||
toml-to-yaml:
|
||||
title: TOML to YAML
|
||||
description: Parse and convert TOML to YAML.
|
||||
|
||||
@@ -3,22 +3,39 @@ import { useI18n } from 'vue-i18n';
|
||||
import { useCopy } from '@/composable/copy';
|
||||
import { base64ToText, isValidBase64, textToBase64 } from '@/utils/base64';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useITStorage, useQueryParam } from '@/composable/queryParams';
|
||||
import { useITStorage, useQueryParam, useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const encodeUrlSafe = useITStorage('base64-string-converter:encode-url-safe', false);
|
||||
const decodeUrlSafe = useITStorage('base64-string-converter:decode-url-safe', false);
|
||||
|
||||
const encoding = useQueryParamOrStorage({
|
||||
storageName: 'base64-string-converter:encoding',
|
||||
name: 'encoding',
|
||||
defaultValue: 'utf8',
|
||||
});
|
||||
|
||||
const textInput = useQueryParam({ tool: 'base64-string-converter', name: 'text', defaultValue: '' });
|
||||
const base64Output = computed(() => textToBase64(textInput.value, { makeUrlSafe: encodeUrlSafe.value }));
|
||||
const { copy: copyTextBase64 } = useCopy({ source: base64Output, text: t('tools.base64-string-converter.texts.text-base64-string-copied-to-the-clipboard') });
|
||||
const base64Output = computed(() =>
|
||||
textToBase64(textInput.value, { makeUrlSafe: encodeUrlSafe.value, encoding: encoding.value }),
|
||||
);
|
||||
const { copy: copyTextBase64 } = useCopy({
|
||||
source: base64Output,
|
||||
text: t('tools.base64-string-converter.texts.text-base64-string-copied-to-the-clipboard'),
|
||||
});
|
||||
|
||||
const base64Input = useQueryParam({ tool: 'base64-string-converter', name: 'base64', defaultValue: '' });
|
||||
const textOutput = computed(() =>
|
||||
withDefaultOnError(() => base64ToText(base64Input.value.trim(), { makeUrlSafe: decodeUrlSafe.value }), ''),
|
||||
withDefaultOnError(
|
||||
() => base64ToText(base64Input.value.trim(), { makeUrlSafe: decodeUrlSafe.value, encoding: encoding.value }),
|
||||
'',
|
||||
),
|
||||
);
|
||||
const { copy: copyText } = useCopy({ source: textOutput, text: t('tools.base64-string-converter.texts.text-string-copied-to-the-clipboard') });
|
||||
const { copy: copyText } = useCopy({
|
||||
source: textOutput,
|
||||
text: t('tools.base64-string-converter.texts.text-string-copied-to-the-clipboard'),
|
||||
});
|
||||
const b64ValidationRules = [
|
||||
{
|
||||
message: t('tools.base64-string-converter.texts.message-invalid-base64-string'),
|
||||
@@ -26,13 +43,81 @@ const b64ValidationRules = [
|
||||
},
|
||||
];
|
||||
const b64ValidationWatch = [decodeUrlSafe];
|
||||
|
||||
const encodings = [
|
||||
{ label: 'UTF-8', value: 'utf8' },
|
||||
{ label: 'UTF-8 (Alias: UTF8)', value: 'utf-8' },
|
||||
|
||||
{ label: 'UTF-16LE', value: 'utf16le' },
|
||||
{ label: 'UTF-16BE', value: 'utf16be' },
|
||||
|
||||
{ label: 'UTF-7', value: 'utf7' },
|
||||
{ label: 'UTF-7 (IMAP)', value: 'utf7imap' },
|
||||
|
||||
{ label: 'UTF-32LE', value: 'utf32le' },
|
||||
{ label: 'UTF-32BE', value: 'utf32be' },
|
||||
|
||||
{ label: 'ISO-8859-1 (Latin-1)', value: 'latin1' },
|
||||
{ label: 'ISO-8859-2 (Central European)', value: 'iso-8859-2' },
|
||||
{ label: 'ISO-8859-3 (South European)', value: 'iso-8859-3' },
|
||||
{ label: 'ISO-8859-4 (North European)', value: 'iso-8859-4' },
|
||||
{ label: 'ISO-8859-5 (Cyrillic)', value: 'iso-8859-5' },
|
||||
{ label: 'ISO-8859-6 (Arabic)', value: 'iso-8859-6' },
|
||||
{ label: 'ISO-8859-7 (Greek)', value: 'iso-8859-7' },
|
||||
{ label: 'ISO-8859-8 (Hebrew)', value: 'iso-8859-8' },
|
||||
{ label: 'ISO-8859-9 (Turkish)', value: 'iso-8859-9' },
|
||||
{ label: 'ISO-8859-10 (Nordic)', value: 'iso-8859-10' },
|
||||
{ label: 'ISO-8859-13 (Baltic)', value: 'iso-8859-13' },
|
||||
{ label: 'ISO-8859-14 (Celtic)', value: 'iso-8859-14' },
|
||||
{ label: 'ISO-8859-15 (Latin-9)', value: 'iso-8859-15' },
|
||||
{ label: 'ISO-8859-16 (Romanian)', value: 'iso-8859-16' },
|
||||
|
||||
{ label: 'Windows-1250 (Central European)', value: 'win1250' },
|
||||
{ label: 'Windows-1251 (Cyrillic)', value: 'win1251' },
|
||||
{ label: 'Windows-1252 (Western European)', value: 'win1252' },
|
||||
{ label: 'Windows-1253 (Greek)', value: 'win1253' },
|
||||
{ label: 'Windows-1254 (Turkish)', value: 'win1254' },
|
||||
{ label: 'Windows-1255 (Hebrew)', value: 'win1255' },
|
||||
{ label: 'Windows-1256 (Arabic)', value: 'win1256' },
|
||||
{ label: 'Windows-1257 (Baltic)', value: 'win1257' },
|
||||
{ label: 'Windows-1258 (Vietnamese)', value: 'win1258' },
|
||||
|
||||
{ label: 'KOI8-R (Russian)', value: 'koi8-r' },
|
||||
{ label: 'KOI8-U (Ukrainian)', value: 'koi8-u' },
|
||||
|
||||
{ label: 'GBK (Simplified Chinese)', value: 'gbk' },
|
||||
{ label: 'GB2312 (Simplified Chinese)', value: 'gb2312' },
|
||||
{ label: 'GB18030 (Simplified Chinese)', value: 'gb18030' },
|
||||
|
||||
{ label: 'Big5 (Traditional Chinese)', value: 'big5' },
|
||||
|
||||
{ label: 'Shift-JIS (Japanese)', value: 'shift_jis' },
|
||||
{ label: 'EUC-JP (Japanese)', value: 'euc-jp' },
|
||||
|
||||
{ label: 'MacRoman', value: 'macroman' },
|
||||
{ label: 'MacGreek', value: 'macgreek' },
|
||||
{ label: 'MacTurkish', value: 'macturkish' },
|
||||
{ label: 'MacCyrillic', value: 'maccyrillic' },
|
||||
{ label: 'MacCentralEurope', value: 'maccentraleurope' },
|
||||
{ label: 'MacIceland', value: 'maciceland' },
|
||||
];
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<c-card :title="t('tools.base64-string-converter.texts.title-string-to-base64')">
|
||||
<n-form-item :label="t('tools.base64-string-converter.texts.label-encode-url-safe')" label-placement="left">
|
||||
<n-switch v-model:value="encodeUrlSafe" />
|
||||
</n-form-item>
|
||||
<n-space>
|
||||
<n-form-item :label="t('tools.base64-string-converter.texts.label-encode-url-safe')" label-placement="left">
|
||||
<n-switch v-model:value="encodeUrlSafe" />
|
||||
</n-form-item>
|
||||
<c-select
|
||||
:label="t('tools.base64-string-converter.texts.text-encoding')"
|
||||
label-position="left"
|
||||
style="width: 400px"
|
||||
v-model:value="encoding"
|
||||
:options="encodings"
|
||||
searchable
|
||||
/>
|
||||
</n-space>
|
||||
<c-input-text
|
||||
v-model:value="textInput"
|
||||
multiline
|
||||
@@ -48,7 +133,9 @@ const b64ValidationWatch = [decodeUrlSafe];
|
||||
:value="base64Output"
|
||||
multiline
|
||||
readonly
|
||||
:placeholder="t('tools.base64-string-converter.texts.placeholder-the-base64-encoding-of-your-string-will-be-here')"
|
||||
:placeholder="
|
||||
t('tools.base64-string-converter.texts.placeholder-the-base64-encoding-of-your-string-will-be-here')
|
||||
"
|
||||
rows="5"
|
||||
mb-5
|
||||
/>
|
||||
@@ -61,9 +148,19 @@ const b64ValidationWatch = [decodeUrlSafe];
|
||||
</c-card>
|
||||
|
||||
<c-card :title="t('tools.base64-string-converter.texts.title-base64-to-string')">
|
||||
<n-form-item :label="t('tools.base64-string-converter.texts.label-decode-url-safe')" label-placement="left">
|
||||
<n-switch v-model:value="decodeUrlSafe" />
|
||||
</n-form-item>
|
||||
<n-space>
|
||||
<n-form-item :label="t('tools.base64-string-converter.texts.label-decode-url-safe')" label-placement="left">
|
||||
<n-switch v-model:value="decodeUrlSafe" />
|
||||
</n-form-item>
|
||||
<c-select
|
||||
:label="t('tools.base64-string-converter.texts.text-encoding')"
|
||||
label-position="left"
|
||||
style="width: 400px"
|
||||
v-model:value="encoding"
|
||||
:options="encodings"
|
||||
searchable
|
||||
/>
|
||||
</n-space>
|
||||
<c-input-text
|
||||
v-model:value="base64Input"
|
||||
multiline
|
||||
|
||||
+21
-6
@@ -1,13 +1,26 @@
|
||||
import iconv from 'iconv-lite';
|
||||
import { Base64 } from 'js-base64';
|
||||
|
||||
export { textToBase64, base64ToText, isValidBase64, removePotentialDataAndMimePrefix };
|
||||
|
||||
function textToBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
||||
function textToBase64(
|
||||
str: string,
|
||||
{ makeUrlSafe = false, encoding = 'utf8' }: { makeUrlSafe?: boolean; encoding?: string } = {},
|
||||
) {
|
||||
if (encoding !== 'utf8') {
|
||||
// Handle non-UTF-8 encoding if needed
|
||||
const buffer = iconv.encode(str, encoding);
|
||||
const encoded = Base64.fromUint8Array(buffer);
|
||||
return makeUrlSafe ? makeUriSafe(encoded) : encoded;
|
||||
}
|
||||
const encoded = Base64.encode(str);
|
||||
return makeUrlSafe ? makeUriSafe(encoded) : encoded;
|
||||
}
|
||||
|
||||
function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boolean } = {}) {
|
||||
function base64ToText(
|
||||
str: string,
|
||||
{ makeUrlSafe = false, encoding = 'utf8' }: { makeUrlSafe?: boolean; encoding?: string } = {},
|
||||
) {
|
||||
if (!isValidBase64(str, { makeUrlSafe })) {
|
||||
throw new Error('Incorrect base64 string');
|
||||
}
|
||||
@@ -18,9 +31,12 @@ function base64ToText(str: string, { makeUrlSafe = false }: { makeUrlSafe?: bool
|
||||
}
|
||||
|
||||
try {
|
||||
if (encoding !== 'utf8') {
|
||||
// Handle non-UTF-8 encoding if needed
|
||||
return iconv.decode(Base64.toUint8Array(cleanStr), encoding);
|
||||
}
|
||||
return Base64.decode(cleanStr);
|
||||
}
|
||||
catch (_) {
|
||||
} catch (_) {
|
||||
throw new Error('Incorrect base64 string');
|
||||
}
|
||||
}
|
||||
@@ -41,8 +57,7 @@ function isValidBase64(str: string, { makeUrlSafe = false }: { makeUrlSafe?: boo
|
||||
return removePotentialPadding(reEncodedBase64) === cleanStr;
|
||||
}
|
||||
return reEncodedBase64 === cleanStr.replace(/\s/g, '');
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user