-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
executable file
·121 lines (107 loc) · 4.1 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Pfs.Plex.Model;
namespace Pfs
{
public static class Utils
{
private static readonly Regex _windowsPathRegex =
new Regex(@"^[a-zA-Z]:(\\|\/|$)", RegexOptions.Compiled | RegexOptions.Singleline);
public static void CleanAndDedupe(IEnumerable<Node> items)
{
var fileNameCache = new HashSet<string>();
var invalidChars = Path.GetInvalidFileNameChars();
foreach (var item in items)
{
var sanitised = new string(item.Name.Select(c => invalidChars.Contains(c) ? '_' : c).ToArray());
var ext = Path.GetExtension(sanitised);
var name = Path.GetFileNameWithoutExtension(sanitised);
var addition = "";
var i = 0;
while (fileNameCache.Contains(name + addition + ext))
{
addition = i++ == 0 ? $" - ({item.Id})" : $" - {i}";
}
var filename = name + addition + ext;
fileNameCache.Add(filename);
item.Name = filename;
}
}
public static async Task<T> FirstSuccessfulTask<T>(IEnumerable<Task<T>> tasks)
{
var taskList = tasks.ToList();
var tcs = new TaskCompletionSource<T>();
var remainingTasks = taskList.Count;
foreach (var task in taskList)
{
task.ContinueWith(t =>
{
if (task.Status == TaskStatus.RanToCompletion)
{
tcs.TrySetResult(t.Result);
}
else if (Interlocked.Decrement(ref remainingTasks) == 0)
{
tcs.TrySetResult(default(T));
}
});
}
return await tcs.Task;
}
public static long ToUnixTimestamp(this DateTime dateTime)
{
return (long)(TimeZoneInfo.ConvertTimeToUtc(dateTime) -
new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds;
}
public static DateTime ToDateTime(this long val)
{
return DateTimeOffset.FromUnixTimeSeconds(val).DateTime;
}
public static DateTime ToDateTime(this string val)
{
return ToDateTime(long.Parse(val));
}
public class PlexDateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetInt64().ToDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public static (string directory, string filename) GetPathInfo(string path)
{
if (path.EndsWith(Path.DirectorySeparatorChar) || path.EndsWith(Path.AltDirectorySeparatorChar))
{
path = path.Substring(0, path.Length - 1);
}
var i = Math.Max(path.LastIndexOf(Path.DirectorySeparatorChar),
path.LastIndexOf(Path.AltDirectorySeparatorChar));
var folder = i <= 0 ? "/" : path.Substring(0, i);
var file = i + 2 > path.Length ? "" : path.Substring(i + 1);
return (folder, file);
}
public static string NormalisePath(string path)
{
var inputPath = _windowsPathRegex.Replace(path, "\\")
.Replace('?', '\n')
.Replace('#', '\r');
return new Uri($"file://{inputPath}")
.LocalPath
.Replace("\\", "")
.Replace('\r', '#')
.Replace('\n', '?')
.Trim();
}
}
}