|
|
@@ -4,11 +4,22 @@ const fs = require('fs');
|
|
|
|
|
|
const ApiKeyModel = require('../models/apiKey.js').ApiKeyModel;
|
|
|
const PasteContent = require('../models/pasteContent.js').PasteContent;
|
|
|
+const AccessModel = require('../models/access.js').AccessModel;
|
|
|
const mCrypto = require('../src/crypto.js');
|
|
|
const Security = require('../src/security.js');
|
|
|
const CONFIG = require('../src/config.js');
|
|
|
|
|
|
+async function onAccessContent(app, req, entity) {
|
|
|
+ if (entity.privId !== req.params.id) {
|
|
|
+ //FIXME ?rel
|
|
|
+ let accessEntry = new AccessModel(entity.privId, req.params.id, Security.getRequestIp(req));
|
|
|
+ await accessEntry.resolveIp();
|
|
|
+ await app.databaseHelper.insertOne(accessEntry);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
async function renderRawPage(app, req, res, entity) {
|
|
|
+ await onAccessContent(app, req, entity);
|
|
|
if (entity.type === 'paste')
|
|
|
return await app.routerUtils.staticServe(res, app.getData(entity.privId));
|
|
|
else if (entity.type === 'file') {
|
|
|
@@ -21,6 +32,7 @@ async function renderRawPage(app, req, res, entity) {
|
|
|
}
|
|
|
|
|
|
async function renderPublicPage(app, req, res, entity) {
|
|
|
+ await onAccessContent(app, req, entity);
|
|
|
if (entity.type === 'paste')
|
|
|
return await app.routerUtils.staticServe(res, app.getData(entity.privId));
|
|
|
else if (entity.type === 'file') {
|
|
|
@@ -33,10 +45,20 @@ async function renderPublicPage(app, req, res, entity) {
|
|
|
app.routerUtils.onInternalError(res, "Unknown type: " +entity.type);
|
|
|
}
|
|
|
|
|
|
-function renderPrivatePage(app, res, entity) {
|
|
|
+async function _readAccess(app, entityId) {
|
|
|
+ return (await app.databaseHelper.fetch(AccessModel, { privId: entityId }))
|
|
|
+ .map(x => x.describe())
|
|
|
+ .map(x => { delete x.ipAddress; delete x.privId; x.ipRegion = JSON.parse(x.ipRegion); return x; });
|
|
|
+}
|
|
|
+
|
|
|
+async function renderPrivatePage(app, res, entity) {
|
|
|
let stat;
|
|
|
try { stat = fs.statSync(app.dataDir+entity.privId); } catch (e) { stat = { error: e }; }
|
|
|
- app.routerUtils.jsonResponse(res, { ...entity.describe(), ...stat, ...{ path: app.getData(entity.privId) } });
|
|
|
+ const access = await _readAccess(app, entity.privId);
|
|
|
+
|
|
|
+ let context = app.routerUtils.commonRenderInfos();
|
|
|
+ context.page_title += " - Pastit";
|
|
|
+ res.end(whiskers.render(require('../templates/stats.js'), { ...context, ...{size: stat.size}, ...{ access: JSON.stringify(access) }}));
|
|
|
}
|
|
|
|
|
|
module.exports = { register: app => {
|