-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
96 lines (85 loc) · 2.99 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unosquare.Labs.EmbedIO;
using Unosquare.Labs.EmbedIO.Modules;
using Unosquare.Swan;
using Zio.FileSystems;
namespace EmbedStorage
{
class Program
{
public static List<StorageModel> FileStorages;
static void Main(string[] args)
{
Load();
var url = "http://+:5000/";
using (var server = new WebServer(url))
{
server.WithLocalSession();
server.RegisterModule(new WebApiModule());
server.Module<WebApiModule>().RegisterController<APIController>();
server.RunAsync();
Console.ReadKey(true);
}
}
public static bool Save()
{
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (var fs = GetWorkFileSystem().OpenFile("/data/storage.bin",
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None))
{
serializer.Serialize(fs, FileStorages);
}
return true;
}
catch (Exception ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
return false;
}
}
public static void Load()
{
try
{
if (!GetWorkFileSystem().FileExists("/data/storage.bin")
|| GetWorkFileSystem().GetFileLength("/data/storage.bin") < 10)
{
FileStorages = new List<StorageModel>();
return;
}
using (var fs = GetWorkFileSystem().OpenFile("/data/storage.bin",
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite))
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
FileStorages = deserializer.Deserialize(fs) as List<StorageModel>;
}
}
catch (System.Runtime.Serialization.SerializationException)
{
FileStorages = new List<StorageModel>();
}
catch (Exception ex)
{
Terminal.Error(ex, ex.Source, ex.Message);
}
}
public static SubFileSystem GetWorkFileSystem()
{
var fs = new PhysicalFileSystem();
var path = fs.ConvertPathFromInternal(AppDomain.CurrentDomain.BaseDirectory);
var subfs = new SubFileSystem(fs, path);
return subfs;
}
}
}