update hash and put object

This commit is contained in:
熊潇 2025-05-12 17:37:30 +08:00
parent 1c4e9d9bb9
commit ab29a1eb0b
5 changed files with 92 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/oss",
"version": "0.0.10",
"version": "0.0.11",
"main": "dist/index.js",
"scripts": {
"build": "tsup",

View File

@ -3,6 +3,7 @@ import { ListFileObject, ListObjectResult, OssBaseOperation } from './core/type.
import { hash } from './util/hash.ts';
import { copyObject } from './core/copy-object.ts';
import omit from 'lodash/omit.js';
import { getContentType } from './util/get-content-type.ts';
export type OssBaseOptions<T = { [key: string]: any }> = {
/**
* minio client
@ -66,7 +67,7 @@ export class OssBase implements OssBaseOperation {
});
}
/**
* ,
* , etag会变async await busboy check
* @param objectName
* @param data
* @param metaData
@ -99,7 +100,7 @@ export class OssBase implements OssBaseOperation {
if (opts?.check) {
const obj = await this.statObject(objectName, true);
if (obj) {
const omitMeda = ['content-type', 'cache-control', 'app-source'];
const omitMeda = ['size', 'content-type', 'cache-control', 'app-source'];
const objMeta = JSON.parse(JSON.stringify(omit(obj.metaData, omitMeda)));
metaData = {
...objMeta,
@ -107,7 +108,6 @@ export class OssBase implements OssBaseOperation {
};
}
}
console.log('putObject metaData', metaData);
const bucketName = this.bucketName;
const obj = await this.client.putObject(bucketName, `${this.prefix}${objectName}`, putData, size, metaData);
@ -174,12 +174,43 @@ export class OssBase implements OssBaseOperation {
* @param hash
* @returns
*/
async checkObjectHash(objectName: string, hash: string) {
async checkObjectHash(
objectName: string,
hash: string,
meta?: ItemBucketMetadata,
): Promise<{ success: boolean; metaData: ItemBucketMetadata | null; obj: any; equalMeta?: boolean }> {
const obj = await this.statObject(`${this.prefix}${objectName}`, true);
if (!obj) {
return false;
return { success: false, metaData: null, obj: null, equalMeta: false };
}
return obj.etag === hash;
let metaData: ItemBucketMetadata = {};
const omitMeda = ['content-type', 'cache-control', 'app-source'];
const objMeta = omit(obj.metaData, omitMeda);
metaData = {
...objMeta,
};
let equalMeta = false;
if (meta) {
equalMeta = JSON.stringify(metaData) === JSON.stringify(meta);
}
return { success: obj.etag === hash, metaData, obj, equalMeta };
}
getMetadata(pathname: string, meta: ItemBucketMetadata = { 'app-source': 'user-app' }) {
const isHtml = pathname.endsWith('.html');
if (isHtml) {
meta = {
...meta,
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-cache',
};
} else {
meta = {
...meta,
'content-type': getContentType(pathname),
'cache-control': 'max-age=31536000, immutable',
};
}
return meta;
}
async copyObject(sourceObject: any, targetObject: any) {

View File

@ -0,0 +1,49 @@
import path from 'node:path';
// 获取文件的 content-type
export const getContentType = (filePath: string) => {
const extname = path.extname(filePath);
const contentType = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.md': 'text/markdown; charset=utf-8', // utf-8配置
'.ico': 'image/x-icon', // Favicon 图标
'.webp': 'image/webp', // WebP 图像格式
'.webm': 'video/webm', // WebM 视频格式
'.ogg': 'audio/ogg', // Ogg 音频格式
'.mp3': 'audio/mpeg', // MP3 音频格式
'.m4a': 'audio/mp4', // M4A 音频格式
'.m3u8': 'application/vnd.apple.mpegurl', // HLS 播放列表
'.ts': 'video/mp2t', // MPEG Transport Stream
'.pdf': 'application/pdf', // PDF 文档
'.doc': 'application/msword', // Word 文档
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // Word 文档 (新版)
'.ppt': 'application/vnd.ms-powerpoint', // PowerPoint 演示文稿
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', // PowerPoint (新版)
'.xls': 'application/vnd.ms-excel', // Excel 表格
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // Excel 表格 (新版)
'.csv': 'text/csv; charset=utf-8', // CSV 文件
'.xml': 'application/xml; charset=utf-8', // XML 文件
'.rtf': 'application/rtf', // RTF 文本文件
'.eot': 'application/vnd.ms-fontobject', // Embedded OpenType 字体
'.ttf': 'font/ttf', // TrueType 字体
'.woff': 'font/woff', // Web Open Font Format 1.0
'.woff2': 'font/woff2', // Web Open Font Format 2.0
'.otf': 'font/otf', // OpenType 字体
'.wasm': 'application/wasm', // WebAssembly 文件
'.pem': 'application/x-pem-file', // PEM 证书文件
'.crt': 'application/x-x509-ca-cert', // CRT 证书文件
'.yaml': 'application/x-yaml; charset=utf-8', // YAML 文件
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
'.zip': 'application/octet-stream',
};
return contentType[extname] || 'application/octet-stream';
};

View File

@ -11,7 +11,9 @@ import crypto from 'node:crypto';
*/
export const hash = (str: string | Buffer | Object) => {
let hashStr: string | Buffer;
if (typeof str === 'object') {
if (str instanceof Buffer) {
hashStr = str;
} else if (str instanceof Object) {
hashStr = JSON.stringify(str, null, 2);
} else {
hashStr = str;

View File

@ -1 +1,3 @@
export * from './hash.ts';
export * from './get-content-type.ts';