Add support of allow free url config option with correspond modifications

This commit is contained in:
Wu Cheng-Han 2016-12-16 15:38:05 +08:00
parent 4b7b902806
commit 5bb3de2675
3 changed files with 14 additions and 2 deletions

View file

@ -119,6 +119,7 @@ Environment variables (will overwrite other server configs)
| HMD_URL_ADDPORT | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) | | HMD_URL_ADDPORT | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) |
| HMD_USECDN | `true` or `false` | set to use CDN resources or not (default is `true`) | | HMD_USECDN | `true` or `false` | set to use CDN resources or not (default is `true`) |
| HMD_ALLOW_ANONMYOUS | `true` or `false` | set to allow anonmyous usage (default is `true`) | | HMD_ALLOW_ANONMYOUS | `true` or `false` | set to allow anonmyous usage (default is `true`) |
| HMD_ALLOW_FREEURL | `true` or `false` | set to allow new note by accessing not exist note url |
| HMD_FACEBOOK_CLIENTID | no example | Facebook API client id | | HMD_FACEBOOK_CLIENTID | no example | Facebook API client id |
| HMD_FACEBOOK_CLIENTSECRET | no example | Facebook API client secret | | HMD_FACEBOOK_CLIENTSECRET | no example | Facebook API client secret |
| HMD_TWITTER_CONSUMERKEY | no example | Twitter API consumer key | | HMD_TWITTER_CONSUMERKEY | no example | Twitter API consumer key |
@ -155,6 +156,7 @@ Server settings `config.json`
| urladdport | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) | | urladdport | `true` or `false` | set to add port on callback url (port 80 or 443 won't applied) (only applied when domain is set) |
| usecdn | `true` or `false` | set to use CDN resources or not (default is `true`) | | usecdn | `true` or `false` | set to use CDN resources or not (default is `true`) |
| allowanonmyous | `true` or `false` | set to allow anonmyous usage (default is `true`) | | allowanonmyous | `true` or `false` | set to allow anonmyous usage (default is `true`) |
| allowfreeurl | `true` or `false` | set to allow new note by accessing not exist note url |
| db | `{ "dialect": "sqlite", "storage": "./db.hackmd.sqlite" }` | set the db configs, [see more here](http://sequelize.readthedocs.org/en/latest/api/sequelize/) | | db | `{ "dialect": "sqlite", "storage": "./db.hackmd.sqlite" }` | set the db configs, [see more here](http://sequelize.readthedocs.org/en/latest/api/sequelize/) |
| sslkeypath | `./cert/client.key` | ssl key path (only need when you set usessl) | | sslkeypath | `./cert/client.key` | ssl key path (only need when you set usessl) |
| sslcertpath | `./cert/hackmd_io.crt` | ssl cert path (only need when you set usessl) | | sslcertpath | `./cert/hackmd_io.crt` | ssl cert path (only need when you set usessl) |

View file

@ -21,6 +21,8 @@ var usecdn = process.env.HMD_USECDN ? (process.env.HMD_USECDN === 'true') : ((ty
var allowanonmyous = process.env.HMD_ALLOW_ANONMYOUS ? (process.env.HMD_ALLOW_ANONMYOUS === 'true') : ((typeof config.allowanonmyous === 'boolean') ? config.allowanonmyous : true); var allowanonmyous = process.env.HMD_ALLOW_ANONMYOUS ? (process.env.HMD_ALLOW_ANONMYOUS === 'true') : ((typeof config.allowanonmyous === 'boolean') ? config.allowanonmyous : true);
var allowfreeurl = process.env.HMD_ALLOW_FREEURL ? (process.env.HMD_ALLOW_FREEURL === 'true') : !!config.allowfreeurl;
// db // db
var db = config.db || { var db = config.db || {
dialect: 'sqlite', dialect: 'sqlite',
@ -128,6 +130,7 @@ module.exports = {
serverurl: getserverurl(), serverurl: getserverurl(),
usecdn: usecdn, usecdn: usecdn,
allowanonmyous: allowanonmyous, allowanonmyous: allowanonmyous,
allowfreeurl: allowfreeurl,
db: db, db: db,
sslkeypath: path.join(cwd, sslkeypath), sslkeypath: path.join(cwd, sslkeypath),
sslcertpath: path.join(cwd, sslcertpath), sslcertpath: path.join(cwd, sslcertpath),

View file

@ -112,7 +112,8 @@ function newNote(req, res, next) {
return response.errorForbidden(res); return response.errorForbidden(res);
} }
models.Note.create({ models.Note.create({
ownerId: owner ownerId: owner,
alias: req.alias ? req.alias : null
}).then(function (note) { }).then(function (note) {
return res.redirect(config.serverurl + "/" + LZString.compressToBase64(note.id)); return res.redirect(config.serverurl + "/" + LZString.compressToBase64(note.id));
}).catch(function (err) { }).catch(function (err) {
@ -133,6 +134,7 @@ function checkViewPermission(req, note) {
} }
function findNote(req, res, callback, include) { function findNote(req, res, callback, include) {
var noteId = req.params.noteId;
var id = req.params.noteId || req.params.shortid; var id = req.params.noteId || req.params.shortid;
models.Note.parseNoteId(id, function (err, _id) { models.Note.parseNoteId(id, function (err, _id) {
models.Note.findOne({ models.Note.findOne({
@ -142,8 +144,13 @@ function findNote(req, res, callback, include) {
include: include || null include: include || null
}).then(function (note) { }).then(function (note) {
if (!note) { if (!note) {
if (config.allowfreeurl && noteId) {
req.alias = noteId;
return newNote(req, res);
} else {
return response.errorNotFound(res); return response.errorNotFound(res);
} }
}
if (!checkViewPermission(req, note)) { if (!checkViewPermission(req, note)) {
return response.errorForbidden(res); return response.errorForbidden(res);
} else { } else {