This commit is contained in:
2025-05-12 17:41:11 +08:00
parent cf82967060
commit 3938a0f87e
3 changed files with 57 additions and 21 deletions

View File

@@ -74,14 +74,14 @@ export const getMetadata = (pathname: string) => {
if (isHtml) {
meta = {
...meta,
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-cache',
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-cache',
};
} else {
meta = {
...meta,
'Content-Type': getContentType(pathname),
'Cache-Control': 'max-age=31536000, immutable',
'content-type': getContentType(pathname),
'cache-control': 'max-age=31536000, immutable',
};
}
return meta;
@@ -118,10 +118,18 @@ export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts:
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
let statMeta: any = {};
if (!force) {
const check = await oss.checkObjectHash(objectName, hash);
if (check) {
return end({ success: true, hash }, '文件已存在');
const check = await oss.checkObjectHash(objectName, hash, meta);
statMeta = check?.metaData || {};
let isNewMeta = false;
if (check.success && JSON.stringify(meta) !== '{}' && !check.equalMeta) {
meta = { ...statMeta, ...getMetadata(pathname), ...meta };
isNewMeta = true;
await oss.replaceObject(objectName, { ...meta });
}
if (check.success) {
return end({ success: true, hash, meta, isNewMeta, equalMeta: check.equalMeta }, '文件已存在');
}
}
const bb = busboy({
@@ -135,16 +143,19 @@ export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts:
bb.on('file', async (name, file, info) => {
fileProcessed = true;
try {
// console.log('file', stat?.metaData);
// await sleep(2000);
await oss.putObject(
objectName,
file,
{
...statMeta,
...getMetadata(pathname),
...meta,
},
{ check: true, isStream: true },
{ check: false, isStream: true },
);
end({ success: true, name, info }, '上传成功', 200);
end({ success: true, name, info, meta: meta?.metaData, statMeta }, '上传成功', 200);
} catch (error) {
end({ error: error }, '上传失败', 500);
}