forked from revenz/FileFlows
/
FileHelper.cs
107 lines (98 loc) · 3.67 KB
/
FileHelper.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
using System.Diagnostics;
namespace FileFlows.ServerShared.Helpers;
/// <summary>
/// A helper for interacting with files
/// </summary>
public class FileHelper
{
/// <summary>
/// Removes illegal file/path characters from a string
/// </summary>
/// <param name="input">the string to clean</param>
/// <returns>the original string with all illegal characters removed</returns>
public static string RemoveIllegalCharacters(string input)
{
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
foreach (var c in invalid)
input = input.Replace(c.ToString(), string.Empty);
return input;
}
/// <summary>
/// Calculates a fingerprint for a file
/// </summary>
/// <param name="file">The filename</param>
/// <returns>The fingerprint</returns>
public static string CalculateFingerprint(string file)
{
try
{
var fileInfo = new FileInfo(file);
if (fileInfo.Exists == false)
return string.Empty;
using var hasher = System.Security.Cryptography.SHA256.Create();
byte[] hash;
if (fileInfo.Length > 100_000_000)
{
// compute hash on first 100MB to speed it update
using var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
var bytes = new byte[100_000_000];
int realLength = stream.Read(bytes, 0, bytes.Length);
hash = hasher.ComputeHash(bytes, 0, realLength);
}
else
{
using var stream = FileOpenHelper.OpenRead_NoLocks(file);
hash = hasher.ComputeHash(stream);
}
string hashStr = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
return hashStr;
}
catch (Exception ex)
{
Logger.Instance?.ELog($"Failed to calculate fingerprint for file '{file}': {ex.Message}{Environment.NewLine}{ex.StackTrace}");
return string.Empty;
}
finally
{
GC.Collect();
}
}
/// <summary>
/// Makes a file executable on linux
/// </summary>
/// <param name="file">the file to make executable</param>
/// <returns>if successful or not</returns>
public static bool MakeExecutable(string file)
{
try
{
var fi = new FileInfo(file);
using var process = new Process();
process.StartInfo = new ProcessStartInfo("/bin/bash", $"-c \"chmod +x {fi.Name}\"")
{
WorkingDirectory = fi.DirectoryName,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
process.Start();
string output = process.StandardError.ReadToEnd();
Console.WriteLine(output);
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
return true;
Logger.Instance?.ELog($"Failed making executable:" + process.StartInfo.FileName,
process.StartInfo.Arguments + Environment.NewLine + output);
if (string.IsNullOrWhiteSpace(error) == false)
Logger.Instance?.ELog($"Making Executable error output:" + output);
return false;
}
catch (Exception ex)
{
Logger.Instance?.ELog($"Failed making executable: " + file + " => " + ex.Message);
return false;
}
}
}