Skip to content

Commit bdcd0e0

Browse files
Ashley DaviesAshley Davies
authored andcommitted
added download file
added get file contents added compare loca with web file
1 parent 7a760ff commit bdcd0e0

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

FileMasta/Extensions/WebExtensions.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Net;
35

46
namespace FileMasta.Extensions
@@ -64,6 +66,29 @@ public static bool URLExists(string url)
6466
else { return false; }
6567
}
6668

69+
/// <summary>
70+
/// Checks if local file is older than the one stored on the database
71+
/// </summary>
72+
/// <param name="webFile">String URL of the file to check for update</param>
73+
/// <param name="fileName">File name, used to check local directory</param>
74+
/// <returns></returns>
75+
public static bool IsLocalFileOld(string webFile, string fileName)
76+
{
77+
try
78+
{
79+
Program.Log.Info($"Checking if '{fileName}' needs to be updated");
80+
81+
if (File.Exists($"{LocalExtensions.PathData}{fileName}"))
82+
if (WebFileSize($"{webFile}") == new FileInfo($"{LocalExtensions.PathData}{fileName}").Length)
83+
return false;
84+
else
85+
return true;
86+
else
87+
return true;
88+
}
89+
catch (Exception ex) { Program.Log.Error($"Unable to check '{fileName}' for update, URL : {webFile}", ex); return true; }
90+
}
91+
6792
/// <summary>
6893
/// Initialize new ftp web response
6994
/// </summary>
@@ -82,14 +107,28 @@ static FtpWebResponse GetFtpReponse(string url)
82107
/// </summary>
83108
/// <param name="url">URL to request</param>
84109
/// <returns>Returns a web response from the URL</returns>
85-
static HttpWebResponse GetWebReponse(string url)
110+
public static HttpWebResponse GetWebReponse(string url)
86111
{
87112
var request = WebRequest.Create(url);
88113
request.Method = "HEAD";
89114
request.Timeout = 300000;
90115
return (HttpWebResponse)request.GetResponse();
91116
}
92117

118+
public static List<string> GetFileContents(string url)
119+
{
120+
var items = new List<string>();
121+
var request = GetRequest(url);
122+
using (WebResponse webResponse = request.GetResponse())
123+
using (var reader = new StreamReader(webResponse.GetResponseStream()))
124+
{
125+
string line;
126+
while ((line = reader.ReadLine()) != null)
127+
items.Add(line);
128+
}
129+
return items;
130+
}
131+
93132
/// <summary>
94133
/// Initialize new http web request
95134
/// </summary>
@@ -108,5 +147,20 @@ public static HttpWebRequest GetRequest(string requestUriString, string httpMeth
108147
request.Method = httpMethod;
109148
return request;
110149
}
150+
151+
/// <summary>
152+
/// Download a web file using given URL to users local hard disk
153+
/// </summary>
154+
/// <param name="url"></param>
155+
/// <param name="path"></param>
156+
public static void DownloadFile(string url, string path)
157+
{
158+
using (var wc = new WebClient())
159+
{
160+
wc.Headers.Add("Accept: text/plain");
161+
wc.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
162+
wc.DownloadFile(new Uri(url), path);
163+
}
164+
}
111165
}
112166
}

0 commit comments

Comments
 (0)