summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorCheng-Han, Wu2016-01-31 15:42:26 -0600
committerCheng-Han, Wu2016-01-31 15:42:26 -0600
commitff2fc76491c11fad33ae6c62c4d5b5c47b431597 (patch)
treee7eb990eacfb8361d9ab319a87dfc272f7f9cd98 /lib
parent0f87fd4493e68b6cd17ba5b1b097d11d11add83d (diff)
Supported export to gist
Diffstat (limited to '')
-rw-r--r--lib/response.js135
1 files changed, 134 insertions, 1 deletions
diff --git a/lib/response.js b/lib/response.js
index e656d550..daf5fb11 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -9,6 +9,8 @@ var LZString = require('lz-string');
var S = require('string');
var shortId = require('shortid');
var metaMarked = require('meta-marked');
+var querystring = require('querystring');
+var request = require('request');
//core
var config = require("../config.js");
@@ -56,7 +58,8 @@ var response = {
showPublishSlide: showPublishSlide,
showIndex: showIndex,
noteActions: noteActions,
- publishNoteActions: publishNoteActions
+ publishNoteActions: publishNoteActions,
+ githubActions: githubActions
};
function responseError(res, code, detail, msg) {
@@ -362,6 +365,28 @@ function actionPDF(req, res, noteId) {
});
}
+function actionGist(req, res, noteId) {
+ db.readFromDB(noteId, function (err, data) {
+ if (err) {
+ return response.errorNotFound(res);
+ }
+ var owner = data.rows[0].owner;
+ Note.findOrNewNote(noteId, owner, function (err, note) {
+ if (err) {
+ return response.errorNotFound(res);
+ }
+ var data = {
+ client_id: config.github.clientID,
+ redirect_uri: config.getserverurl() + '/auth/github/callback/' + LZString.compressToBase64(noteId) + '/gist',
+ scope: "gist",
+ state: shortId.generate()
+ };
+ var query = querystring.stringify(data);
+ res.redirect("https://github.com/login/oauth/authorize?" + query);
+ });
+ });
+}
+
function noteActions(req, res, next) {
var noteId = req.params.noteId;
if (noteId != config.featuresnotename) {
@@ -402,6 +427,9 @@ function noteActions(req, res, next) {
case "pdf":
actionPDF(req, res, noteId);
break;
+ case "gist":
+ actionGist(req, res, noteId);
+ break;
default:
if (noteId != config.featuresnotename)
res.redirect('/' + LZString.compressToBase64(noteId));
@@ -444,6 +472,111 @@ function publishNoteActions(req, res, next) {
}
}
+function githubActions(req, res, next) {
+ var noteId = req.params.noteId;
+ if (noteId != config.featuresnotename) {
+ if (!Note.checkNoteIdValid(noteId)) {
+ return response.errorNotFound(res);
+ }
+ noteId = LZString.decompressFromBase64(noteId);
+ if (!noteId) {
+ return response.errorNotFound(res);
+ }
+ }
+ Note.findNote(noteId, function (err, note) {
+ if (err || !note) {
+ return response.errorNotFound(res);
+ }
+ db.readFromDB(note.id, function (err, data) {
+ if (err) {
+ return response.errorNotFound(res);
+ }
+ var notedata = data.rows[0];
+ //check view permission
+ if (note.permission == 'private') {
+ if (!req.isAuthenticated() || notedata.owner != req.user._id)
+ return response.errorForbidden(res);
+ }
+ var action = req.params.action;
+ switch (action) {
+ case "gist":
+ githubActionGist(req, res, noteId);
+ break;
+ default:
+ if (noteId != config.featuresnotename)
+ res.redirect('/' + LZString.compressToBase64(noteId));
+ else
+ res.redirect('/' + noteId);
+ break;
+ }
+ });
+ });
+}
+
+function githubActionGist(req, res, noteId) {
+ db.readFromDB(noteId, function (err, data) {
+ if (err) {
+ return response.errorNotFound(res);
+ }
+ var notedata = data.rows[0];
+ var code = req.query.code;
+ var state = req.query.state;
+ if (!code || !state) {
+ return response.errorForbidden(res);
+ } else {
+ var data = {
+ client_id: config.github.clientID,
+ client_secret: config.github.clientSecret,
+ code: code,
+ state: state
+ }
+ var auth_url = 'https://github.com/login/oauth/access_token';
+ request({
+ url: auth_url,
+ method: "POST",
+ json: data
+ }, function (error, httpResponse, body) {
+ if (!error && httpResponse.statusCode == 200) {
+ var access_token = body.access_token;
+ if (access_token) {
+ var content = LZString.decompressFromBase64(notedata.content);
+ var title = notedata.title;
+ var decodedTitle = LZString.decompressFromBase64(title);
+ if (decodedTitle) title = decodedTitle;
+ var filename = title.replace('/', ' ') + '.md';
+ var gist = {
+ "files": {}
+ };
+ gist.files[filename] = {
+ "content": content
+ };
+ var gist_url = "https://api.github.com/gists";
+ request({
+ url: gist_url,
+ headers: {
+ 'User-Agent': 'HackMD',
+ 'Authorization': 'token ' + access_token
+ },
+ method: "POST",
+ json: gist
+ }, function (error, httpResponse, body) {
+ if (!error && httpResponse.statusCode == 201) {
+ res.redirect(body.html_url);
+ } else {
+ return response.errorForbidden(res);
+ }
+ });
+ } else {
+ return response.errorForbidden(res);
+ }
+ } else {
+ return response.errorForbidden(res);
+ }
+ })
+ }
+ });
+}
+
function showPublishSlide(req, res, next) {
var shortid = req.params.shortid;
if (shortId.isValid(shortid)) {