diff options
Diffstat (limited to 'lib/response.js')
-rw-r--r-- | lib/response.js | 177 |
1 files changed, 158 insertions, 19 deletions
diff --git a/lib/response.js b/lib/response.js index a7dcc023..d3b011bf 100644 --- a/lib/response.js +++ b/lib/response.js @@ -6,6 +6,8 @@ var path = require('path'); var uuid = require('node-uuid'); var markdownpdf = require("markdown-pdf"); var LZString = require('lz-string'); +var S = require('string'); +var shortId = require('shortid'); //core var config = require("../config.js"); @@ -31,16 +33,20 @@ var response = { newNote: newNote, showFeatures: showFeatures, showNote: showNote, - noteActions: noteActions + showShareNote: showShareNote, + noteActions: noteActions, + shareNoteActions: shareNoteActions }; function responseError(res, code, detail, msg) { res.writeHead(code, { 'Content-Type': 'text/html' }); - var content = ejs.render(fs.readFileSync(config.errorpath, 'utf8'), { + var template = config.errorpath; + var content = ejs.render(fs.readFileSync(template, 'utf8'), { + title: code + ' ' + detail + ' ' + msg, cache: !config.debug, - filename: config.errorpath, + filename: template, code: code, detail: detail, msg: msg @@ -49,16 +55,44 @@ function responseError(res, code, detail, msg) { res.end(); } -function responseHackMD(res) { - res.writeHead(200, { - 'Content-Type': 'text/html' - }); - var content = ejs.render(fs.readFileSync(config.hackmdpath, 'utf8'), { - cache: !config.debug, - filename: config.hackmdpath +function responseHackMD(res, noteId) { + if (noteId != config.featuresnotename) { + if (!Note.checkNoteIdValid(noteId)) { + responseError(res, "404", "Not Found", "oops."); + return; + } + noteId = LZString.decompressFromBase64(noteId); + if (!noteId) { + responseError(res, "404", "Not Found", "oops."); + return; + } + } + db.readFromDB(noteId, function (err, data) { + if (err) { + responseError(res, "404", "Not Found", "oops."); + return; + } + var title = data.rows[0].title; + var decodedTitle = LZString.decompressFromBase64(title); + if (decodedTitle) title = decodedTitle; + title = Note.generateWebTitle(title); + var template = config.hackmdpath; + var options = { + cache: !config.debug, + filename: template + }; + var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); + var html = compiled({ + title: title + }); + var buf = html; + res.writeHead(200, { + 'Content-Type': 'text/html; charset=UTF-8', + 'Cache-Control': 'private', + 'Content-Length': buf.length + }); + res.end(buf); }); - res.write(content); - res.end(); } function newNote(req, res, next) { @@ -88,10 +122,10 @@ function showFeatures(req, res, next) { responseError(res, "500", "Internal Error", "wtf."); return; } - responseHackMD(res); + responseHackMD(res, config.featuresnotename); }); } else { - responseHackMD(res); + responseHackMD(res, config.featuresnotename); } }); } @@ -102,22 +136,105 @@ function showNote(req, res, next) { responseError(res, "404", "Not Found", "oops."); return; } - responseHackMD(res); + responseHackMD(res, noteId); } +function showShareNote(req, res, next) { + var shortid = req.params.shortid; + if (shortId.isValid(shortid)) { + Note.findNote(shortid, function (err, note) { + if (err || !note) { + responseError(res, "404", "Not Found", "oops."); + return; + } + //increase note viewcount + Note.increaseViewCount(note, function (err, note) { + if (err || !note) { + responseError(res, "404", "Not Found", "oops."); + return; + } + db.readFromDB(note.id, function (err, data) { + if (err) { + responseError(res, "404", "Not Found", "oops."); + return; + } + var body = LZString.decompressFromBase64(data.rows[0].content); + var updatetime = data.rows[0].update_time; + var text = S(body).escapeHTML().s; + var title = data.rows[0].title; + var decodedTitle = LZString.decompressFromBase64(title); + if (decodedTitle) title = decodedTitle; + title = Note.generateWebTitle(title); + var template = config.prettypath; + var options = { + cache: !config.debug, + filename: template + }; + var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options); + var origin = "//" + req.headers.host; + var html = compiled({ + title: title, + viewcount: note.viewcount, + updatetime: updatetime, + url: origin, + body: text + }); + var buf = html; + res.writeHead(200, { + 'Content-Type': 'text/html; charset=UTF-8', + 'Cache-Control': 'private', + 'Content-Length': buf.length + }); + res.end(buf); + }); + }); + }); + } else { + responseError(res, "404", "Not Found", "oops."); + } +} + +function actionShare(req, res, noteId) { + db.readFromDB(noteId, function (err, data) { + if (err) { + responseError(res, "404", "Not Found", "oops."); + return; + } + var owner = data.rows[0].owner; + var permission = "freely"; + if (owner && owner != "null") { + permission = "editable"; + } + Note.findOrNewNote(noteId, permission, function (err, note) { + if (err) { + responseError(res, "404", "Not Found", "oops."); + return; + } + res.redirect("/s/" + note.shortid); + }); + }); +} + +//pretty api is deprecated function actionPretty(req, res, noteId) { db.readFromDB(noteId, function (err, data) { if (err) { responseError(res, "404", "Not Found", "oops."); return; } - var body = data.rows[0].content; + var body = LZString.decompressFromBase64(data.rows[0].content); + var text = S(body).escapeHTML().s; + var title = data.rows[0].title; + var decodedTitle = LZString.decompressFromBase64(title); + if (decodedTitle) title = decodedTitle; + title = Note.generateWebTitle(title); var template = config.prettypath; var compiled = ejs.compile(fs.readFileSync(template, 'utf8')); var origin = "//" + req.headers.host; var html = compiled({ + title: title, url: origin, - body: body + body: text }); var buf = html; res.writeHead(200, { @@ -190,8 +307,9 @@ function noteActions(req, res, next) { } var action = req.params.action; switch (action) { - case "pretty": - actionPretty(req, res, noteId); + case "share": + case "pretty": //pretty deprecated + actionShare(req, res, noteId); break; case "download": actionDownload(req, res, noteId); @@ -208,4 +326,25 @@ function noteActions(req, res, next) { } } +function shareNoteActions(req, res, next) { + var action = req.params.action; + switch (action) { + case "edit": + var shortid = req.params.shortid; + if (shortId.isValid(shortid)) { + Note.findNote(shortid, function (err, note) { + if (err || !note) { + responseError(res, "404", "Not Found", "oops."); + return; + } + if (note.id != config.featuresnotename) + res.redirect('/' + LZString.compressToBase64(note.id)); + else + res.redirect('/' + note.id); + }); + } + break; + } +} + module.exports = response;
\ No newline at end of file |