-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIoAccessProvider.cs
More file actions
119 lines (107 loc) · 4.73 KB
/
FileIoAccessProvider.cs
File metadata and controls
119 lines (107 loc) · 4.73 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
using FileSyncLibNet.Commons;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace FileSyncLibNet.AccessProviders
{
internal class FileIoAccessProvider : IAccessProvider
{
public string AccessPath { get; private set; }
private readonly RemoteState remoteState;
private readonly ILogger logger;
private const int DefaultBandwidthLimit = 100 * 1024 * 1024; // 100 MB/s
private int bandwidthLimit = DefaultBandwidthLimit;
public FileIoAccessProvider(ILogger logger, string stateFilename)
{
this.logger = logger;
if (!string.IsNullOrEmpty(stateFilename))
remoteState = new RemoteState(stateFilename);
}
public void UpdateAccessPath(string accessPath)
{
AccessPath = accessPath;
}
public void CreateDirectory(string path)
{
Directory.CreateDirectory(Path.Combine(AccessPath, path));
}
public FileInfo2 GetFileInfo(string path)
{
if (null != remoteState)
return remoteState.GetFileInfo(Path.Combine(AccessPath, path));
var fi = new FileInfo(Path.Combine(AccessPath, path));
return new FileInfo2(path, fi.Exists)
{
LastWriteTime = fi.Exists ? new DateTime(fi.LastWriteTime.Ticks) : DateTime.MinValue,
Length = fi.Exists ? fi.Length : 0
};
}
public List<FileInfo2> GetFiles(DateTime minimumLastWriteTime, string pattern, string path = null, bool recursive = false, List<string> subfolders = null, bool olderFiles = false)
{
DirectoryInfo _di = new DirectoryInfo(AccessPath);
List<FileInfo2> ret_val = new List<FileInfo2>();
foreach (var dir in (subfolders != null && subfolders.Count > 0) ? _di.GetDirectories() : new[] { _di })
{
try
{
if (subfolders != null && subfolders.Count > 0 && !subfolders.Select(x => x.ToLower()).Contains(dir.Name.ToLower()))
continue;
var _fi = dir.EnumerateFiles(
searchPattern: pattern,
searchOption: recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
ret_val.AddRange(_fi.Where(x => (olderFiles ? x.LastWriteTime <= minimumLastWriteTime : x.LastWriteTime >= minimumLastWriteTime))
.Select(x => new FileInfo2(x.FullName.Substring(AccessPath.Length + 1), x.Exists) { Length = x.Length, LastWriteTime = x.LastWriteTime })
.ToList());
}
catch (Exception exc)
{
logger?.LogError(exc, "exception in GetFiles for path {A}, dir {B}", path, dir);
}
}
return ret_val;
}
public Stream GetStream(FileInfo2 file)
{
var realFilename = Path.Combine(AccessPath, file.Name);
return File.OpenRead(realFilename);
}
public void WriteFile(FileInfo2 file, Stream content)
{
var realFilename = Path.Combine(AccessPath, file.Name);
Directory.CreateDirectory(Path.GetDirectoryName(realFilename));
if (bandwidthLimit == DefaultBandwidthLimit)
{
using (var stream = File.Create(realFilename))
{
content.CopyTo(stream);
}
}
else
{
using (var stream = new FileStream(realFilename, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.None))
{
int sleepIntervalMs = 50; // Fixed sleep interval
int bufferSize = bandwidthLimit / (1000 / sleepIntervalMs); // Calculate buffer size based on bandwidth limit and interval
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = content.Read(buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, bytesRead);
Thread.Sleep(sleepIntervalMs); // Sleep for the fixed interval
}
}
}
File.SetLastWriteTime(realFilename, file.LastWriteTime);
remoteState?.SetFileInfo(realFilename, file);
}
public void Delete(FileInfo2 fileInfo)
{
var realFilename = Path.Combine(AccessPath, fileInfo.Name);
File.Delete(realFilename);
remoteState?.RemoveFileInfo(realFilename);
}
}
}