-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlExtender.cs
More file actions
120 lines (97 loc) · 3.57 KB
/
UrlExtender.cs
File metadata and controls
120 lines (97 loc) · 3.57 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Newtonsoft.Json;
using UrlExtender.Gzip;
using UrlExtender.Objects;
namespace UrlExtender {
public static class Extender {
private static readonly Config config;
private static Dictionary<int, ExtendedEntry> db;
private static bool dbLock = false;
static Extender() {
config = new Config();
if(File.Exists(config.dbFile)) {
var compressed = File.ReadAllBytes(config.dbFile);
string json = ExtenderGzip.Decompress(compressed);
db = JsonConvert.DeserializeObject<Dictionary<int,ExtendedEntry>>(json);
}
else {
db = new Dictionary<int, ExtendedEntry>();
}
new Thread(new ThreadStart(ScheduledSave)).Start();
}
private static void SaveDB() {
while(dbLock) {}
dbLock = true;
string json = JsonConvert.SerializeObject(db, Formatting.None);
var compressed = ExtenderGzip.Compress(json);
File.WriteAllBytes(config.dbFile, compressed);
dbLock = false;
}
private static void ScheduledSave() {
while(true) {
Thread.Sleep(60000);
SaveDB();
}
}
private static string getUrlFromCount(int count) {
string url = "";
for(int i = 0; i < count; i++) {
url += config.magicWord;
}
return url;
}
private static int getCountFromUrl(string url) {
return Regex.Matches(url, config.magicWord).Count;
}
private static int generateValidCount() {
try {
int count = new Random().Next(1,1000);
foreach(int i in db.Keys) {
if(i == count) return generateValidCount();
}
return count;
}
catch (System.StackOverflowException) { // detect infinite recursion if we keep failing to find a valid URL
throw new OutOfURLsException("Y'all used this so much that we ran out of extended URLs. Tell me ASAP");
}
}
public static string Add(string url) {
int count;
int alreadyExtended = -1;
foreach(int i in db.Keys) {
if(db[i].url == url) alreadyExtended = i;
}
if(alreadyExtended != -1) {
return getUrlFromCount(alreadyExtended);
}
else {
count = generateValidCount();
}
db.Add(count, new ExtendedEntry(url));
new Thread(new ThreadStart(SaveDB)).Start();
return getUrlFromCount(count);
}
public static string Dereferance(string url) {
int count = getCountFromUrl(url);
db[count].hits++;
return db[count].url;
}
public static int GetHits(string url) {
foreach(int i in db.Keys) {
if(db[i].url == url) {
return db[i].hits;
}
}
throw new KeyNotFoundException("The given URL has not been extended");
}
}
public class OutOfURLsException : Exception {
public OutOfURLsException() {}
public OutOfURLsException(string message) : base(message) {}
public OutOfURLsException(string message, Exception inner) : base(message, inner) {}
}
}