-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
shortner.js
60 lines (50 loc) · 1.54 KB
/
shortner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Created by rishabhshukla on 09/03/17.
*/
var firebase = require('firebase');
const r = require('convert-radix64');
const hasha = require("hasha");
const hashMap = {};
var config = {
apiKey: "AIzaSyBAAUCZ8xXzS4r7jxMhlvPB6OzKIC0MRE8",
authDomain: "urlshortner-b1883.firebaseapp.com",
databaseURL: "https://urlshortner-b1883.firebaseio.com",
storageBucket: "urlshortner-b1883.appspot.com",
};
firebase.initializeApp(config);
module.exports = {
shorten: (url) => {
hash = hasha(url, {encoding:"base64", algorithm:"md5"});
hash = hash.slice(0,4);
hash = hash.replace('/','-');
hash = hash.replace('+','_');
// let hashInt = parseInt(hash,16)
// conv = atob(hashInt);
hashMap[hash] = url;
writeUserData(url,r.from64(hash),hash);
return hash;
},
expand: (shortcode) => {
return new Promise(function(resolve, reject){
if(shortcode === undefined){
reject(null);
}
var ref = firebase.database().ref('/'+r.from64(shortcode));
ref.once('value').then(function(snapshot) {
val = snapshot.val();
if(val){
let url = val.url;
resolve(url);
}else{
resolve(hashMap[shortcode]);
}
});
});
}
};
writeUserData = (url,shortcode,code) => {
firebase.database().ref('/'+shortcode).set({
code:code,
url:url
});
}