mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
基于时间戳的文件下载secret
This commit is contained in:
1 parent
a6829863f2
commit
5e51bd2fe0
7 files changed
+118
-25
No files matched your search
@@ -1,4 +1,4 @@
|
||||
const db = require('../service/db.js');
|
||||
const db = require('../service/db');
|
||||
|
||||
const coll = db.collection('blacklist');
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
const { ForbiddenError } = require('../error');
|
||||
const db = require('../service/db');
|
||||
const fs = require('../service/gridfs');
|
||||
const hash = require('../lib/hash.hydro');
|
||||
|
||||
const coll = db.collection('file');
|
||||
|
||||
async function add(stream, filename, meta = {}) {
|
||||
const file = await coll.insertOne(meta);
|
||||
const w = fs.openUploadStreamWithId(file.insertedId, filename);
|
||||
await new Promise((resolve, reject) => {
|
||||
stream.on('end', resolve);
|
||||
stream.on('error', reject);
|
||||
stream.pipe(w);
|
||||
});
|
||||
const c = await fs.find({ _id: file.insertedId }).toArray();
|
||||
await coll.updateOne({ _id: file.insertedId }, { $set: { md5: c[0].md5, size: c[0].size } });
|
||||
return file.insertedId;
|
||||
}
|
||||
|
||||
function del(_id) {
|
||||
return Promise.all([
|
||||
coll.deleteOne({ _id }),
|
||||
fs.delete(_id),
|
||||
]);
|
||||
}
|
||||
|
||||
async function inc(_id) {
|
||||
const file = await coll.findOne({ _id });
|
||||
file.count++;
|
||||
}
|
||||
|
||||
async function dec(_id) {
|
||||
const file = await coll.findOneAndUpdate(
|
||||
{ _id },
|
||||
{ $inc: { count: -1 } },
|
||||
{ returnOriginal: false },
|
||||
);
|
||||
if (!file.count) await del(_id);
|
||||
return file.count;
|
||||
}
|
||||
|
||||
async function get(_id, secret = null) {
|
||||
const file = await coll.findOne({ _id });
|
||||
if (typeof secret !== 'undefined') {
|
||||
const timestamp = Math.floor(Number(new Date())).toString();
|
||||
if (!(
|
||||
hash.check(file.secret, timestamp, secret)
|
||||
|| hash.check(file.secret, timestamp - 1, secret))) {
|
||||
throw new ForbiddenError();
|
||||
}
|
||||
}
|
||||
return fs.openDownloadStream(_id);
|
||||
}
|
||||
|
||||
function getMeta(_id) {
|
||||
return coll.findOne({ _id });
|
||||
}
|
||||
|
||||
async function url(_id, name) {
|
||||
const file = await coll.findOne({ _id });
|
||||
const secret = hash.hash(file.secret, Math.floor(Number(new Date())).toString());
|
||||
if (name) return `/fs/${_id}/${secret}?name=${Buffer.from(name).toString('base64')}`;
|
||||
return `/fs/${_id}/${secret}`;
|
||||
}
|
||||
|
||||
global.Hydro.model.blacklist = module.exports = {
|
||||
add, get, del, inc, dec, getMeta, url,
|
||||
};
|
||||
+6
-11
@@ -1,9 +1,9 @@
|
||||
const { ObjectID } = require('bson');
|
||||
const { STATUS_ACCEPTED } = require('./builtin').STATUS;
|
||||
const file = require('./file');
|
||||
const { ProblemNotFoundError } = require('../error');
|
||||
const validator = require('../lib/validator');
|
||||
const db = require('../service/db.js');
|
||||
const gridfs = require('../service/gridfs');
|
||||
const db = require('../service/db');
|
||||
|
||||
const coll = db.collection('problem');
|
||||
const collStatus = db.collection('problem.status');
|
||||
@@ -155,20 +155,15 @@ async function updateStatus(pid, uid, rid, status) {
|
||||
}
|
||||
async function setTestdata(_id, readStream) {
|
||||
const pdoc = await getById(_id);
|
||||
const f = gridfs.openUploadStream('data.zip');
|
||||
await new Promise((resolve, reject) => {
|
||||
readStream.pipe(f);
|
||||
f.once('finish', resolve);
|
||||
f.once('error', reject);
|
||||
});
|
||||
if (pdoc.data && typeof pdoc.data === 'object') gridfs.delete(this.pdoc.data);
|
||||
return await edit(_id, { data: f.id }); // eslint-disable-line no-return-await
|
||||
const id = await file.add(readStream, 'data.zip');
|
||||
if (pdoc.data && typeof pdoc.data === 'object') file.dec(this.pdoc.data);
|
||||
return await edit(_id, { data: id }); // eslint-disable-line no-return-await
|
||||
}
|
||||
|
||||
async function getData(pid) {
|
||||
const pdoc = await get(pid);
|
||||
if (!pdoc.data) return null;
|
||||
return gridfs.openDownloadStream(pdoc.data);
|
||||
return file.get(pdoc.data);
|
||||
}
|
||||
|
||||
global.Hydro.model.problem = module.exports = {
|
||||
|
||||
Reference in new issue
Block a user