Add /admin page

This commit is contained in:
Christoph Wiechert
2017-07-12 15:19:53 +02:00
parent 4c2f5c1d94
commit 1bb9d5792f
10 changed files with 300 additions and 7 deletions

View File

@@ -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));
}