图片上传hash 有bug,修复

This commit is contained in:
2025-12-03 18:05:40 +08:00
parent a97f015205
commit 87824c0021
6 changed files with 27 additions and 10 deletions

View File

@@ -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';

View File

@@ -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);

View File

@@ -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) => {

View File

@@ -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');
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 KiB

14
test/hash-fix.ts Normal file
View File

@@ -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);