图片上传hash 有bug,修复
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import fs from 'node:fs';
|
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 { fileIsExist } from '@/uitls/file.ts';
|
||||||
import { getHash } from '@/uitls/hash.ts';
|
import { getHash } from '@/uitls/hash.ts';
|
||||||
import glob from 'fast-glob';
|
import glob from 'fast-glob';
|
||||||
|
|||||||
@@ -56,12 +56,12 @@ const syncUpload = new Command('upload')
|
|||||||
});
|
});
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
if (res.data?.isNew) {
|
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) {
|
} 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 {
|
} 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);
|
logger.debug(res);
|
||||||
|
|||||||
@@ -79,12 +79,15 @@ export const upload = (opts: UploadOptions): Promise<{ code?: number; message?:
|
|||||||
value = opts.file;
|
value = opts.file;
|
||||||
}
|
}
|
||||||
form.append('file', value);
|
form.append('file', value);
|
||||||
|
const fileSize = Buffer.byteLength(value);
|
||||||
if (opts.needHash) {
|
if (opts.needHash) {
|
||||||
hash = opts?.hash || getBufferHash(value);
|
hash = opts?.hash || getBufferHash(value);
|
||||||
opts.url = new URL(opts.url.toString());
|
opts.url = new URL(opts.url.toString());
|
||||||
opts.url.searchParams.append('hash', hash);
|
opts.url.searchParams.append('hash', hash);
|
||||||
|
opts.url.searchParams.append('size', fileSize.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log('上传文件到', opts.url.toString());
|
||||||
const headers = form.getHeaders();
|
const headers = form.getHeaders();
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
form.submit(getFormParams(opts, headers), (err, res) => {
|
form.submit(getFormParams(opts, headers), (err, res) => {
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import MD5 from 'crypto-js/md5.js';
|
import crypto from 'node:crypto';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
|
|
||||||
export const getHash = (file: string) => {
|
export const getHash = (file: string) => {
|
||||||
if (!fs.existsSync(file)) return '';
|
if (!fs.existsSync(file)) return '';
|
||||||
const content = fs.readFileSync(file, 'utf-8');
|
const buffer = fs.readFileSync(file); // 不指定编码,返回 Buffer
|
||||||
return MD5(content).toString();
|
return crypto.createHash('md5').update(buffer).digest('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBufferHash = (buffer: Buffer) => {
|
export const getBufferHash = (buffer: Buffer) => {
|
||||||
return MD5(buffer.toString()).toString();
|
return crypto.createHash('md5').update(buffer).digest('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getStringHash = (str: string) => {
|
export const getStringHash = (str: string) => {
|
||||||
return MD5(str).toString();
|
return crypto.createHash('md5').update(str).digest('hex');
|
||||||
}
|
}
|
||||||
BIN
test/Snipaste_2025-12-03_16-53-48.png
Normal file
BIN
test/Snipaste_2025-12-03_16-53-48.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 169 KiB |
14
test/hash-fix.ts
Normal file
14
test/hash-fix.ts
Normal 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);
|
||||||
Reference in New Issue
Block a user