-
Notifications
You must be signed in to change notification settings - Fork 9
/
Router.cs
101 lines (87 loc) · 3.54 KB
/
Router.cs
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
using System;
using System.Text;
using LiteDB;
using EasyEncryption;
using Multiformats.Base;
using Nancy;
using Newtonsoft.Json;
namespace SharpPaste
{
public class Router : NancyModule
{
public Router()
{
Get["/"] = parameters => View["index"];
Get["/{longId}"] = parameters =>
{
using (var db = new LiteDatabase(Config.DBPATH))
{
var collection = db.GetCollection<Paste>("pastes");
var paste = collection.FindOne(Query.EQ("LongId", parameters.longId.ToString()));
if (paste == null) return HttpStatusCode.NotFound;
return View["paste", paste];
}
};
Get["/json/{longId}"] = parameters =>
{
using (var db = new LiteDatabase(Config.DBPATH))
{
var collection = db.GetCollection<Paste>("pastes");
var paste = collection.FindOne(Query.EQ("LongId", parameters.longId.ToString()));
if (paste == null) return HttpStatusCode.NotFound;
return JsonConvert.SerializeObject(paste);
}
};
Get["/raw/{longId}"] = parameters =>
{
string longId = parameters.longId;
using (var db = new LiteDatabase(Config.DBPATH))
{
var result = db.GetCollection<Paste>("pastes").FindOne(Query.EQ("LongId", longId));
return result.Body;
}
};
Post["/upload"] = parameters =>
{
var body = Request.Body;
var length = (int) body.Length;
var data = new byte[length];
body.Read(data, 0, length);
var jsonPaste = JsonConvert.DeserializeObject<Paste>(Encoding.Default.GetString(data));
if (HexUtils.isHex(jsonPaste.Title) && HexUtils.isHex(jsonPaste.Body))
{
using (var db = new LiteDatabase(Config.DBPATH))
{
var pastes = db.GetCollection<Paste>("pastes");
string hashSeed = pastes.Count().ToString() + jsonPaste.Date.ToString() + jsonPaste.Title + jsonPaste.Body + jsonPaste.Language;
string longId = Multibase.Base64.Encode(HexUtils.toByteArray(SHA.ComputeSHA256Hash(hashSeed)), false, true);
var newPaste = new Paste
{
LongId = longId,
Date = DateTime.Now,
Title = jsonPaste.Title,
Body = jsonPaste.Body,
Language = jsonPaste.Language
};
pastes.Insert(newPaste);
var res = new UploadResponse
{
Status = "success",
LongId = longId
};
return JsonConvert.SerializeObject(res);
}
}
else
{
var res = new UploadResponse
{
Status = "error",
ErrMsg = "Error: the paste is not encrypted with AES-256."
};
return JsonConvert.SerializeObject(res);
}
};
}
}
}