Add /admin page
This commit is contained in:
@@ -116,6 +116,15 @@ module.exports = class DB {
|
||||
}
|
||||
|
||||
|
||||
async updateLastDownload(sid, key) {
|
||||
debug(`Update last download ${sid}++${key}`);
|
||||
const data = this.get(sid).find(item => item.key === key);
|
||||
if(!data) return;
|
||||
data.metadata.lastDownload = Date.now();
|
||||
await this.store.update(`${sid}++${key}`, data);
|
||||
}
|
||||
|
||||
|
||||
get(sid) {
|
||||
return this.db[sid];
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ const MD5 = require("crypto-js/md5");
|
||||
const debug = require('debug')('psitransfer:main');
|
||||
const archiver = require('archiver');
|
||||
const zlib = require('zlib');
|
||||
const _ = require('lodash');
|
||||
|
||||
const errorPage = fs.readFileSync(path.join(__dirname, '../public/html/error.html')).toString();
|
||||
const store = new Store(config.uploadDir);
|
||||
@@ -54,6 +55,32 @@ app.get('/config.json', (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
app.get('/admin', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '../public/html/admin.html'));
|
||||
});
|
||||
app.get('/admin/data.json', (req, res) => {
|
||||
if(!config.adminPass || !req.get('x-passwd')) return res.status(401).send('Unauthorized');
|
||||
if(req.get('x-passwd') !== config.adminPass) return res.status(403).send('Forbidden');
|
||||
|
||||
const result = _.chain(db.db)
|
||||
.cloneDeep()
|
||||
.forEach(bucket => {
|
||||
bucket.forEach(file => {
|
||||
if(file.metadata.password) {
|
||||
file.metadata._password = true;
|
||||
delete file.metadata.password;
|
||||
delete file.metadata.key;
|
||||
delete file.key;
|
||||
delete file.url;
|
||||
}
|
||||
})
|
||||
})
|
||||
.value();
|
||||
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
|
||||
// List files / Download App
|
||||
app.get('/:sid', (req, res, next) => {
|
||||
if(req.url.endsWith('.json')) {
|
||||
@@ -128,6 +155,8 @@ app.get('/files/:fid', async(req, res, next) => {
|
||||
bucket.forEach(async info => {
|
||||
if(info.metadata.retention === 'one-time') {
|
||||
await db.remove(info.metadata.sid, info.metadata.key);
|
||||
} else {
|
||||
await db.updateLastDownload(info.metadata.sid, info.metadata.key);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -145,11 +174,13 @@ app.get('/files/:fid', async(req, res, next) => {
|
||||
res.download(store.getFilename(req.params.fid), info.metadata.name);
|
||||
|
||||
// remove one-time files after download
|
||||
if(info.metadata.retention === 'one-time') {
|
||||
res.on('finish', async () => {
|
||||
res.on('finish', async () => {
|
||||
if(info.metadata.retention === 'one-time') {
|
||||
await db.remove(info.metadata.sid, info.metadata.key);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await db.updateLastDownload(info.metadata.sid, info.metadata.key);
|
||||
}
|
||||
});
|
||||
} catch(e) {
|
||||
res.status(404).send(errorPage.replace('%%ERROR%%', e.message));
|
||||
}
|
||||
|
||||
@@ -45,6 +45,13 @@ class Store {
|
||||
}
|
||||
|
||||
|
||||
async update(fid, data) {
|
||||
debug(`Update File ${this.getFilename(fid)}`);
|
||||
await fsp.writeJson(this.getFilename(fid) + '.json', data);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
async info(fid) {
|
||||
try {
|
||||
const info = await fsp.readJson(this.getFilename(fid) + '.json');
|
||||
|
||||
Reference in New Issue
Block a user