core: add file fallback

pull/178/head
undefined 3 years ago
parent 9037a9bfd3
commit d37f485e30

@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.32.27", "version": "2.32.28",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "src/loader", "main": "src/loader",
"module": "src/loader", "module": "src/loader",

@ -320,7 +320,12 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler {
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1]; if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`; const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target); const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename); if (!file) {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
return;
}
this.response.etag = file.etag; this.response.etag = file.etag;
const type = lookup(filename).toString(); const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length; const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;

@ -184,7 +184,12 @@ export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHan
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1]; if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`; const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target); const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename); if (!file) {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
return;
}
this.response.etag = file.etag; this.response.etag = file.etag;
const type = lookup(filename).toString(); const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length; const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;

@ -111,7 +111,12 @@ export class FSDownloadHandler extends Handler {
this.response.addHeader('Cache-Control', 'public'); this.response.addHeader('Cache-Control', 'public');
const target = `user/${uid}/${filename}`; const target = `user/${uid}/${filename}`;
const file = await storage.getMeta(target); const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename); if (!file) {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
return;
}
this.response.etag = file.etag; this.response.etag = file.etag;
const type = lookup(filename).toString(); const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length; const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;

@ -417,7 +417,12 @@ export class ProblemFileDownloadHandler extends ProblemDetailHandler {
} }
const target = `problem/${domainId}/${this.pdoc.docId}/${type}/${filename}`; const target = `problem/${domainId}/${this.pdoc.docId}/${type}/${filename}`;
const file = await storage.getMeta(target); const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename); if (!file) {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
return;
}
this.response.etag = file.etag; this.response.etag = file.etag;
const fileType = lookup(filename).toString(); const fileType = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => fileType.includes(i)).length; const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => fileType.includes(i)).length;

@ -18,7 +18,7 @@ export class StorageModel {
meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out')) meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out'))
? 'text/plain' ? 'text/plain'
: lookup(path) || 'application/octet-stream'; : lookup(path) || 'application/octet-stream';
const _id = nanoid() + extname(path); const _id = `${nanoid(3)}/${nanoid()}${extname(path)}`;
await storage.put(_id, file, meta); await storage.put(_id, file, meta);
const { metaData, size, etag } = await storage.getMeta(_id); const { metaData, size, etag } = await storage.getMeta(_id);
await StorageModel.coll.insertOne({ await StorageModel.coll.insertOne({
@ -88,7 +88,7 @@ export class StorageModel {
{ path: target, autoDelete: { $exists: false } }, { path: target, autoDelete: { $exists: false } },
{ $set: { lastUsage: new Date() } }, { $set: { lastUsage: new Date() } },
); );
if (!res.value) return ''; if (!res.value) return await storage.signDownloadLink(target, filename, noExpire, useAlternativeEndpointFor);
return await storage.signDownloadLink(res.value._id, filename, noExpire, useAlternativeEndpointFor); return await storage.signDownloadLink(res.value._id, filename, noExpire, useAlternativeEndpointFor);
} }
} }

Loading…
Cancel
Save