-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaFile.cs
100 lines (88 loc) · 3.56 KB
/
MetaFile.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
using System;
using System.Collections.Generic;
namespace FreneticMediaServer
{
public class MetaFile
{
public MetaFile(string fileData)
{
bool timeSet = false;
foreach (KeyValuePair<string, string> option in GeneralHelpers.ReadConfigData(fileData, (line) => throw new Exception("Invalid meta configuration line '" + line + "'")))
{
string value = option.Value;
switch (option.Key)
{
case "original_name":
OriginalName = GeneralHelpers.UnescapeFromConfig(value);
break;
case "time":
Time = DateTimeOffset.FromUnixTimeSeconds(long.Parse(value));
timeSet = true;
break;
case "uploader":
Uploader = GeneralHelpers.UnescapeFromConfig(value);
break;
case "uploader_ip":
UploaderIP = GeneralHelpers.UnescapeFromConfig(value);
break;
case "delete_code":
DeleteCode = GeneralHelpers.UnescapeFromConfig(value);
break;
case "description":
Description = GeneralHelpers.UnescapeFromConfig(value);
break;
default:
continue;
}
}
if (Description == null)
{
throw new Exception("Meta file data does not contain a description value!");
}
if (Uploader == null)
{
throw new Exception("Meta file data does not contain an uploader value!");
}
if (OriginalName == null)
{
throw new Exception("Meta file data does not contain an original name value!");
}
if (!timeSet)
{
throw new Exception("Meta file data does not contain an upload time value!");
}
}
public MetaFile(string _uploader, string _description, string _originalname, string _ip)
{
Time = DateTimeOffset.Now;
Uploader = _uploader;
Description = _description;
OriginalName = _originalname;
UploaderIP = _ip;
}
public void GenerateDeleteCode()
{
DeleteCode_Clean = GeneralHelpers.BytesToHex(SecurityHelper.GetRandomBytes(32));
DeleteCode = SecurityHelper.HashCurrent(DeleteCode_Clean);
}
public DateTimeOffset Time;
public string Description;
public string Uploader;
public string UploaderIP;
public string DeleteCode;
public string DeleteCode_Clean;
public string OriginalName;
public DateTimeOffset? DeleteTime;
public string FileOutputString()
{
return "# Generated by FreneticMediaServer\noriginal_name=" + GeneralHelpers.EscapeForConfig(OriginalName)
+ "\ntime=" + Time.ToUnixTimeSeconds()
+ (DeleteTime.HasValue ? "\ndelete_time=" + DeleteTime.Value.ToUnixTimeSeconds() : "")
+ "\nuploader=" + GeneralHelpers.EscapeForConfig(Uploader)
+ "\nuploader_ip=" + GeneralHelpers.EscapeForConfig(UploaderIP)
+ "\ndelete_code=" + DeleteCode
+ "\ndescription=" + GeneralHelpers.EscapeForConfig(Description)
+ "\n";
}
}
}