Skip to content

Commit 2489b05

Browse files
Ashley DaviesAshley Davies
authored andcommitted
Refactored entire backend to improve performance
1 parent 0f8d806 commit 2489b05

26 files changed

+3631
-521
lines changed

FileMasta/App.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@
3030
</setting>
3131
</FileMasta.Properties.Settings>
3232
</userSettings>
33+
<appSettings>
34+
<add key="url.project"
35+
value="https://github.com/mostlyash/filemasta/"/>
36+
<add key="fileUrl.changelog"
37+
value="https://raw.githubusercontent.com/mostlyash/filemasta/master/CHANGELOG.md"/>
38+
<add key="fileUrl.version"
39+
value="https://dl.dropbox.com/s/ioiu8vwxxc9r430/app-version.txt?raw=true"/>
40+
<add key="fileUrl.database"
41+
value="https://www.dropbox.com/s/8ba7wbwow2mnq1l/od-database.csv?raw=true"/>
42+
<add key="fileName.database"
43+
value="od-database.csv"/>
44+
<add key="fileName.saved"
45+
value="saved.txt"/>
46+
</appSettings>
3347
</configuration>

FileMasta/Data/DataHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ internal static class DataHelper
1010
{
1111
private static string AppDataPath { get; } = $@"{Application.UserAppDataPath}\";
1212
public static string DatabaseFilePath { get; } = $@"{AppDataPath}{AppExtensions.DatabaseFilename}";
13-
public static string BookmarkedFilePath { get; } = $@"{AppDataPath}{AppExtensions.BookmarkedFilename}";
13+
public static string SavedFilePath { get; } = $@"{AppDataPath}{AppExtensions.SavedFilename}";
1414

15-
public static WebFile CreateWebFile(string fileUrl)
15+
public static WebFile CreateFile(string fileUrl)
1616
{
1717
return new WebFile(Path.GetFileName(fileUrl), FtpExtensions.GetFileSize(fileUrl), FtpExtensions.GetFileLastModified(fileUrl), fileUrl);
1818
}
1919

20+
public static void RemoveSavedFile()
21+
{
22+
if (File.Exists(SavedFilePath)) File.Delete(SavedFilePath);
23+
}
24+
2025
/// <summary>
2126
/// Retrieve list of keywords from database
2227
/// </summary>

FileMasta/Data/Database.cs

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public class Database
1515
private readonly List<WebFile> _dbFiles = new List<WebFile>();
1616

1717
/// <summary>
18-
/// Contains the users bookmarked files url
18+
/// Contains the users saved files url
1919
/// </summary>
20-
private readonly List<string> _bookmarkedFiles = new List<string>();
20+
private readonly List<string> _savedFiles = new List<string>();
2121

2222
/// <summary>
2323
/// Contains the metadata of the database
@@ -32,26 +32,22 @@ public Database()
3232
if (!HttpExtensions.IsFileSizeEqual(DataHelper.DatabaseFilePath, AppExtensions.DatabaseUrl))
3333
HttpExtensions.DownloadFile(AppExtensions.DatabaseUrl, DataHelper.DatabaseFilePath);
3434

35-
var totalNoFiles = 0;
36-
long totalFileSize = 0;
37-
38-
if (!File.Exists(DataHelper.BookmarkedFilePath))
39-
File.Create(DataHelper.BookmarkedFilePath);
40-
41-
if (!string.IsNullOrEmpty(File.ReadAllText(DataHelper.BookmarkedFilePath)))
35+
if (File.Exists(DataHelper.SavedFilePath))
4236
{
43-
using (var fs = File.Open(DataHelper.BookmarkedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
37+
using (var fs = File.Open(DataHelper.SavedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
4438
using (var bs = new BufferedStream(fs))
4539
using (var sr = new StreamReader(bs))
4640
{
4741
string s;
4842
while ((s = sr.ReadLine()) != null)
4943
{
50-
_bookmarkedFiles.Add(s);
44+
_savedFiles.Add(s);
5145
}
5246
}
5347
}
54-
48+
49+
var totalNoFiles = 0;
50+
long totalFileSize = 0;
5551
using (var fs = File.Open(DataHelper.DatabaseFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
5652
using (var bs = new BufferedStream(fs))
5753
using (var sr = new StreamReader(bs))
@@ -60,6 +56,7 @@ public Database()
6056
string s;
6157
while ((s = sr.ReadLine()) != null)
6258
{
59+
// Messy way to split csv into a file object
6360
var lineParts = s.Split(',');
6461
var fileSize = long.Parse(lineParts[0]);
6562
var fileLastModified = DateTime.Parse(lineParts[1]);
@@ -117,16 +114,16 @@ where webFile.Url.Equals(fileUrl)
117114
private static readonly object SearchLock = new object();
118115

119116
/// <summary>
120-
/// Search files and Bookmarked from the database
117+
/// Search files from the database
121118
/// </summary>
122119
/// <param name="sort">Sort results by property</param>
123120
/// <param name="name">File name to match words/terms</param>
124121
/// <param name="type">File type to filter</param>
125-
/// <param name="minimumSize">More than or equal to file size in bytes</param>
126-
/// <param name="lastModifiedMin">Last modified minimum file date</param>
127-
/// <param name="lastModifiedMax">Last modified maximum file date</param>
122+
/// <param name="minSize">More than or equal to file size in bytes</param>
123+
/// <param name="minDateModified">Last modified minimum file date</param>
124+
/// <param name="maxDateModified">Last modified maximum file date</param>
128125
/// <returns>Returns a list of matching files with the specified parameters</returns>
129-
public List<WebFile> Search(Sort sort, string name, string[] type, long minimumSize, DateTime lastModifiedMin, DateTime lastModifiedMax)
126+
public List<WebFile> Search(Sort sort, string name, string[] type, long minSize, DateTime minDateModified, DateTime maxDateModified)
130127
{
131128
lock (SearchLock)
132129
{
@@ -135,10 +132,10 @@ public List<WebFile> Search(Sort sort, string name, string[] type, long minimumS
135132
where StringExtensions.ContainsAll(Uri.UnescapeDataString(webFile.Url.ToLower()),
136133
StringExtensions.GetWords(name.ToLower())) &&
137134
webFile.IsType(type) &&
138-
webFile.Size >= minimumSize &&
139-
webFile.LastModified > lastModifiedMin &&
140-
webFile.LastModified < lastModifiedMax
141-
select webFile).ToList();
135+
webFile.Size >= minSize &&
136+
webFile.LastModified > minDateModified &&
137+
webFile.LastModified < maxDateModified
138+
select webFile).ToList();
142139
}
143140
}
144141

@@ -184,5 +181,71 @@ private void Sort(Sort sortBy, bool sortReverse = false)
184181
});
185182
}
186183
}
184+
185+
/// <summary>
186+
/// Search files from the database
187+
/// </summary>
188+
/// <returns>Returns a list of matching files with the specified parameters</returns>
189+
public List<WebFile> SavedFiles()
190+
{
191+
lock (SearchLock)
192+
{
193+
return (from webFile in _savedFiles
194+
let file = GetFile(webFile)
195+
select file).ToList();
196+
}
197+
}
198+
199+
/// <summary>
200+
/// Save the specified file to users the saved list
201+
/// </summary>
202+
/// <param name="fileUrl">URL to add</param>
203+
public void AddToSaved(string fileUrl)
204+
{
205+
_savedFiles.Add(fileUrl);
206+
}
207+
208+
/// <summary>
209+
/// Remove the specified file from the users saved list
210+
/// </summary>
211+
/// <param name="fileUrl">URL to remove</param>
212+
public void RemoveFromSaved(string fileUrl)
213+
{
214+
_savedFiles.Remove(fileUrl);
215+
}
216+
217+
/// <summary>
218+
/// Check if user has saved the specified file
219+
/// </summary>
220+
/// <param name="fileUrl">URL of the File</param>
221+
/// <returns>True if exists</returns>
222+
public bool IsFileSaved(string fileUrl)
223+
{
224+
foreach (var file in _savedFiles)
225+
if (file == fileUrl)
226+
return true;
227+
return false;
228+
}
229+
230+
/// <summary>
231+
/// Remove all saved file urls
232+
/// </summary>
233+
public void ClearSaved()
234+
{
235+
_savedFiles.Clear();
236+
}
237+
238+
/// <summary>
239+
/// Save/update saved files to a local file
240+
/// </summary>
241+
public void UpdateSavedFile()
242+
{
243+
if (_savedFiles.Count == 0) { DataHelper.RemoveSavedFile(); return; }
244+
using (var fs = File.OpenWrite(DataHelper.SavedFilePath))
245+
using (var bs = new BufferedStream(fs))
246+
using (var sw = new StreamWriter(bs))
247+
foreach (var fileUrl in _savedFiles)
248+
sw.WriteLine(fileUrl);
249+
}
187250
}
188251
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Configuration;
2+
3+
namespace FileMasta.Extensions
4+
{
5+
internal abstract class AppExtensions
6+
{
7+
/// <summary>
8+
/// Retrieve the url of the project hosted on GitHub
9+
/// </summary>
10+
public static readonly string ProjectUrl = ConfigurationManager.AppSettings["url.project"];
11+
12+
/// <summary>
13+
/// Retrieve the url of the project's change log file
14+
/// </summary>
15+
public static readonly string ChangelogUrl = ConfigurationManager.AppSettings["fileUrl.changelog"];
16+
17+
/// <summary>
18+
/// Retrieve the url containing the latest project version
19+
/// </summary>
20+
public static readonly string VersionUrl = ConfigurationManager.AppSettings["fileUrl.version"];
21+
22+
/// <summary>
23+
/// Retrieve the url pointing to the database location
24+
/// </summary>
25+
public static readonly string DatabaseUrl = ConfigurationManager.AppSettings["fileUrl.database"];
26+
27+
/// <summary>
28+
/// Retrieve the file name of the database
29+
/// </summary>
30+
public static readonly string DatabaseFilename = ConfigurationManager.AppSettings["fileName.database"];
31+
32+
/// <summary>
33+
/// Retrieve the file name of the users bookmarked data
34+
/// </summary>
35+
public static readonly string SavedFilename = ConfigurationManager.AppSettings["fileName.saved"];
36+
}
37+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Drawing;
3+
using System.IO;
4+
using System.Windows.Forms;
5+
using FileMasta.Forms;
6+
7+
namespace FileMasta.Extensions
8+
{
9+
internal abstract class ControlExtensions
10+
{
11+
/// <summary>
12+
/// Shows raw text from the specified URL in a window box
13+
/// </summary>
14+
/// <param name="owner">Text to display in title</param>
15+
/// <param name="title">Text to display in title</param>
16+
/// <param name="url">URL to fetch string data from</param>
17+
public static void ShowDataWindow(Form owner, string title, string url)
18+
{
19+
var frmInfo = new DataViewDialog { Text = title };
20+
21+
//using (var client = Program._webClient)
22+
using (var stream = Program.WebClient.OpenRead(url))
23+
using (var reader = new StreamReader(stream ?? throw new InvalidOperationException("Unable to read data")))
24+
frmInfo.labelData.Text = reader.ReadToEnd();
25+
26+
frmInfo.MaximumSize = new Size(frmInfo.MaximumSize.Width, owner.Height - 100);
27+
frmInfo.ShowDialog(owner);
28+
}
29+
30+
/// <summary>
31+
/// Change button text and auto size
32+
/// </summary>
33+
/// <param name="ctrl">Control to set text</param>
34+
/// <param name="text">Text to set to control</param>
35+
public static void SetControlTextWidth(Button ctrl, string text)
36+
{
37+
ctrl.Text = text;
38+
var myFont = new Font(ctrl.Font.FontFamily, ctrl.Font.Size);
39+
var mySize = ctrl.CreateGraphics().MeasureString(ctrl.Text, myFont);
40+
ctrl.Width = (int)Math.Round(mySize.Width, 0) + 22;
41+
ctrl.Refresh();
42+
}
43+
44+
/// <summary>
45+
/// Create a keyword label from template
46+
/// </summary>
47+
/// <param name="text">Most Search text/value</param>
48+
/// <param name="clickEvent">Most Search text/value</param>
49+
/// <returns></returns>
50+
public static Label KeywordLabel(string text, EventHandler clickEvent)
51+
{
52+
var a = new Label
53+
{
54+
Text = text,
55+
TextAlign = ContentAlignment.MiddleCenter,
56+
Font = new Font("Verdana", 8.25F, FontStyle.Regular | FontStyle.Underline),
57+
AutoSize = true,
58+
BackColor = Color.Transparent,
59+
ForeColor = Color.Black,
60+
Margin = new Padding(0, 0, 0, 1),
61+
Cursor = Cursors.Hand,
62+
Name = "LabelKeyword",
63+
};
64+
65+
// Click event performs a search in main form
66+
a.Click += clickEvent;
67+
68+
return a;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)