-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystem.cs
189 lines (155 loc) · 4.89 KB
/
FileSystem.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
using Almostengr.VideoProcessor.Domain.Common;
using Almostengr.VideoProcessor.Domain.Interfaces;
using Almostengr.VideoProcessor.Infrastructure.FileSystem.Exceptions;
namespace Almostengr.VideoProcessor.Infrastructure.FileSystem;
public sealed class FileSystem : IFileSystem
{
private readonly Random _random;
public FileSystem()
{
_random = new Random();
}
public void CreateDirectory(string directory)
{
if (Directory.Exists(directory))
{
return;
}
Directory.CreateDirectory(directory);
}
public void CopyFile(string source, string destination, bool createDestinationDirectory = true)
{
if (createDestinationDirectory)
{
CreateDirectory(destination);
}
File.Copy(source, destination);
}
public void DeleteFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
public void DeleteDirectory(string directory)
{
if (Directory.Exists(directory))
{
Directory.Delete(directory, true);
}
}
public string GetRandomTarballFromDirectory(string directory)
{
return GetFilesInDirectory(directory)
.Where(f => f.Contains(FileExtension.Tar))
.Where(f => f.StartsWith(".") == false)
.OrderBy(f => _random.Next()).Take(1)
// .FirstOrDefault();
.First();
}
public string GetRandomSrtFileFromDirectory(string directory)
{
return GetFilesInDirectory(directory)
.Where(f => f.EndsWith(FileExtension.Srt))
.Where(f => f.StartsWith(".") == false)
.OrderBy(f => _random.Next()).Take(1)
// .FirstOrDefault();
.First();
}
public bool IsDiskSpaceAvailable(string directory)
{
const double THRESHOLD = 2.0;
DriveInfo driveInfo = new DriveInfo(directory);
double freeSpace = driveInfo.AvailableFreeSpace;
double totalSpace = driveInfo.TotalSize;
double spaceRemaining = (freeSpace / totalSpace);
if (spaceRemaining > THRESHOLD)
{
return true;
}
throw new DiskSpaceIsLowException();
}
public void MoveFile(string source, string destination, bool createDestinationDirectory = true)
{
if (createDestinationDirectory)
{
CreateDirectory(destination);
}
File.Move(source, destination);
}
public IEnumerable<string> GetFilesInDirectory(string directory)
{
return Directory.GetFiles(directory);
}
public IEnumerable<string> GetDirectoriesInDirectory(string directory)
{
return Directory.GetDirectories(directory);
}
public void PrepareAllFilesInDirectory(string directory)
{
var allFiles = GetFilesInDirectory(directory);
foreach (string childDirectory in GetDirectoriesInDirectory(directory))
{
foreach (string childFile in GetFilesInDirectory(childDirectory))
{
MoveFile(
Path.Combine(childDirectory, childFile),
Path.Combine(directory, Path.GetFileName(childFile))
);
}
}
foreach (string file in GetFilesInDirectory(directory))
{
File.Move(
file,
Path.Combine(
directory,
Path.GetFileName(file)
.ToLower()
.Replace(";", "_")
.Replace(" ", "_")
.Replace("__", "_")
.Replace("\"", string.Empty)
.Replace("\'", string.Empty))
);
}
}
public bool DoesFileExist(string filePath)
{
return File.Exists(filePath);
}
public void DeleteFiles(string[] files)
{
foreach (var file in files)
{
DeleteFile(file);
}
}
public string GetFileContents(string filePath)
{
var fileStream = new FileStream(filePath, FileMode.Open);
using (var streamReader = new StreamReader(fileStream))
{
return streamReader.ReadToEnd();
}
}
public void SaveFileContents(string filePath, string content)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new SaveFilePathIsNullOrEmptyException();
}
if (string.IsNullOrWhiteSpace(content))
{
throw new SaveFileContentIsNullOrEmptyException();
}
var directoryName = Path.GetDirectoryName(filePath);
if (string.IsNullOrEmpty(directoryName))
{
throw new SaveFileDirectoryIsNullOrEmptyException();
}
CreateDirectory(directoryName);
File.WriteAllText(filePath, content);
}
}