/
Utils.cs
337 lines (289 loc) · 12.9 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using GChan.Data;
using GChan.Trackers;
using Microsoft.Win32;
using NLog;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Thread = GChan.Trackers.Thread;
namespace GChan
{
/// <summary>
/// Utility methods
/// </summary>
internal class Utils
{
public const string PROGRAM_NAME = "GChan";
private static readonly ILogger logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// Save settings to disk.
/// </summary>
/// <param name="path">Path to where the application downloads</param>
/// <param name="time">Thrad refresh timer in seconds</param>
/// <param name="loadHTML">Save HTML or not</param>
/// <param name="saveOnclose">Save URLs or not</param>
/// <param name="minimizeToTray">Minimize to tray or not</param>
/// <param name="closeWarn">Warn before closing or not</param>
public static void SaveSettings(
string path,
int time,
ImageFileNameFormat imageFileNameFormat,
ThreadFolderNameFormat threadFolderNameFormat,
bool loadHTML,
bool saveOnclose,
bool minimizeToTray,
bool closeWarn,
bool startWithWindows,
bool startWithWindowsMinimized,
bool addThreadSubjectToFolder,
bool addUrlFromClipboardWhenTextboxEmpty,
bool checkForUpdatesOnStart)
{
Properties.Settings.Default.SavePath = path;
Properties.Settings.Default.ScanTimer = time;
Properties.Settings.Default.ImageFilenameFormat = (byte)imageFileNameFormat;
Properties.Settings.Default.ThreadFolderNameFormat = (byte)threadFolderNameFormat;
Properties.Settings.Default.SaveHTML = loadHTML;
Properties.Settings.Default.SaveListsOnClose = saveOnclose;
Properties.Settings.Default.MinimizeToTray = minimizeToTray;
Properties.Settings.Default.WarnOnClose = closeWarn;
Properties.Settings.Default.StartWithWindowsMinimized = startWithWindowsMinimized;
Properties.Settings.Default.AddThreadSubjectToFolder = addThreadSubjectToFolder;
Properties.Settings.Default.AddUrlFromClipboardWhenTextboxEmpty = addUrlFromClipboardWhenTextboxEmpty;
Properties.Settings.Default.CheckForUpdatesOnStart = checkForUpdatesOnStart;
Properties.Settings.Default.Save();
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (startWithWindows)
registryKey.SetValue(PROGRAM_NAME, '"' + Application.ExecutablePath + '"' + (minimizeToTray && startWithWindowsMinimized ? $" {Program.TRAY_CMDLINE_ARG}" : ""));
else if (registryKey.GetValue(PROGRAM_NAME) != null)
registryKey.DeleteValue(PROGRAM_NAME);
}
/// <summary>
/// Validates if the string only contains digits
/// </summary>
/// <param name="str">String to validate</param>
public static bool IsDigitsOnly(string str)
{
foreach(char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
/// <summary>
/// Create a new Tracker (Thread or Board).
/// </summary>
public static Tracker CreateNewTracker(LoadedInfo info)
{
long greatestSaved = long.Parse(info.GreatestSaved);
Tracker tracker = CreateNewTracker(info.URL);
// TODO: Use 'is' check on type and cast at same time.
if (tracker.Type == Trackers.Type.Thread)
{
((Thread)tracker).GreatestSavedFileTim = greatestSaved;
((Thread)tracker).Subject = ((LoadedThreadInfo)info).Subject;
}
else if (tracker.Type == Trackers.Type.Board)
{
((Board)tracker).LargestAddedThreadNo = greatestSaved;
}
return tracker;
}
/// <summary>
/// Create a new Tracker (Thread or Board).
/// </summary>
public static Tracker CreateNewTracker(string url)
{
if (Thread_4Chan.UrlIsThread(url))
{
return new Thread_4Chan(url);
}
else if (Thread_8Kun.UrlIsThread(url))
{
return new Thread_8Kun(url);
}
if (Board_4Chan.UrlIsBoard(url))
{
return new Board_4Chan(url);
}
else if (Board_8Kun.UrlIsBoard(url))
{
return new Board_8Kun(url);
}
return null;
}
/// <summary>
/// Remove the reply specifier from a thread url.
/// eg "4chan.org/gif/thread/16245377#p16245377" becomes "4chan.org/gif/thread/16245377".
/// (hashtag and any following characters are trimmed).
/// </summary>
public static string PrepareURL(string url)
{
url = url.Trim();
int indexOfHash = url.IndexOf('#');
if (indexOfHash > 0)
url = url.Substring(0, indexOfHash);
return url;
}
private static string GetFilenameFromUrl(string hrefLink)
{
var parts = hrefLink.Split('/');
var fileName = parts.LastOrDefault() ?? hrefLink;
return fileName;
}
/// <returns>True if file was downloaded or already existed, false for error occured.</returns>
public static bool DownloadFile(string url, string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var fileName = GetFilenameFromUrl(url);
var fullpath = Path.Combine(directory, fileName);
try
{
if (!File.Exists(fullpath))
{
using var client = new WebClient();
client.DownloadFile(url, fullpath);
}
return true;
}
catch (Exception e)
{
// Should we throw?
logger.Log(LogLevel.Warn, e, "Exception occured downloading file.");
return false;
}
}
/// <summary>
/// New DownloadToDir overload that takes a new ImageLink class, this is used to retain the image's uploaded filename.
/// </summary>
public static bool DownloadToDir(ImageLink link, string dir)
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string destFilepath = CombinePathAndFilename(dir, link.GenerateNewFilename((ImageFileNameFormat)Properties.Settings.Default.ImageFilenameFormat));
try
{
using var webClient = new WebClient();
webClient.DownloadFile(link.URL, destFilepath);
// TODO: Figure out how to make the async downloading work properly.
// This line is currently not working and downloads 0 byte files 100% of the time.
//webClient.DownloadFileAsync(new Uri(link.URL), destFilepath);
/*Task.Run(() => {
webClient.DownloadFileAsync(new Uri(link.URL), destFilepath);
while (webClient.IsBusy)
{
//Do nothing?
}
});*/
return true;
}
catch (WebException ex)
{
logger.Error(ex, $"Error occured while downloading link {link.URL}.");
return false;
}
}
/// <summary>
/// Move thread directory for removal, based on settings.
/// </summary>
/// <exception cref="IOException"/>
/// <exception cref="UnauthorizedAccessException"/>
/// <exception cref="PathTooLongException"/>
public static void MoveThread(Thread thread)
{
string currentDirectory = thread.SaveTo.Replace("\r", "");
string subject = SanitiseSubjectString(thread.Subject);
// There are \r characters appearing from the custom subjects, TODO: need to get to the bottom of the cause of this.
var folderNameFormat = (ThreadFolderNameFormat)Properties.Settings.Default.ThreadFolderNameFormat;
var destinationDirectory = folderNameFormat switch
{
ThreadFolderNameFormat.IdSubject => $"{currentDirectory} - {subject}",
ThreadFolderNameFormat.SubjectId => Path.Combine(Path.GetDirectoryName(currentDirectory), $"{subject} - {thread.ID}"),
_ => throw new Exception($"Unknown value {folderNameFormat} for thread folder name format."),
};
destinationDirectory = destinationDirectory.Replace("\r", "").Trim('\\', '/');
if (!Directory.Exists(currentDirectory))
{
logger.Info($"While attempting to move thread {thread} the current directory could not be found, abandoning.");
return;
}
// Attmept normal directory move, if already exists add a number in bracket starting with 1 going upwards.
int number = 0;
string indexBracket() => (number == 0) ? "" : $" ({number})";
string newDirectory() => destinationDirectory + indexBracket();
while (Directory.Exists(newDirectory()))
{
number++;
}
logger.Info($"Moving thread directory '{currentDirectory}' to '{newDirectory()}'.");
Directory.Move(currentDirectory, newDirectory());
}
/// <summary>
/// Combine a path and filename with two backslashes \\ and clamp the final return out at a constant 255 length.
/// </summary>
public static string CombinePathAndFilename(string directory, string filename)
{
/// https://stackoverflow.com/a/265803/8306962
/// This could be off by a few 1s. Information that floats around is conflicted, can't be bothered to research more.
const int FILEPATH_MAX_LENGTH = 255;
string fullpath = Path.Combine(directory, filename);
if (fullpath.Length >= FILEPATH_MAX_LENGTH)
{
fullpath = fullpath.Substring(0, FILEPATH_MAX_LENGTH - Path.GetExtension(fullpath).Length) + Path.GetExtension(fullpath);
//Program.Log(true, $"destFilepath max length exceeded new destfilepath: {fullpath}. Old Length: {oldLength}. New Length: {fullpath.Length}");
}
return fullpath;
}
public static string RemoveCharactersFromString(string haystack, params char[] charactersToRemove)
{
string ret = haystack;
for (int i = 0; i < charactersToRemove.Length; i++)
{
ret = ret.Replace(charactersToRemove[i].ToString(), "");
}
return ret;
}
readonly static char[] SubjectIllegalCharacters = Path.GetInvalidFileNameChars();
/// <summary>
/// Remove a string of characters illegal for a folder name. Used for thread subjects if
/// addThreadSubjectToFolder setting is enabled.
/// </summary>
public static string SanitiseSubjectString(string subject)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < subject.Length; i++)
{
if (!SubjectIllegalCharacters.Contains(subject[i]))
{
sb.Append(subject[i]);
}
}
return sb.ToString();
}
/// <summary>
/// Method to bypass 63 character limit of NotifyIcon.Text
/// Credit: https://stackoverflow.com/a/580264/8306962
/// </summary>
public static void SetNotifyIconText(NotifyIcon ni, string text)
{
if (text.Length >= 128)
throw new ArgumentOutOfRangeException("Text limited to 127 characters");
System.Type t = typeof(NotifyIcon);
BindingFlags hidden = BindingFlags.NonPublic | BindingFlags.Instance;
t.GetField("text", hidden).SetValue(ni, text);
if ((bool)t.GetField("added", hidden).GetValue(ni))
t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });
}
}
}