-
Notifications
You must be signed in to change notification settings - Fork 12
/
FolderMappings.cs
154 lines (140 loc) · 4.91 KB
/
FolderMappings.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.IO;
using Shoko.Models.Server;
namespace Shoko.Commons
{
public class FolderMappings
{
public static FolderMappings Instance { get; }=new FolderMappings();
private Dictionary<int, string> _mappings = new Dictionary<int, string>();
private Action<Dictionary<int, string>> _saveFunction;
private Func<Dictionary<int, string>> _loadFunction;
private bool _wasloaded;
private void LoadCheck()
{
if (_wasloaded)
return;
if (_loadFunction != null)
{
_mappings = _loadFunction();
_wasloaded = true;
}
}
public void SetLoadAndSaveCallback(Func<Dictionary<int, string>> loadFunction, Action<Dictionary<int, string>> saveFunction)
{
_loadFunction = loadFunction;
_saveFunction = saveFunction;
}
public void MapFolder(int folderid, string localpath)
{
LoadCheck();
_mappings[folderid] = localpath;
_saveFunction?.Invoke(_mappings);
}
public void UnMapFolder(int folderid)
{
LoadCheck();
if (_mappings.ContainsKey(folderid))
_mappings.Remove(folderid);
_saveFunction?.Invoke(_mappings);
}
public string GetMapping(int folderid)
{
if (_mappings.ContainsKey(folderid))
return _mappings[folderid];
return string.Empty;
}
public bool IsValid(ImportFolder impfolder) => !string.IsNullOrEmpty(TranslateDirectory(impfolder, string.Empty));
public string TranslateFile(ImportFolder impfolder, string path)
{
if (impfolder == null) return string.Empty;
string result=TranslateFile(impfolder.ImportFolderID, path);
try
{
if (result == string.Empty && Directory.Exists(impfolder.ImportFolderLocation))
{
string npath = CombineNoChecks(impfolder.ImportFolderLocation, path);
if (File.Exists(npath))
return npath;
}
}
catch
{
}
return result;
}
public string TranslateDirectory(ImportFolder impfolder, string path)
{
if (impfolder == null) return string.Empty;
string result=TranslateDirectory(impfolder.ImportFolderID, path);
try
{
if (result == string.Empty && Directory.Exists(impfolder.ImportFolderLocation))
{
string npath = CombineNoChecks(impfolder.ImportFolderLocation, path);
if (Directory.Exists(npath))
return npath;
}
}
catch
{
}
return result;
}
public static string CombineNoChecks(string path1, string path2)
{
if (path2.StartsWith(Path.DirectorySeparatorChar.ToString()))
path2 = path2.Substring(1);
if (path2.Length == 0)
return path1;
if (path1.Length == 0)
return path2;
char ch = path1[path1.Length - 1];
if (ch != Path.DirectorySeparatorChar && ch != Path.AltDirectorySeparatorChar &&
ch != Path.VolumeSeparatorChar)
return path1 + Path.DirectorySeparatorChar + path2;
return path1 + path2;
}
public string TranslateFile(int folderid, string path)
{
LoadCheck();
if (path == null)
return string.Empty;
if (_mappings == null) return string.Empty;
if (!_mappings.ContainsKey(folderid) || string.IsNullOrEmpty(_mappings[folderid]))
return string.Empty;
string start = CombineNoChecks(_mappings[folderid], path);
try
{
if (File.Exists(start))
return start;
}
catch
{
//TODO: Security issue
}
return string.Empty;
}
public string TranslateDirectory(int folderid, string path)
{
LoadCheck();
if (path == null)
return string.Empty;
if (_mappings == null) return string.Empty;
if (!_mappings.ContainsKey(folderid) || string.IsNullOrEmpty(_mappings[folderid]))
return string.Empty;
string start = CombineNoChecks(_mappings[folderid], path);
try
{
if (Directory.Exists(start))
return start;
}
catch
{
//TODO: Security issue
}
return string.Empty;
}
}
}