Skip to content

Commit 6188821

Browse files
Ashley DaviesAshley Davies
Ashley Davies
authored and
Ashley Davies
committed
- Created an od-database library
- Added application configuration settings - Added FileHelpers to parse database - Added bookmarks extension (stored to csv file) - Added boomarked window - Added DateTime, Local and File Extensions - Search functions are asynchronous - Overall cleanup and optimization of project code
1 parent 3332b4b commit 6188821

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+5418
-1070
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## [0.4.6] 2019-05-27
5+
* Created an od-database library
6+
* Added application configuration settings
7+
* Added FileHelpers to parse database
8+
* Added bookmarks extension (stored to csv file)
9+
* Added boomarked window
10+
* Added DateTime, Local and File Extensions
11+
* Search functions are asynchronous
12+
* Overall cleanup and optimization of project code
13+
14+
## [0.4.5] 2019-04-02
15+
* Hoping to fix most issues with loading data
16+
* Minor improvements to help efficiency
17+
418
## [0.4.4] 2019-01-25
519
* Data is now retrieved from od-database
620
* Improved backend performance (#130)

FileMasta.Core/App.config

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
11+
<gcAllowVeryLargeObjects enabled="true" />
12+
</dependentAssembly>
13+
</assemblyBinding>
14+
</runtime>
15+
</configuration>

FileMasta.Core/Data/Bookmarks.cs

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using FileHelpers;
2+
using FileMasta.Core.Extensions;
3+
using FileMasta.Core.Models;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
8+
namespace FileMasta.Core.Data
9+
{
10+
internal class Bookmarks
11+
{
12+
private readonly List<FileItem> files = new List<FileItem>();
13+
14+
/// <summary>
15+
/// Location of bookmarked files containing records
16+
/// </summary>
17+
public string FileLocation { get; private set; }
18+
19+
/// <summary>
20+
/// Create a new users bookmarks instance
21+
/// </summary>
22+
public Bookmarks(string fileLocation)
23+
{
24+
if (File.Exists(fileLocation))
25+
{
26+
FileHelperAsyncEngine<DataItem> engine = new FileHelperAsyncEngine<DataItem>();
27+
engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;
28+
29+
using (engine.BeginReadFile(fileLocation))
30+
{
31+
foreach (DataItem file in engine)
32+
{
33+
files.Add(FileExtensions.DataItemToFile(file));
34+
}
35+
}
36+
}
37+
38+
FileLocation = fileLocation;
39+
}
40+
41+
/// <summary>
42+
/// Get the list of bookmarked files
43+
/// </summary>
44+
public IList<FileItem> GetFiles => files;
45+
46+
/// <summary>
47+
/// Search and retrieve a collection of files from the database with the specified properties
48+
/// </summary>
49+
/// <param name="name">String that contains the file name</param>
50+
/// <param name="type">Type of file to return</param>
51+
/// <param name="minSize">Minimum file size</param>
52+
/// <param name="minMTime">Minimum file modified time</param>
53+
/// <param name="maxMTime">Maximum file modified time</param>
54+
/// <returns>An enumerable collection of results with the specified parameters</returns>
55+
public IEnumerable<DataItem> Search(string name, string[] type, long minSize, DateTime minMTime, DateTime maxMTime)
56+
{
57+
FileHelperAsyncEngine<DataItem> engine = new FileHelperAsyncEngine<DataItem>();
58+
engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;
59+
List<DataItem> results = new List<DataItem>();
60+
61+
using (engine.BeginReadFile(FileLocation))
62+
{
63+
foreach (DataItem file in engine)
64+
{
65+
_ = double.TryParse(file.Mtime, out double fileTStampDbl);
66+
DateTime fileTimeStamp = DateTimeExtensions.ParseTimeStamp(fileTStampDbl);
67+
68+
if (StringExtensions.ContainsAll(file.Name.ToLower(), StringExtensions.GetWords(name.ToLower())) &&
69+
file.IsType(type) &&
70+
long.Parse(file.Size) >= minSize &&
71+
fileTimeStamp > minMTime &&
72+
fileTimeStamp < maxMTime)
73+
results.Add(file);
74+
}
75+
}
76+
77+
return results;
78+
}
79+
80+
/// <summary>
81+
/// Add a file to users bookmarks
82+
/// </summary>
83+
/// <param name="file">URL to add</param>
84+
public void Add(FileItem file)
85+
{
86+
files.Add(file);
87+
}
88+
89+
/// <summary>
90+
/// Remove a file from users bookmarks
91+
/// </summary>
92+
/// <param name="file">URL to remove</param>
93+
public void Remove(FileItem file)
94+
{
95+
files.Remove(file);
96+
}
97+
98+
/// <summary>
99+
/// Check if file exists in the bookmarks
100+
/// </summary>
101+
/// <param name="file">URL of the File</param>
102+
/// <returns>True if exists</returns>
103+
public bool IsBookmarked(FileItem file)
104+
{
105+
foreach (FileItem bookmark in files)
106+
if (bookmark.Url == file.Url)
107+
return true;
108+
return false;
109+
}
110+
111+
/// <summary>
112+
/// Remove all items from users bookmarks
113+
/// </summary>
114+
public void RemoveAll()
115+
{
116+
files.Clear();
117+
}
118+
119+
/// <summary>
120+
/// Store and update users bookmarks file
121+
/// </summary>
122+
public void UpdateCache()
123+
{
124+
if (files.Count == 0)
125+
{
126+
DeleteCache();
127+
return;
128+
}
129+
130+
FileHelperAsyncEngine<DataItem> engine = new FileHelperAsyncEngine<DataItem>();
131+
engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;
132+
133+
using (engine.BeginWriteFile(FileLocation))
134+
{
135+
foreach (FileItem file in files)
136+
{
137+
engine.WriteNext(FileExtensions.FileItemToData(file));
138+
}
139+
}
140+
}
141+
142+
/// <summary>
143+
/// Delete the bookmarks file
144+
/// </summary>
145+
public void DeleteCache()
146+
{
147+
if (File.Exists(FileLocation))
148+
File.Delete(FileLocation);
149+
}
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace FileMasta.Core.Extensions
4+
{
5+
internal abstract class DateTimeExtensions
6+
{
7+
/// <summary>
8+
/// Parse a Unix TimeStamp to DateTime
9+
/// </summary>
10+
/// <param name="timeStamp">TimeStamp in Seconds</param>
11+
/// <returns></returns>
12+
public static DateTime ParseTimeStamp(double timeStamp)
13+
{
14+
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
15+
dt = dt.AddSeconds(timeStamp).ToLocalTime();
16+
return dt;
17+
}
18+
}
19+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using FileMasta.Core.Models;
2+
using System;
3+
using System.IO;
4+
5+
namespace FileMasta.Core.Extensions
6+
{
7+
public abstract class FileExtensions
8+
{
9+
/// <summary>
10+
/// Converts a data record item to a file object with properties
11+
/// </summary>
12+
/// <param name="data"></param>
13+
/// <returns></returns>
14+
public static FileItem DataItemToFile(DataItem data)
15+
{
16+
if (data == null) return null;
17+
18+
_ = double.TryParse(
19+
(data.Mtime ?? string.Empty).ToString(),
20+
out double unixTimestamp);
21+
_ = long.TryParse(
22+
(data.Size ?? string.Empty).ToString(),
23+
out long fileSize);
24+
25+
return new FileItem(
26+
string.IsNullOrEmpty(data.Ext)
27+
? data.Name
28+
: data.Name + "." + data.Ext,
29+
fileSize,
30+
DateTimeExtensions.ParseTimeStamp(unixTimestamp),
31+
string.IsNullOrEmpty(data.Ext)
32+
? data.WebsiteUrl.Substring(0, data.WebsiteUrl.IndexOf('/', data.WebsiteUrl.IndexOf('/') + 2)) + "/" + data.Path + "/" + data.Name
33+
: data.WebsiteUrl.Substring(0, data.WebsiteUrl.IndexOf('/', data.WebsiteUrl.IndexOf('/') + 2)) + "/" + data.Path + "/" + data.Name + "." + data.Ext);
34+
35+
}
36+
37+
public static DataItem FileItemToData(FileItem file)
38+
{
39+
Uri fileUri = new Uri(file.Url);
40+
return new DataItem()
41+
{
42+
WebsiteId = "0",
43+
WebsiteUrl = fileUri.GetLeftPart(UriPartial.Authority) + "/",
44+
Path = Path.GetDirectoryName(fileUri.LocalPath).Replace(@"\", "/").TrimStart('/'),
45+
Name = Path.GetFileNameWithoutExtension(file.Name),
46+
Ext = file.GetExtension(),
47+
Size = file.Size.ToString(),
48+
Mtime = ((DateTimeOffset)file.Mtime).ToUnixTimeSeconds().ToString()
49+
};
50+
}
51+
52+
/// <summary>
53+
/// Creates a file object from the given link
54+
/// </summary>
55+
/// <param name="url"></param>
56+
/// <returns></returns>
57+
public static FileItem CreateFile(string url)
58+
{
59+
return new FileItem(
60+
Path.GetFileName(url),
61+
HttpExtensions.GetFileSize(new Uri(url)),
62+
HttpExtensions.GetFileLastModified(new Uri(url)),
63+
url);
64+
}
65+
66+
public static FileItem CreateFile(long size, string url)
67+
{
68+
return new FileItem(
69+
Path.GetFileName(url),
70+
size,
71+
HttpExtensions.GetFileLastModified(new Uri(url)),
72+
url);
73+
}
74+
75+
public static FileItem CreateFile(DateTime mTime, string url)
76+
{
77+
return new FileItem(
78+
Path.GetFileName(url),
79+
HttpExtensions.GetFileSize(new Uri(url)),
80+
mTime,
81+
url);
82+
}
83+
84+
public static FileItem CreateFile(DateTime mTime, long size, string url)
85+
{
86+
return new FileItem(
87+
Path.GetFileName(url),
88+
size,
89+
mTime,
90+
url);
91+
}
92+
93+
public static long CountLines(string fileName)
94+
{
95+
long count = -1;
96+
using (StreamReader sr = File.OpenText(fileName))
97+
{
98+
string s = "";
99+
while ((s = sr.ReadLine()) != null)
100+
{
101+
count++;
102+
}
103+
}
104+
return count;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)