Navigation Menu

Skip to content

Commit

Permalink
add support for generating private urls with "private=1"
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Sep 5, 2012
1 parent a07ab67 commit a283590
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit a283590

Please sign in to comment.