-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSource.cs
62 lines (52 loc) · 2.08 KB
/
FileSource.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
namespace ShibuyaTools.Core;
public sealed class FileSource(string sourcePath, string backupPath)
{
public string FileName => Path.GetFileName(sourcePath);
public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(sourcePath);
public FileDestination Destination => new(sourcePath);
public DateTime LastWriteTimeUtc => File.GetLastWriteTimeUtc(ReadPath);
private string ReadPath => File.Exists(backupPath) ? backupPath : sourcePath;
public FileStream OpenRead() => File.OpenRead(ReadPath);
public bool CanUnroll() => File.Exists(backupPath);
public FileTarget CreateTarget(ProgressCallback<long> callback)
{
var createBackupIfNotExists = !Path.Exists(backupPath);
if (createBackupIfNotExists)
{
// Backup source if backup directory is different
var sourceDir = Path.GetDirectoryName(sourcePath);
var backupDir = Path.GetDirectoryName(backupPath);
if (sourceDir != backupDir)
{
Copy(sourcePath, backupPath, callback);
createBackupIfNotExists = false; // already backed up
}
}
return new(sourcePath, createBackupIfNotExists);
}
public void Unroll(ProgressCallback<long> callback)
{
if (File.Exists(backupPath))
{
var sourceDir = Path.GetDirectoryName(sourcePath);
var backupDir = Path.GetDirectoryName(backupPath);
// Only copy backup if the directory is different
if (sourceDir == backupDir)
{
File.Move(backupPath, sourcePath, overwrite: true);
}
else
{
Copy(backupPath, sourcePath, callback);
}
}
}
private static void Copy(string sourcePath, string targetPath, ProgressCallback<long> callback)
{
using var target = new FileTarget(targetPath);
using var source = File.OpenRead(sourcePath);
source.CopyTo(target.Stream, callback);
target.CopyFileInfo(sourcePath);
target.Commit();
}
}