-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
293 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,46 @@ | ||
var express = require('express') | ||
, router = express.Router() | ||
, mongoose = require('mongoose') | ||
, Shortlink = mongoose.model('Shortlink') | ||
, utils = require('../../utils') | ||
, log = require('./../../config/middlewares/log')() | ||
, _ = require('underscore'); | ||
var express = require('express'), | ||
router = express.Router(), | ||
mongoose = require('mongoose'), | ||
config = require('../../config/config'), | ||
lookup = require('safe-browse-url-lookup')({ apiKey: config.googleApiKey, clientId: 'anchr.io' }), | ||
Shortlink = mongoose.model('Shortlink'), | ||
utils = require('../../utils'), | ||
log = require('./../../config/middlewares/log')(), | ||
_ = require('underscore'); | ||
|
||
module.exports = function (app, passport) { | ||
app.use('/api/shortlink', router); | ||
app.use('/s', router); | ||
module.exports = function(app, passport) { | ||
app.use('/api/shortlink', router); | ||
app.use('/s', router); | ||
}; | ||
|
||
router.use(log); | ||
|
||
router.get('/:id', function (req, res, next) { | ||
var asJson = req.query.json; | ||
router.get('/:id', function(req, res, next) { | ||
var asJson = req.query.json; | ||
|
||
Shortlink.findOne({_id : req.params.id}, {__v : 0}, function (err, obj) { | ||
if (err || !obj) return res.makeError(404, "Not found."); | ||
if (!asJson && obj.url) res.redirect(obj.url); | ||
else res.send(_.omit(obj.toObject(), '__v', 'id')); | ||
}); | ||
Shortlink.findOne({ _id: req.params.id }, { __v: 0 }, function(err, obj) { | ||
if (err || !obj) return res.makeError(404, "Not found."); | ||
if (!asJson && obj.url) res.redirect(obj.url); | ||
else res.send(_.omit(obj.toObject(), '__v', 'id')); | ||
}); | ||
}); | ||
|
||
router.post('/', function (req, res, next) { | ||
if (!req.body.url) return res.makeError(400, 'Malformed request: You need to pass a url attribute.') | ||
router.post('/', function(req, res, next) { | ||
if (!req.body.url) return res.makeError(400, 'Malformed request: You need to pass a url attribute.'); | ||
|
||
var shortlink = new Shortlink({ | ||
url: req.body.url, | ||
_id: utils.generateUUID() | ||
}); | ||
shortlink.save(function (err, obj) { | ||
if (err) return res.makeError(500, 'Unable to save shortlink to database.', err); | ||
res.status(201).send(_.omit(obj.toObject(), '__v', 'id')); | ||
}); | ||
}); | ||
lookup.checkSingle(req.body.url) | ||
.then(isMalicious => { | ||
if (isMalicious) return res.makeError(400, 'The link you try to reference is not safe!'); | ||
var shortlink = new Shortlink({ | ||
url: req.body.url, | ||
_id: utils.generateUUID() | ||
}); | ||
shortlink.save(function(err, obj) { | ||
if (err) return res.makeError(500, 'Unable to save shortlink to database.', err); | ||
res.status(201).send(_.omit(obj.toObject(), '__v', 'id')); | ||
}); | ||
}) | ||
.catch(err => { | ||
return res.makeError(500, 'Something went wrong.' + err); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
'use strict'; | ||
|
||
angular.module('anchrClientApp') | ||
.controller('ShortlinkCtrl', ['$scope', '$rootScope', 'Shortlink', 'Collection', 'Snackbar', function ($scope, $rootScope, Shortlink, Collection, Snackbar) { | ||
|
||
$scope.createShortlink = function (url) { | ||
$scope.data.loading = true; | ||
var sl = new Shortlink({url : url}); | ||
sl.$save(function (result) { | ||
if (!result || !result._id) return Snackbar.show("Error while shortening link :-("); | ||
$scope.data.shortlinks.push(result); | ||
$scope.data.loading = false; | ||
$scope.data.linkInput = ''; | ||
|
||
if ($rootScope.loggedIn()) saveToCollection(result); | ||
}); | ||
}; | ||
|
||
$scope.clear = function () { | ||
$scope.data = { | ||
loading: false, | ||
linkInput: '', | ||
shortlinks : [] | ||
}; | ||
} | ||
|
||
// RUN | ||
init(); | ||
|
||
function init() { | ||
$scope.clear(); | ||
$rootScope.init(); | ||
} | ||
|
||
function saveToCollection (link) { | ||
new Collection.shortlinks({ | ||
url : link.href, | ||
description: 'Shortlink to ' + link.url | ||
}).$save(function (result){}, function (err) { | ||
Snackbar.show('Failed to save shortlink to your collection: ' + err.data.error); | ||
}); | ||
}; | ||
|
||
}]); | ||
.controller('ShortlinkCtrl', ['$scope', '$rootScope', 'Shortlink', 'Collection', 'Snackbar', function($scope, $rootScope, Shortlink, Collection, Snackbar) { | ||
|
||
$scope.createShortlink = function(url) { | ||
$scope.data.loading = true; | ||
var sl = new Shortlink({ url: url }); | ||
sl.$save(function(result) { | ||
if (!result || !result._id) { | ||
$scope.data.loading = false; | ||
return Snackbar.show("Error while shortening link :-("); | ||
} | ||
$scope.data.shortlinks.push(result); | ||
$scope.data.loading = false; | ||
$scope.data.linkInput = ''; | ||
|
||
if ($rootScope.loggedIn()) saveToCollection(result); | ||
}, function(err) { | ||
$scope.data.loading = false; | ||
return Snackbar.show(err.data.error); | ||
}); | ||
}; | ||
|
||
$scope.clear = function() { | ||
$scope.data = { | ||
loading: false, | ||
linkInput: '', | ||
shortlinks: [] | ||
}; | ||
} | ||
|
||
// RUN | ||
init(); | ||
|
||
function init() { | ||
$scope.clear(); | ||
$rootScope.init(); | ||
} | ||
|
||
function saveToCollection(link) { | ||
new Collection.shortlinks({ | ||
url: link.href, | ||
description: 'Shortlink to ' + link.url | ||
}).$save(function(result) {}, function(err) { | ||
Snackbar.show('Failed to save shortlink to your collection: ' + err.data.error); | ||
}); | ||
}; | ||
|
||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,30 @@ | ||
{ | ||
"name": "anchr-client", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"angular": "1.4.7", | ||
"bootstrap": "^3.2.0", | ||
"angular-cookies": "^1.4.0", | ||
"angular-resource": "^1.4.0", | ||
"angular-route": "^1.4.0", | ||
"bootstrap-material-design": "~0.4.3", | ||
"ngclipboard": "~1.0.0", | ||
"snackbarjs": "~1.0.0", | ||
"ng-file-upload": "~10.0.2", | ||
"angular-jwt": "~0.0.9", | ||
"cryptojslib": "^3.1.2" | ||
}, | ||
"devDependencies": { | ||
"angular-mocks": "^1.4.0" | ||
}, | ||
"appPath": "app", | ||
"moduleName": "anchrClientApp", | ||
"overrides": { | ||
"bootstrap": { | ||
"main": [ | ||
"less/bootstrap.less", | ||
"dist/css/bootstrap.css", | ||
"dist/js/bootstrap.js" | ||
] | ||
} | ||
}, | ||
"resolutions": { | ||
"angular": "1.4.7" | ||
"name": "anchr-client", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"angular": "1.4.7", | ||
"bootstrap": "^3.2.0", | ||
"angular-cookies": "^1.4.0", | ||
"angular-resource": "^1.4.0", | ||
"angular-route": "^1.4.0", | ||
"bootstrap-material-design": "~0.4.3", | ||
"ngclipboard": "~1.0.0", | ||
"snackbarjs": "~1.0.0", | ||
"ng-file-upload": "~10.0.2", | ||
"angular-jwt": "~0.0.9", | ||
"cryptojslib": "^3.1.2" | ||
}, | ||
"devDependencies": { | ||
"angular-mocks": "^1.4.0" | ||
}, | ||
"appPath": "app", | ||
"moduleName": "anchrClientApp", | ||
"overrides": { | ||
"bootstrap": { | ||
"main": [ | ||
"less/bootstrap.less", | ||
"dist/css/bootstrap.css", | ||
"dist/js/bootstrap.js" | ||
] | ||
} | ||
} |
Oops, something went wrong.