diff --git a/src/command/sync/modules/base.ts b/src/command/sync/modules/base.ts index 91aa44e..7a10e10 100644 --- a/src/command/sync/modules/base.ts +++ b/src/command/sync/modules/base.ts @@ -1,6 +1,6 @@ import path from 'node:path'; import fs from 'node:fs'; -import { Config, SyncList, SyncConfigType, SyncConfig } from './type.ts'; +import { Config, SyncList, SyncConfigType } from './type.ts'; import { fileIsExist } from '@/uitls/file.ts'; import { getHash } from '@/uitls/hash.ts'; import glob from 'fast-glob'; diff --git a/src/command/sync/sync.ts b/src/command/sync/sync.ts index 89615f6..ca4fa79 100644 --- a/src/command/sync/sync.ts +++ b/src/command/sync/sync.ts @@ -56,12 +56,12 @@ const syncUpload = new Command('upload') }); if (res.code === 200) { if (res.data?.isNew) { - newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('文件上传')]); + newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('文件上传'), chalk.yellow(`hash: ${res.data.hash}`)]); } else if (res.data?.isNewMeta) { - newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('元数据更新')]); + newInfos.push(['上传成功', item.key, chalk.green(item.url), chalk.green('元数据更新'), chalk.yellow(`hash: ${res.data.hash}`)]); } else { // 文件未更新 - logger.info('上传成功', item.key, chalk.green(item.url), chalk.blue('文件未更新')); + logger.debug('上传成功', item.key, chalk.green(item.url), chalk.blue('文件未更新'), chalk.yellow(`hash: ${res.data.hash}`)); } } logger.debug(res); diff --git a/src/module/download/upload.ts b/src/module/download/upload.ts index 03fab64..8b028b5 100644 --- a/src/module/download/upload.ts +++ b/src/module/download/upload.ts @@ -79,12 +79,15 @@ export const upload = (opts: UploadOptions): Promise<{ code?: number; message?: value = opts.file; } form.append('file', value); + const fileSize = Buffer.byteLength(value); if (opts.needHash) { hash = opts?.hash || getBufferHash(value); opts.url = new URL(opts.url.toString()); opts.url.searchParams.append('hash', hash); + opts.url.searchParams.append('size', fileSize.toString()); } } + console.log('上传文件到', opts.url.toString()); const headers = form.getHeaders(); return new Promise((resolve) => { form.submit(getFormParams(opts, headers), (err, res) => { diff --git a/src/uitls/hash.ts b/src/uitls/hash.ts index 8fc3981..d820721 100644 --- a/src/uitls/hash.ts +++ b/src/uitls/hash.ts @@ -1,16 +1,16 @@ -import MD5 from 'crypto-js/md5.js'; +import crypto from 'node:crypto'; import fs from 'node:fs'; export const getHash = (file: string) => { if (!fs.existsSync(file)) return ''; - const content = fs.readFileSync(file, 'utf-8'); - return MD5(content).toString(); + const buffer = fs.readFileSync(file); // 不指定编码,返回 Buffer + return crypto.createHash('md5').update(buffer).digest('hex'); }; export const getBufferHash = (buffer: Buffer) => { - return MD5(buffer.toString()).toString(); + return crypto.createHash('md5').update(buffer).digest('hex'); }; export const getStringHash = (str: string) => { - return MD5(str).toString(); -} + return crypto.createHash('md5').update(str).digest('hex'); +} \ No newline at end of file diff --git a/test/Snipaste_2025-12-03_16-53-48.png b/test/Snipaste_2025-12-03_16-53-48.png new file mode 100644 index 0000000..dc8afd8 Binary files /dev/null and b/test/Snipaste_2025-12-03_16-53-48.png differ diff --git a/test/hash-fix.ts b/test/hash-fix.ts new file mode 100644 index 0000000..b5a3a3b --- /dev/null +++ b/test/hash-fix.ts @@ -0,0 +1,14 @@ +import { getHash } from '../src/uitls/hash.ts'; +const minioHash = '380655ee50227439956c3a468cb6956a'; +const myHash = 'e0d3293395d9459b295a56b03e5720fc' + +import fs from 'node:fs' +import path from 'node:path' + +const filePath = path.resolve('./test/Snipaste_2025-12-03_16-53-48.png') + +const hash = getHash(filePath); + +console.log('计算的hash:', hash); +console.log('minio的hash:', minioHash); +console.log('我的hash:', myHash); \ No newline at end of file