-
Notifications
You must be signed in to change notification settings - Fork 46
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
1 parent
9d301f1
commit cdae339
Showing
15 changed files
with
5,767 additions
and
4,665 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
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,87 +1,87 @@ | ||
/*********************************************************** | ||
* JavaScript syntax format: ES5/ES6 - ECMAScript 2015 | ||
* Loading all required dependencies, libraries and packages | ||
**********************************************************/ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
|
||
const logger = require("../../../logger"); | ||
const us = require("../models/url_shortener"); | ||
const UrlShortener = us.UrlShortener; | ||
const UrlShortener_s = us.sequelize; | ||
|
||
/** | ||
* Creates and saves a shortened url and returns it | ||
* @param url | ||
* @param text *optional* | ||
*/ | ||
router.post("/shorten", function(req, res, next) { | ||
var loop = 0; | ||
var maxLoop = 20; | ||
|
||
shorten(); | ||
function shorten() { | ||
var short = Math.random() | ||
.toString(36) | ||
.substr(2, 4); | ||
|
||
let newUrlShortened = { | ||
full: encodeURIComponent(req.body.url), | ||
short: short, | ||
creator: req.user | ||
}; | ||
|
||
UrlShortener.create(newUrlShortened) | ||
.then(created => { | ||
res.send({ | ||
status: "success", | ||
message: "Successfully shortened URL.", | ||
body: { url: short } | ||
}); | ||
}) | ||
.catch(err => { | ||
if ( | ||
loop < maxLoop && | ||
err.hasOwnProperty("errors") && | ||
err.errors[0] && | ||
err.errors[0].hasOwnProperty("path") && | ||
err.errors[0].hasOwnProperty("type") | ||
) { | ||
loop++; | ||
shorten(); | ||
} else { | ||
logger("error", "Failed to shorten URL.", req.originalUrl, req, err); | ||
res.send({ | ||
status: "failure", | ||
message: "Failed to shorten URL.", | ||
body: {} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
router.post("/expand", function(req, res, next) { | ||
UrlShortener.findOne({ | ||
where: { | ||
short: req.body.short | ||
} | ||
}).then(url => { | ||
if (!url) { | ||
logger("error", "Failed to find short URL.", req.originalUrl, req); | ||
res.send({ | ||
status: "failure", | ||
message: "Failure to find URL.", | ||
body: {} | ||
}); | ||
} else { | ||
res.send({ | ||
status: "success", | ||
message: "Successfully shortened URL.", | ||
body: { url: decodeURIComponent(url.full) } | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
module.exports = router; | ||
/*********************************************************** | ||
* JavaScript syntax format: ES5/ES6 - ECMAScript 2015 | ||
* Loading all required dependencies, libraries and packages | ||
**********************************************************/ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
|
||
const logger = require("../../../logger"); | ||
const us = require("../models/url_shortener"); | ||
const UrlShortener = us.UrlShortener; | ||
const UrlShortener_s = us.sequelize; | ||
|
||
/** | ||
* Creates and saves a shortened url and returns it | ||
* @param url | ||
* @param text *optional* | ||
*/ | ||
router.post("/shorten", function (req, res, next) { | ||
var loop = 0; | ||
var maxLoop = 20; | ||
|
||
shorten(); | ||
function shorten() { | ||
var short = Math.random() | ||
.toString(36) | ||
.substr(2, 5 + loop); | ||
|
||
let newUrlShortened = { | ||
full: encodeURIComponent(req.body.url), | ||
short: short, | ||
creator: req.user, | ||
}; | ||
|
||
UrlShortener.create(newUrlShortened) | ||
.then((created) => { | ||
res.send({ | ||
status: "success", | ||
message: "Successfully shortened URL.", | ||
body: { url: short }, | ||
}); | ||
}) | ||
.catch((err) => { | ||
if ( | ||
loop < maxLoop && | ||
err.hasOwnProperty("errors") && | ||
err.errors[0] && | ||
err.errors[0].hasOwnProperty("path") && | ||
err.errors[0].hasOwnProperty("type") | ||
) { | ||
loop++; | ||
shorten(); | ||
} else { | ||
logger("error", "Failed to shorten URL.", req.originalUrl, req, err); | ||
res.send({ | ||
status: "failure", | ||
message: "Failed to shorten URL.", | ||
body: {}, | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
router.post("/expand", function (req, res, next) { | ||
UrlShortener.findOne({ | ||
where: { | ||
short: req.body.short, | ||
}, | ||
}).then((url) => { | ||
if (!url) { | ||
logger("error", "Failed to find short URL.", req.originalUrl, req); | ||
res.send({ | ||
status: "failure", | ||
message: "Failure to find URL.", | ||
body: {}, | ||
}); | ||
} else { | ||
res.send({ | ||
status: "success", | ||
message: "Successfully shortened URL.", | ||
body: { url: decodeURIComponent(url.full) }, | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.