mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-05-08 23:25:03 -04:00
style: lint fix
This commit is contained in:
parent
dceb80d9aa
commit
b1341852e5
3 changed files with 87 additions and 83 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { test, expect } from '@playwright/test';
|
import { expect, test } from '@playwright/test';
|
||||||
|
|
||||||
test.describe('Tool - File hash', () => {
|
test.describe('Tool - File hash', () => {
|
||||||
test.beforeEach(async ({ page }) => {
|
test.beforeEach(async ({ page }) => {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { expect, describe, it } from 'vitest';
|
|
||||||
// import { } from './file-hash.service';
|
// import { } from './file-hash.service';
|
||||||
//
|
//
|
||||||
// describe('file-hash', () => {
|
// describe('file-hash', () => {
|
||||||
|
|
|
@ -1,3 +1,91 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { type Ref, ref, watch } from 'vue';
|
||||||
|
import type { UploadFileInfo } from 'naive-ui';
|
||||||
|
import { Upload } from '@vicons/tabler';
|
||||||
|
import CryptoJS from 'crypto-js';
|
||||||
|
import { useCopy } from '@/composable/copy';
|
||||||
|
|
||||||
|
type Hasher = 'md5';
|
||||||
|
type Digest = 'base64' | 'hex';
|
||||||
|
|
||||||
|
function useFileHash(target: Ref<File>, hasher: Ref<Hasher>, digest: Ref<Digest>) {
|
||||||
|
const hash = ref('');
|
||||||
|
const promise = ref() as Ref<Promise<string>>;
|
||||||
|
|
||||||
|
function execute() {
|
||||||
|
if (typeof window === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
promise.value = new Promise<string>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
if (target.value) {
|
||||||
|
resolve(readAndHashFile(target.value, hasher.value, digest.value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
promise.value.then(res => hash.value = res);
|
||||||
|
return promise.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(target, execute, { immediate: true });
|
||||||
|
watch(hasher, execute, { immediate: true });
|
||||||
|
watch(digest, execute, { immediate: true });
|
||||||
|
|
||||||
|
return {
|
||||||
|
hash,
|
||||||
|
promise,
|
||||||
|
execute,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function readAndHashFile(file: File, hashType: Hasher, digestType: Digest) {
|
||||||
|
return new Promise<string>((resolve, reject) => {
|
||||||
|
const fr = new FileReader();
|
||||||
|
fr.onload = (ev) => {
|
||||||
|
const content = ev.target!.result as string;
|
||||||
|
|
||||||
|
const hasher = {
|
||||||
|
md5: CryptoJS.MD5,
|
||||||
|
sha1: CryptoJS.SHA1,
|
||||||
|
sha256: CryptoJS.SHA256,
|
||||||
|
sha512: CryptoJS.SHA512,
|
||||||
|
sha3: CryptoJS.SHA3,
|
||||||
|
};
|
||||||
|
|
||||||
|
const digest = {
|
||||||
|
base64: CryptoJS.enc.Base64,
|
||||||
|
hex: CryptoJS.enc.Hex,
|
||||||
|
};
|
||||||
|
|
||||||
|
resolve(hasher[hashType](content).toString(digest[digestType]));
|
||||||
|
};
|
||||||
|
fr.onerror = reject;
|
||||||
|
fr.readAsBinaryString(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileList = ref();
|
||||||
|
const fileInput = ref() as Ref<File>;
|
||||||
|
|
||||||
|
const hasher = ref<Hasher>('md5');
|
||||||
|
const digest = ref<Digest>('hex');
|
||||||
|
|
||||||
|
const { hash } = useFileHash(fileInput, hasher, digest);
|
||||||
|
const { copy: copyHash } = useCopy({ source: hash, text: 'File hash copied to the clipboard' });
|
||||||
|
|
||||||
|
async function onUpload({ file: { file } }: { file: UploadFileInfo }) {
|
||||||
|
if (file) {
|
||||||
|
fileList.value = [];
|
||||||
|
fileInput.value = file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<c-card title="Calculate file hash">
|
<c-card title="Calculate file hash">
|
||||||
<n-upload v-model:file-list="fileList" :show-file-list="true" :on-before-upload="onUpload" list-type="image">
|
<n-upload v-model:file-list="fileList" :show-file-list="true" :on-before-upload="onUpload" list-type="image">
|
||||||
|
@ -12,7 +100,8 @@
|
||||||
</n-upload>
|
</n-upload>
|
||||||
|
|
||||||
<n-form-item mt-5 label="Hash algorithm:" :show-feedback="false">
|
<n-form-item mt-5 label="Hash algorithm:" :show-feedback="false">
|
||||||
<n-select mb-5 v-model:value="hasher" :options="[
|
<n-select
|
||||||
|
v-model:value="hasher" mb-5 :options="[
|
||||||
{
|
{
|
||||||
label: 'MD5',
|
label: 'MD5',
|
||||||
value: 'md5',
|
value: 'md5',
|
||||||
|
@ -33,11 +122,13 @@
|
||||||
label: 'SHA-3',
|
label: 'SHA-3',
|
||||||
value: 'sha3',
|
value: 'sha3',
|
||||||
},
|
},
|
||||||
]" />
|
]"
|
||||||
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
<n-form-item label="Digest encoding:" :show-feedback="false">
|
<n-form-item label="Digest encoding:" :show-feedback="false">
|
||||||
<n-select mb-5 v-model:value="digest" :options="[
|
<n-select
|
||||||
|
v-model:value="digest" mb-5 :options="[
|
||||||
{
|
{
|
||||||
label: 'Hexadecimal (base 16)',
|
label: 'Hexadecimal (base 16)',
|
||||||
value: 'hex',
|
value: 'hex',
|
||||||
|
@ -46,7 +137,8 @@
|
||||||
label: 'Base64 (base 64)',
|
label: 'Base64 (base 64)',
|
||||||
value: 'base64',
|
value: 'base64',
|
||||||
},
|
},
|
||||||
]" />
|
]"
|
||||||
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
<n-form-item label="Your file's hash:" :show-feedback="false">
|
<n-form-item label="Your file's hash:" :show-feedback="false">
|
||||||
|
@ -60,90 +152,3 @@
|
||||||
</div>
|
</div>
|
||||||
</c-card>
|
</c-card>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import { type Ref, ref, isRef, watch } from 'vue';
|
|
||||||
import { useCopy } from '@/composable/copy';
|
|
||||||
import type { UploadFileInfo } from 'naive-ui';
|
|
||||||
import { Upload } from '@vicons/tabler';
|
|
||||||
import CryptoJS from "crypto-js";
|
|
||||||
|
|
||||||
type Hasher = "md5";
|
|
||||||
type Digest = "base64" | "hex";
|
|
||||||
|
|
||||||
function useFileHash(target: Ref<File>, hasher: Ref<Hasher>, digest: Ref<Digest>) {
|
|
||||||
const hash = ref('');
|
|
||||||
const promise = ref() as Ref<Promise<string>>
|
|
||||||
|
|
||||||
function execute() {
|
|
||||||
if (typeof window === "undefined") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
promise.value = new Promise<string>((resolve, reject) => {
|
|
||||||
try {
|
|
||||||
if (target.value) {
|
|
||||||
resolve(readAndHashFile(target.value, hasher.value, digest.value));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
promise.value.then(res => hash.value = res)
|
|
||||||
return promise.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(target, execute, { immediate: true });
|
|
||||||
watch(hasher, execute, { immediate: true });
|
|
||||||
watch(digest, execute, { immediate: true });
|
|
||||||
|
|
||||||
return {
|
|
||||||
hash,
|
|
||||||
promise,
|
|
||||||
execute,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function readAndHashFile(file: File, hashType: Hasher, digestType: Digest) {
|
|
||||||
return new Promise<string>((resolve, reject) => {
|
|
||||||
const fr = new FileReader();
|
|
||||||
fr.onload = (ev) => {
|
|
||||||
const content = ev.target!.result as string;
|
|
||||||
|
|
||||||
const hasher = {
|
|
||||||
md5: CryptoJS.MD5,
|
|
||||||
sha1: CryptoJS.SHA1,
|
|
||||||
sha256: CryptoJS.SHA256,
|
|
||||||
sha512: CryptoJS.SHA512,
|
|
||||||
sha3: CryptoJS.SHA3,
|
|
||||||
}
|
|
||||||
|
|
||||||
const digest = {
|
|
||||||
base64: CryptoJS.enc.Base64,
|
|
||||||
hex: CryptoJS.enc.Hex
|
|
||||||
};
|
|
||||||
|
|
||||||
resolve(hasher[hashType](content).toString(digest[digestType]));
|
|
||||||
};
|
|
||||||
fr.onerror = reject;
|
|
||||||
fr.readAsBinaryString(file);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileList = ref();
|
|
||||||
const fileInput = ref() as Ref<File>;
|
|
||||||
|
|
||||||
const hasher = ref<Hasher>("md5");
|
|
||||||
const digest = ref<Digest>("hex");
|
|
||||||
|
|
||||||
const { hash } = useFileHash(fileInput, hasher, digest);
|
|
||||||
const { copy: copyHash } = useCopy({ source: hash, text: 'File hash copied to the clipboard' });
|
|
||||||
|
|
||||||
async function onUpload({ file: { file } }: { file: UploadFileInfo }) {
|
|
||||||
if (file) {
|
|
||||||
fileList.value = [];
|
|
||||||
fileInput.value = file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue