-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
358 lines (294 loc) · 12 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using Cloudreve_FileSynchronizer.EF;
using Mono.Options;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Cloudreve_FileSynchronizer
{
class Program
{
private const string IMAGE_EXT = ".jpg|.jpeg|.gif|.tiff|.png|.svg";
static void Main(string[] args)
{
ProgramParameter parameter = ParseParameter(args);
if (parameter == null)
return;
if(!Directory.Exists(parameter.SourceDirectory))
{
Console.WriteLine("Error: source directory does not exist");
return;
}
DirectoryInfo srcDir = new DirectoryInfo(parameter.SourceDirectory);
Task.WaitAll(Task.Run(async () =>
{
using (var dbContext = new CloudreveContext(parameter.ConnectionString))
{
var user = dbContext.Users.FirstOrDefault(p =>
p.Email == parameter.UserName || p.Nick == parameter.UserName);
if (user == null)
{
Console.WriteLine($"Error: user \"{parameter.UserName}\" does not exist");
return;
}
var policy = dbContext.Policies.FirstOrDefault(p => p.Id == parameter.StoragePolicy);
if (policy == null)
{
Console.WriteLine($"Error: Storage policy (ID = \"{parameter.StoragePolicy}\") does not exist");
return;
}
Folders rootFolder = FindRootFolder(parameter.Target, dbContext);
if (rootFolder == null)
{
Console.WriteLine($"target path \"{parameter.Target}\" does not exist in Cloudreve's database");
return;
}
else
{
Console.WriteLine($"target path \"{parameter.Target}\" found. ID = {rootFolder.Id}");
Console.WriteLine($"Synchronizing files and directories");
}
var trans = await dbContext.Database.BeginTransactionAsync();
try
{
await SaveTree(srcDir, rootFolder, parameter.Target, user, policy, dbContext);
await dbContext.SaveChangesAsync();
await trans.CommitAsync();
Console.WriteLine($"Transaction committed.");
Console.WriteLine($"Successed.");
}
catch(Exception ex)
{
await trans.RollbackAsync();
Console.WriteLine($"An has error occured durning synchronization: {ex.Message}{(ex.InnerException == null ? "" : $" ({ex.InnerException.Message})")}");
Console.WriteLine("Stack trace:");
Console.WriteLine(ex.StackTrace);
}
finally
{
trans.Dispose();
}
}
}));
}
public static async Task SaveTree(DirectoryInfo dir, Folders parentDir,
string thisDirPath, Users user, Policies policy, CloudreveContext dbContext)
{
string dirName = dir.Name;
Console.WriteLine($" - Processing directory \"{thisDirPath}\"");
var newFolder = dbContext.Folders.Add(new Folders()
{
OwnerId = user.Id,
Name = dirName,
ParentId = parentDir.Id,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
});
await dbContext.SaveChangesAsync();
Console.WriteLine($" - Folder \"{thisDirPath}\" created. ID = {newFolder.Entity.Id}");
var files =
dir.GetFiles().Select(p =>
{
string picinfo = null, fileFullPath;
if (!String.IsNullOrEmpty(p.Extension) && IMAGE_EXT.Contains(p.Extension.ToLower()))
picinfo = GetImageSizeStr(p);
fileFullPath = (thisDirPath + "/" + p.Name);
var file = new Files()
{
FolderId = newFolder.Entity.Id,
Name = p.Name,
PicInfo = picinfo,
PolicyId = policy.Id,
Size = (ulong)p.Length,
SourceName = fileFullPath,
UserId = user.Id,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
Console.WriteLine($" - File \"{fileFullPath}\" created.");
return file;
});
await dbContext.Files.AddRangeAsync(files);
Console.WriteLine($" - {files.Count()} files created.");
var subDirectories = dir.GetDirectories();
if (subDirectories.Length > 0)
{
Console.WriteLine($" - {subDirectories.Length} sub directories found. Processing sub directories...");
foreach (var subDir in subDirectories)
await SaveTree(subDir, newFolder.Entity, (thisDirPath + "/" + subDir.Name), user, policy, dbContext);
}
else
{
Console.WriteLine($" - Leaf directory, leaving...");
}
}
private static string GetImageSizeStr(FileInfo p)
{
string picinfo = null;
FileStream imageFileStream = null;
Image image = null;
try
{
imageFileStream = p.Open(FileMode.Open);
image = Image.FromStream(imageFileStream, false, false);
picinfo = $"{image.Width},{image.Height}";
}
catch(Exception ex)
{
Console.WriteLine($" - \"{p.Name}\" Failed to get image dimensions: {ex.Message}");
}
finally
{
if (imageFileStream != null)
imageFileStream.Dispose();
if (image != null)
image.Dispose();
}
return picinfo;
}
private static Folders FindRootFolder(string targetPath, CloudreveContext dbContext)
{
Folders rootFolder = null;
if (targetPath == "/")
{
rootFolder = dbContext.Folders.FirstOrDefault(p => p.Name == "/");
}
else
{
var pathParts = targetPath.Split('/');
if (String.IsNullOrWhiteSpace(pathParts.Last()))
pathParts = pathParts.Take(pathParts.Length - 1).ToArray();
var beginning = pathParts.Last();
var folderRecord = dbContext.Folders.Where(p => p.Name == beginning).ToArray();
foreach (var rec in folderRecord)
{
int i = pathParts.Length - 1;
var cur = rec;
do i--;while (i > 0 && (cur = dbContext.Folders.FirstOrDefault(p => p.Id == cur.ParentId))?.Name == pathParts[i]);
if (i == 0)
{
rootFolder = rec;
break;
}
}
}
return rootFolder;
}
private static ProgramParameter ParseParameter(string[] args)
{
ProgramParameter programParameter = new ProgramParameter();
bool success = true;
//string connectionString = "Server={SERVER};Port={PORT};database={DATABASE};uid={UID};pwd={PWD};SslMode=None";
var opt = new OptionSet() {
{
"s|source-dir=", "source directory to upload",
v => {
if(String.IsNullOrEmpty(v))
{
Console.WriteLine("source directory cannot be empty");
success = false;
}
else
programParameter.SourceDirectory = v;
}
},
{
"n|user-name=", "upload for whom",
v =>
{
if(String.IsNullOrEmpty(v))
{
Console.WriteLine("user name cannot be empty");
success = false;
}
else
programParameter.UserName = v;
}
},
{
"p|policy-id=", "storage policy id",
v =>
{
if(String.IsNullOrEmpty(v))
{
Console.WriteLine("storage policy id cannot be empty");
success = false;
}
programParameter.StoragePolicy = Int32.Parse(v);
}
},
{
"t|target=", "base directory path",
v =>
{
if(String.IsNullOrEmpty(v))
{
Console.WriteLine("target directory cannot be empty");
success = false;
}
else if (!v.StartsWith("/"))
{
Console.WriteLine("target must start with \"/\"");
success = false;
}
else
programParameter.Target = v;
}
},
{
"c|connection-string=", "Connection string to a mysql instance",
v =>
{
if(String.IsNullOrEmpty(v))
{
Console.WriteLine("connection string cannot be empty");
success = false;
}
else
{
programParameter.ConnectionString = v;
}
}
},
{
"h|help", "show help",
v =>
{
//do nothing
}
}
};
if (args == null || args.Length == 0 || args.Any(p => p == "-h" || p == "--help"))
{
ShowHelp(opt);
return null;
}
List<string> extra;
try
{
extra = opt.Parse(args);
if (!success)
return null;
return programParameter;
}
catch (OptionException e)
{
Console.Write("invalid parameter: ");
Console.WriteLine(e.Message);
Console.WriteLine("Try `--help' for more information.");
return null;
}
}
private static void ShowHelp(OptionSet p)
{
Console.WriteLine("This tool is used to post-record data for files that not uploaded by Cloudreve but uploaded via ftp or disk copy.");
Console.WriteLine("This won't perform a real file copy but just make files under control in Cloudreve.");
Console.WriteLine("this tool only work for Cloudreve version 3.0+ (using MySQL database)");
Console.WriteLine();
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
}
}