forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.cs
More file actions
383 lines (343 loc) · 14.5 KB
/
FileOperations.cs
File metadata and controls
383 lines (343 loc) · 14.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using OneScript.Contexts;
using OneScript.StandardLibrary.Collections;
using ScriptEngine.Machine.Contexts;
namespace OneScript.StandardLibrary
{
[GlobalContext(Category="Файловые операции")]
public class FileOperations : GlobalContextBase<FileOperations>
{
/// <summary>
/// Копирует файл из одного расположения в другое. Перезаписывает приемник, если он существует.
/// </summary>
/// <param name="source">Имя файла-источника</param>
/// <param name="destination">Имя файла приемника</param>
[ContextMethod("КопироватьФайл", "CopyFile")]
public void CopyFile(string source, string destination)
{
var scheme = PathScheme(source);
if(scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeHttps)
DownloadFromRemote<HttpWebRequest>(source,
destination, WebRequestMethods.Http.Get);
else if(scheme == Uri.UriSchemeFtp)
DownloadFromRemote<FtpWebRequest>(source,
destination, WebRequestMethods.Ftp.DownloadFile);
else
File.Copy(source, destination, true);
}
/// <summary>
/// Перемещает файл из одного расположения в другое.
/// </summary>
/// <param name="source">Имя файла-источника</param>
/// <param name="destination">Имя файла приемника</param>
[ContextMethod("ПереместитьФайл", "MoveFile")]
public void MoveFile(string source, string destination)
{
var scheme = PathScheme(source);
if (scheme == Uri.UriSchemeHttp || scheme == Uri.UriSchemeHttps)
{
DownloadFromRemote<HttpWebRequest>(source,
destination, WebRequestMethods.Http.Get);
DeleteFromRemote<HttpWebRequest>(source, "DELETE");
}
else if (scheme == Uri.UriSchemeFtp)
{
DownloadFromRemote<FtpWebRequest>(source,
destination, WebRequestMethods.Ftp.DownloadFile);
DeleteFromRemote<FtpWebRequest>(source, WebRequestMethods.Ftp.DeleteFile);
}
else
File.Move(source, destination);
}
public string PathScheme(string path)
{
if(Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out Uri uri) && uri.IsAbsoluteUri)
{
return uri.Scheme;
}
return Uri.UriSchemeFile;
}
private void DownloadFromRemote<T>(string source,
string destination, string method) where T: WebRequest
{
var req = (T)WebRequest.Create(source);
req.Method = method;
using (var respStream = req.GetResponse().GetResponseStream())
using (var fs = File.Create(destination))
respStream.CopyTo(fs);
}
private void DeleteFromRemote<T>(string source, string method) where T : WebRequest
{
var req = (T)WebRequest.Create(source);
req.Method = method;
using (var resp = req.GetResponse()) { };
}
/// <summary>
/// Возвращает каталог временных файлов ОС
/// </summary>
/// <returns>Строка. Путь к каталогу временных файлов</returns>
[ContextMethod("КаталогВременныхФайлов", "TempFilesDir")]
public string TempFilesDir()
{
return Path.GetTempPath();
}
/// <summary>
/// Получает имя файла во временом каталоге.
/// </summary>
/// <param name="ext">Расширение будущего файла. Если не указано, то по умолчанию расширение равно ".tmp"</param>
/// <returns>Строка. Полный путь ко временному файлу.</returns>
[ContextMethod("ПолучитьИмяВременногоФайла", "GetTempFileName")]
public string GetTempFilename(string ext = null)
{
// примитивная реализация "в лоб"
var fn = Path.GetRandomFileName();
if (ext != null && !String.IsNullOrWhiteSpace(ext))
{
if(ext[0] == '.')
fn += ext;
else
fn += "." + ext;
}
return Path.Combine(TempFilesDir(), fn);
}
/// <summary>
/// Выполняет поиск файлов по маске
/// </summary>
/// <param name="dir">Каталог, в котором выполняется поиск</param>
/// <param name="mask">Маска имени файла (включая символы * и ?)</param>
/// <param name="recursive">Флаг рекурсивного поиска в поддиректориях</param>
/// <returns>Массив объектов Файл, которые были найдены.</returns>
[ContextMethod("НайтиФайлы", "FindFiles")]
public ArrayImpl FindFiles(string dir, string mask = null, bool recursive = false)
{
if (mask == null)
{
// fix 225, 227, 228
var fObj = new FileContext(dir);
if(fObj.Exists())
{
return new ArrayImpl(new[] { fObj });
}
else
{
return new ArrayImpl();
}
}
else if (File.Exists(dir))
{
return new ArrayImpl();
}
if(!Directory.Exists(dir))
return new ArrayImpl();
var filesFound = FindFilesV8Compatible(dir, mask, recursive);
return new ArrayImpl(filesFound);
}
private static IEnumerable<FileContext> FindFilesV8Compatible(string dir, string mask, bool recursive)
{
var collectedFiles = new List<FileContext>();
IEnumerable<FileContext> entries;
IEnumerable<FileContext> folders = null;
try
{
if (recursive)
folders = Directory.GetDirectories(dir).Select(x => new FileContext(x));
entries = Directory.EnumerateFileSystemEntries(dir, mask)
.Select(x => new FileContext(x));
}
catch (SecurityException)
{
return collectedFiles;
}
catch (UnauthorizedAccessException)
{
return collectedFiles;
}
if (recursive)
{
foreach (var fileFound in entries)
{
try
{
var attrs = fileFound.GetAttributes();
if (attrs.HasFlag(FileAttributes.ReparsePoint))
{
collectedFiles.Add(fileFound);
continue;
}
}
catch (SecurityException)
{
continue;
}
catch (UnauthorizedAccessException)
{
continue;
}
collectedFiles.Add(fileFound);
}
foreach (var folder in folders)
{
try
{
var attrs = folder.GetAttributes();
if (!attrs.HasFlag(FileAttributes.ReparsePoint))
{
collectedFiles.AddRange(FindFilesV8Compatible(folder.FullName, mask, true));
}
}
catch (SecurityException)
{
}
catch (UnauthorizedAccessException)
{
}
}
}
else
{
collectedFiles.AddRange(entries);
}
return collectedFiles;
}
/// <summary>
/// Удаление файлов
/// </summary>
/// <param name="path">Каталог из которого удаляются файлы, или сам файл.</param>
/// <param name="mask">Маска файлов. Необязательный параметр. Если указан, то первый параметр трактуется, как каталог.</param>
[ContextMethod("УдалитьФайлы", "DeleteFiles")]
public void DeleteFiles(string path, string mask = null)
{
if (mask == null)
{
if (Directory.Exists(path))
{
System.IO.Directory.Delete(path, true);
}
else
{
System.IO.File.Delete(path);
}
}
else
{
// bugfix #419
if (!Directory.Exists(path))
return;
var entries = System.IO.Directory.EnumerateFileSystemEntries(path, mask)
.AsParallel()
.ToArray();
foreach (var item in entries)
{
System.IO.FileInfo finfo = new System.IO.FileInfo(item);
if (finfo.Attributes.HasFlag(System.IO.FileAttributes.Directory))
{
//recursively delete directory
DeleteDirectory(item, true);
}
else
{
System.IO.File.Delete(item);
}
}
}
}
public static void DeleteDirectory(string path, bool recursive)
{
if (recursive)
{
var subfolders = Directory.GetDirectories(path);
foreach (var s in subfolders)
{
DeleteDirectory(s, true);
}
}
var files = Directory.GetFiles(path);
foreach (var f in files)
{
File.Delete(f);
}
Directory.Delete(path);
}
/// <summary>
/// Создать каталог
/// </summary>
/// <param name="path">Имя нового каталога</param>
[ContextMethod("СоздатьКаталог", "CreateDirectory")]
public void CreateDirectory(string path)
{
System.IO.Directory.CreateDirectory(path);
}
/// <summary>
/// Получить текущий каталог
/// </summary>
[ContextMethod("ТекущийКаталог", "CurrentDirectory")]
public string CurrentDirectory()
{
return System.IO.Directory.GetCurrentDirectory();
}
/// <summary>
/// Установить каталог текущим
/// </summary>
/// <param name="path">Имя нового текущего каталога</param>
[ContextMethod("УстановитьТекущийКаталог", "SetCurrentDirectory")]
public void SetCurrentDirectory(string path)
{
System.IO.Directory.SetCurrentDirectory(path);
}
/// <summary>
/// Получает разделитель пути в соответствии с текущей операционной системой
/// </summary>
[ContextMethod("ПолучитьРазделительПути","GetPathSeparator")]
public string GetPathSeparator()
{
return new string(new char[]{Path.DirectorySeparatorChar});
}
/// <summary>
/// Получает маску "все файлы" для текущей операционной системы.
/// В Windows маска принимает значение "*.*", в nix - "*".
/// </summary>
[ContextMethod("ПолучитьМаскуВсеФайлы", "GetAllFilesMask")]
public string GetAllFilesMask()
{
var platform = System.Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT || platform == PlatformID.Win32Windows)
return "*.*";
else
return "*";
}
/// <summary>
/// Объединяет компоненты файлового пути, с учетом разделителей, принятых в данной ОС.
/// При этом корректно, без дублирования, обрабатываются уже существующие разделители пути.
/// </summary>
/// <param name="path1">Первая часть пути</param>
/// <param name="path2">Вторая часть пути</param>
/// <param name="path3">Третья часть пути (необязательно)</param>
/// <param name="path4">Четвертая часть пути (необязательно)</param>
/// <returns>Объединенный путь.</returns>
[ContextMethod("ОбъединитьПути", "CombinePath")]
public string CombinePath(string path1, string path2, string path3 = null, string path4 = null)
{
if (path3 == null)
return Path.Combine(path1, path2);
else if (path4 == null)
return Path.Combine(path1, path2, path3);
else
return Path.Combine(path1, path2, path3, path4);
}
public static IAttachableContext CreateInstance()
{
return new FileOperations();
}
}
}