core: add trace to storage

pull/61/head
undefined 4 years ago
parent 3aadb9511d
commit 8018a79418

@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.20.26", "version": "2.20.27",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",

@ -319,7 +319,7 @@ export class HandlerCommon {
} catch (e) { } catch (e) {
logger.warn(e.message); logger.warn(e.message);
logger.info('%s %o', name, args); 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; return res;
} }

@ -26,6 +26,7 @@ interface MinioEndpointConfig {
} }
function parseMainEndpointUrl(endpoint: string): MinioEndpointConfig { function parseMainEndpointUrl(endpoint: string): MinioEndpointConfig {
if (!endpoint) throw new Error('Empty endpoint');
const url = new URL(endpoint); const url = new URL(endpoint);
const result: Partial<MinioEndpointConfig> = {}; const result: Partial<MinioEndpointConfig> = {};
if (url.pathname !== '/') throw new Error('Main MinIO endpoint URL of a sub-directory is not supported.'); if (url.pathname !== '/') throw new Error('Main MinIO endpoint URL of a sub-directory is not supported.');
@ -116,53 +117,83 @@ class StorageService {
async put(target: string, file: string | Buffer | Readable, meta: ItemBucketMetadata = {}) { async put(target: string, file: string | Buffer | Readable, meta: ItemBucketMetadata = {}) {
if (typeof file === 'string') file = createReadStream(file); if (typeof file === 'string') file = createReadStream(file);
return await this.client.putObject(this.opts.bucket, target, file, meta); 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) { async get(target: string, path?: string) {
if (path) return await this.client.fGetObject(this.opts.bucket, target, path); try {
return await this.client.getObject(this.opts.bucket, target); 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[]) { async del(target: string | string[]) {
if (typeof target === 'string') return await this.client.removeObject(this.opts.bucket, target); try {
return await this.client.removeObjects(this.opts.bucket, target); 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) { async list(target: string, recursive = true) {
const stream = this.client.listObjects(this.opts.bucket, target, recursive); try {
return await new Promise<BucketItem[]>((resolve, reject) => { const stream = this.client.listObjects(this.opts.bucket, target, recursive);
const results: BucketItem[] = []; return await new Promise<BucketItem[]>((resolve, reject) => {
stream.on('data', (result) => { const results: BucketItem[] = [];
if (result.size) { stream.on('data', (result) => {
results.push({ if (result.size) {
...result, results.push({
prefix: target, ...result,
name: result.name.split(target)[1], prefix: target,
}); name: result.name.split(target)[1],
} });
}
});
stream.on('end', () => resolve(results));
stream.on('error', reject);
}); });
stream.on('end', () => resolve(results)); } catch (e) {
stream.on('error', reject); e.stack = new Error().stack;
}); throw e;
}
} }
async getMeta(target: string) { async getMeta(target: string) {
const result = await this.client.statObject(this.opts.bucket, target); try {
return { ...result.metaData, ...result }; 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> { async signDownloadLink(target: string, filename?: string, noExpire = false, useAlternativeEndpointFor?: 'user' | 'judge'): Promise<string> {
const url = await this.client.presignedGetObject( try {
this.opts.bucket, const url = await this.client.presignedGetObject(
target, this.opts.bucket,
noExpire ? 24 * 60 * 60 * 7 : 30 * 60, target,
filename noExpire ? 24 * 60 * 60 * 7 : 30 * 60,
? { 'response-content-disposition': `attachment; filename="${encodeRFC5987ValueChars(filename)}"` } filename
: {}, ? { 'response-content-disposition': `attachment; filename="${encodeRFC5987ValueChars(filename)}"` }
); : {},
if (useAlternativeEndpointFor) return this.replaceWithAlternativeUrlFor[useAlternativeEndpointFor](url); );
return url; if (useAlternativeEndpointFor) return this.replaceWithAlternativeUrlFor[useAlternativeEndpointFor](url);
return url;
} catch (e) {
e.stack = new Error().stack;
throw e;
}
} }
} }

Loading…
Cancel
Save