Permalink
Browse files

Merge pull request #6 from LearnBoost/add/private-urls

add support for generating private urls with "private=1"
  • Loading branch information...
2 parents d5d5317 + a283590 commit e94c5374ba47b6adeffcb97aeb2cee95ea85d2bc @TooTallNate TooTallNate committed Sep 5, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 app.js
View
@@ -8,6 +8,7 @@ var express = require('express')
, sio = require('socket.io')
, base60 = require('./base60')
, jadevu = require('jadevu')
+ , crypto = require('crypto')
, url = require('url')
, nib = require('nib')
, fs = require('fs')
@@ -131,6 +132,7 @@ app.get('/', function (req, res, next) {
app.post('/', validate, exists, function (req, res, next) {
var url = req.body.url
, parsed = req.body.parsed
+ , private = req.body.private
, length
, short
, obj
@@ -141,7 +143,12 @@ app.post('/', validate, exists, function (req, res, next) {
function onLenth (err, len) {
if (err) return next(500);
length = len;
- short = base60.toString(length ? length + 1 : 0);
+
+ if (private) {
+ short = crypto.randomBytes(20).toString('hex');
+ } else {
+ short = base60.toString(length ? length + 1 : 0);
+ }
// next save the short url with the original url to the "urls" hash
redis.hset('urls', short, url, onUrlsSet);
@@ -209,6 +216,10 @@ function accept(type) {
*/
function exists (req, res, next) {
+ if (req.body.private) {
+ // for "private" links, always return a new one
+ return next();
+ }
redis.hget('urls-hash', req.body.url, function (err, val) {
if (err) return next(err);
if (val) return res.send({ short: 'https://' + app.set('domain') + '/' + val });

0 comments on commit e94c537

Please sign in to comment.