Skip to content

Commit

Permalink
Fixed get content disposition filename of issue #23
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Dec 6, 2020
1 parent d6fd813 commit b419fe2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Downloader.Sample/DownloadList.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//{ "FileName": "D:\\TestDownload\\H2017.06.06.0000.0001a.patch", "Url": "http://patch-dl.ffxiv.com/game/4e9a232b/H2017.06.06.0000.0001a.patch" },
//{ "FileName": "D:\\TestDownload\\2020.rar", "Url": "http://test.download.pokecity.club/2020.rar" },
//{ "FileName": "D:\\TestDownload\\test.pdf", "Url": "https://download.taaghche.com/download/kaaurZ7Rj7yQQrd4P7MWwUqtTInMypDA" },
{ "FolderPath": "D:\\TestDownload", "Url": "https://raw.githubusercontent.com/bezzad/Downloader/master/src/Downloader.Test/Assets/excel_sample.xls?test=1" },
//{ "FolderPath": "D:\\TestDownload", "Url": "https://raw.githubusercontent.com/bezzad/Downloader/master/src/Downloader.Test/Assets/excel_sample.xls?test=1" },
{ "FolderPath": "D:\\TestDownload", "Url": "https://5743-zanjan.medu.ir/portal/fileLoader.php?code=0a1c53101df0e93134a7771606813229" },
{ "FolderPath": "D:\\TestDownload", "Url": "http://ipv4.download.thinkbroadband.com/100MB.zip" }
]
34 changes: 32 additions & 2 deletions src/Downloader.Test/RequestTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;

namespace Downloader.Test
{
Expand All @@ -22,6 +22,25 @@ public void GetFileNameTest()
Assert.AreEqual(new Request("http://www.a.com/a/b/c/d/e/").GetFileName(), ""); // ""
}

[TestMethod]
public void GetUrlDispositionFilenameAsyncTest()
{
Assert.IsNull(new Request("").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("test").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("test.xml").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("/test.xml").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("/test.xml?q=1").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("/test.xml?q=1&x=3").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("test.xml?q=1&x=3").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("http://www.a.com/test.xml?q=1&x=3").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("http://www.a.com/test.xml?q=1&x=3#aidjsf").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("http://www.a.com/a/b/c/d").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("http://www.a.com/a/b/c/d/e/").GetUrlDispositionFilenameAsync().Result);
Assert.IsNull(new Request("https://raw.githubusercontent.com/bezzad/Downloader/master/src/Downloader.Test/Assets/excel_sample.xls?test=1").GetUrlDispositionFilenameAsync().Result);
Assert.AreEqual("مسابقه‌ی ایده‌ی کسب و کار دانش‌آموزی در ایام کرونا کلیه‌ی مدارس دوره‌ی دوم متوسطه.rar",
new Request("https://5743-zanjan.medu.ir/portal/fileLoader.php?code=0a1c53101df0e93134a7771606813229").GetUrlDispositionFilenameAsync().Result);
}

[TestMethod]
public void GetFileSizeTest()
{
Expand All @@ -32,5 +51,16 @@ public void GetFileSizeTest()
Assert.AreEqual(DownloadTestHelper.FileSize10Mb, new Request(DownloadTestHelper.File10MbUrl).GetFileSize().Result);
Assert.AreEqual(DownloadTestHelper.FileSize100Mb, new Request(DownloadTestHelper.File100MbUrl).GetFileSize().Result);
}

[TestMethod]
public void ToUnicodeTest()
{
var requestInstance = new Request("");
var encodingLatin1 = Encoding.GetEncoding("iso-8859-1");
Assert.AreEqual("test1", requestInstance.ToUnicode(encodingLatin1.GetString(Encoding.UTF8.GetBytes("test1"))));
Assert.AreEqual("متن تستی.ext", requestInstance.ToUnicode(encodingLatin1.GetString(Encoding.UTF8.GetBytes("متن تستی.ext"))));
Assert.AreEqual("متن تستی.ext", requestInstance.ToUnicode(encodingLatin1.GetString(Encoding.UTF8.GetBytes("متن تستی.ext"))));
Assert.AreEqual("متن تستی1230456789.ext", requestInstance.ToUnicode(encodingLatin1.GetString(Encoding.UTF8.GetBytes("متن تستی1230456789.ext"))));
}
}
}
3 changes: 2 additions & 1 deletion src/Downloader/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public async Task DownloadFileAsync(string address, DirectoryInfo folder)
{
InitialBegin(address);
folder.Create();
Package.FileName = Path.Combine(folder.FullName, RequestInstance.GetFileName());
var filename = await RequestInstance.GetUrlDispositionFilenameAsync() ?? RequestInstance.GetFileName();
Package.FileName = Path.Combine(folder.FullName, filename);

await StartDownload();
}
Expand Down
58 changes: 48 additions & 10 deletions src/Downloader/Request.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Downloader
{
Expand Down Expand Up @@ -31,6 +34,7 @@ protected HttpWebRequest GetRequest(string method)
var request = (HttpWebRequest)WebRequest.CreateDefault(Address);
request.Timeout = -1;
request.Method = method;
request.ContentType = "application/x-www-form-urlencoded;charset=utf-8;";
request.Accept = Configuration.Accept;
request.KeepAlive = Configuration.KeepAlive;
request.AllowAutoRedirect = Configuration.AllowAutoRedirect;
Expand Down Expand Up @@ -64,7 +68,7 @@ protected async Task FetchResponseHeaders()
{
if (ResponseHeaders.Any())
return;

var response = await GetRequest().GetResponseAsync();
if (response?.SupportsHeaders == true)
{
Expand All @@ -90,15 +94,6 @@ public async Task<long> GetFileSize()
}
public string GetFileName()
{
// await FetchResponseHeaders();
// var contentLengthKey = "Content-Length";
// if (ResponseHeaders.TryGetValue(contentLengthKey, out var contentLengthText))
// {
// if (long.TryParse(contentLengthText, out var contentLength))
// {
// return contentLength;
// }
// }
var filename = Path.GetFileName(Address.LocalPath);
var queryIndex = filename.IndexOf("?", StringComparison.Ordinal);
if (queryIndex >= 0)
Expand All @@ -107,5 +102,48 @@ public string GetFileName()
}
return filename;
}

public async Task<string> GetUrlDispositionFilenameAsync()
{
try
{
if (Address?.IsWellFormedOriginalString() == true
&& Address?.Segments.Length > 1)
{
await FetchResponseHeaders();
if (ResponseHeaders.TryGetValue(HeaderContentDispositionKey, out var disposition))
{
var unicodeDisposition = ToUnicode(disposition);
if (string.IsNullOrWhiteSpace(unicodeDisposition) == false)
{
var filenameStartPointKey = "filename=";
var dispositionParts = unicodeDisposition.Split(';');
var filenamePart = dispositionParts.FirstOrDefault(part => part.Trim().StartsWith(filenameStartPointKey, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrWhiteSpace(filenamePart) == false)
{
var filename = filenamePart.Replace(filenameStartPointKey, "")
.Replace("\"", "").Trim();

return filename;
}
}
}
}
}
catch (WebException e)
{
Debug.WriteLine(e);
// No matter in this point
}

return null;
}

public string ToUnicode(string otherEncodedText)
{
// decode 'latin-1' to 'utf-8'
var unicode = Encoding.UTF8.GetString(Encoding.GetEncoding("iso-8859-1").GetBytes(otherEncodedText));
return unicode;
}
}
}

0 comments on commit b419fe2

Please sign in to comment.