core: add trace to storage

This commit is contained in:
undefined committed 2021-03-21 18:38:38 +08:00
1 parent 3aadb9511d
commit 8018a79418
3 files changed
+33 -2

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.20.26",
"version": "2.20.27",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
+1 -1
View File
@@ -319,7 +319,7 @@ export class HandlerCommon {
} catch (e) {
logger.warn(e.message);
logger.info('%s %o', name, args);
logger.info('%s', e.stack);
if (!e.message.includes('Expected') || !e.message.includes('to match')) logger.info('%s', e.stack);
}
return res;
}
+31
View File
@@ -26,6 +26,7 @@ interface MinioEndpointConfig {
}
function parseMainEndpointUrl(endpoint: string): MinioEndpointConfig {
if (!endpoint) throw new Error('Empty endpoint');
const url = new URL(endpoint);
const result: Partial<MinioEndpointConfig> = {};
if (url.pathname !== '/') throw new Error('Main MinIO endpoint URL of a sub-directory is not supported.');
@@ -116,20 +117,36 @@ class StorageService {
async put(target: string, file: string | Buffer | Readable, meta: ItemBucketMetadata = {}) {
if (typeof file === 'string') file = createReadStream(file);
try {
return await this.client.putObject(this.opts.bucket, target, file, meta);
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
async get(target: string, path?: string) {
try {
if (path) return await this.client.fGetObject(this.opts.bucket, target, path);
return await this.client.getObject(this.opts.bucket, target);
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
async del(target: string | string[]) {
try {
if (typeof target === 'string') return await this.client.removeObject(this.opts.bucket, target);
return await this.client.removeObjects(this.opts.bucket, target);
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
async list(target: string, recursive = true) {
try {
const stream = this.client.listObjects(this.opts.bucket, target, recursive);
return await new Promise<BucketItem[]>((resolve, reject) => {
const results: BucketItem[] = [];
@@ -145,14 +162,24 @@ class StorageService {
stream.on('end', () => resolve(results));
stream.on('error', reject);
});
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
async getMeta(target: string) {
try {
const result = await this.client.statObject(this.opts.bucket, target);
return { ...result.metaData, ...result };
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
async signDownloadLink(target: string, filename?: string, noExpire = false, useAlternativeEndpointFor?: 'user' | 'judge'): Promise<string> {
try {
const url = await this.client.presignedGetObject(
this.opts.bucket,
target,
@@ -163,6 +190,10 @@ class StorageService {
);
if (useAlternativeEndpointFor) return this.replaceWithAlternativeUrlFor[useAlternativeEndpointFor](url);
return url;
} catch (e) {
e.stack = new Error().stack;
throw e;
}
}
}