-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFileHelper.cs
More file actions
112 lines (96 loc) · 3.56 KB
/
FileHelper.cs
File metadata and controls
112 lines (96 loc) · 3.56 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace TensorStack.Common.Common
{
public static class FileHelper
{
public static bool DeleteFile(string filename)
{
try
{
if (!File.Exists(filename))
return false;
FileQueue.Delete(filename);
return true;
}
catch (Exception)
{
return false;
}
}
public static void DeleteFiles(params string[] filenames)
{
foreach (string filename in filenames)
{
DeleteFile(filename);
}
}
public static bool DeleteDirectory(string directory, bool recursive = true)
{
try
{
if (!Directory.Exists(directory))
return false;
Directory.Delete(directory, recursive);
return true;
}
catch (Exception)
{
return false;
}
}
public static string RandomFileName(string extension)
{
var ext = Path.HasExtension(extension) ? Path.GetExtension(extension) : extension;
return $"{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}.{ext.Trim('.')}";
}
public static string RandomFileName(string directory, string extension)
{
Directory.CreateDirectory(directory);
return Path.Combine(directory, RandomFileName(extension));
}
/// <summary>
/// Gets the URL file mapping, mapping repository file structure to local directory
/// </summary>
/// <param name="sourceUrls">The source urls.</param>
/// <param name="localDirectory">The local directory.</param>
public static Dictionary<string, string> GetUrlFileMapping(IEnumerable<string> sourceUrls, string localDirectory)
{
var files = new Dictionary<string, string>();
var repositoryUrls = sourceUrls.Select(x => new Uri(x));
var baseUrlSegmentLength = GetBaseUrlSegmentLength(repositoryUrls);
foreach (var repositoryUrl in repositoryUrls)
{
var filename = repositoryUrl.Segments.Last().Trim('\\', '/');
var subFolder = Path.Combine(repositoryUrl.Segments
.Where(x => x != repositoryUrl.Segments.Last())
.Select(x => x.Trim('\\', '/'))
.Skip(baseUrlSegmentLength)
.ToArray()) ?? string.Empty;
var destination = Path.Combine(localDirectory, subFolder);
var destinationFile = Path.Combine(destination, filename);
files.Add(repositoryUrl.OriginalString, destinationFile);
}
return files;
}
/// <summary>
/// Gets the length of the base URL segment.
/// </summary>
/// <param name="repositoryUrls">The repository urls.</param>
/// <returns></returns>
private static int GetBaseUrlSegmentLength(IEnumerable<Uri> repositoryUrls)
{
var minUrlSegmentLength = repositoryUrls.Select(x => x.Segments.Length).Min();
for (int i = 0; i < minUrlSegmentLength; i++)
{
if (repositoryUrls.Select(x => x.Segments[i]).Distinct().Count() > 1)
{
return i;
}
}
return minUrlSegmentLength;
}
}
}