Skip to content

Commit

Permalink
Merge pull request #51 from NguyenDanPhuong/develop
Browse files Browse the repository at this point in the history
Fix cannot save manga to another disk volume
  • Loading branch information
NguyenDanPhuong authored Feb 25, 2017
2 parents 9d315c4 + 4cba61e commit 03ca348
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 43 deletions.
13 changes: 9 additions & 4 deletions MangaRipper.Core/Controllers/WorkerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MangaRipper.Core.Extensions;

namespace MangaRipper.Core.Controllers
{
Expand All @@ -23,7 +24,7 @@ public class WorkerController
CancellationTokenSource _source;
readonly SemaphoreSlim _sema;

private enum ImageExtensions { jpeg, jpg, png, gif };
private enum ImageExtensions { Jpeg, Jpg, Png, Gif };

public WorkerController()
{
Expand Down Expand Up @@ -119,7 +120,11 @@ private async Task DownloadChapterInternal(DownloadChapterTask task, string mang

if (task.Formats.Contains(OutputFormat.Folder))
{
Directory.Move(tempFolder, finalFolder);
if (!Directory.Exists(finalFolder))
{
Directory.CreateDirectory(finalFolder);
}
ExtensionHelper.SuperMove(tempFolder, finalFolder);
}
if (task.Formats.Contains(OutputFormat.CBZ))
{
Expand Down Expand Up @@ -170,11 +175,11 @@ private string GetFilenameFromUrl(string url, int imageNum)

// if everything in parameters and path is incorrect
// e.g. https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?container=focus&gadget=a&no_expand=1&resize_h=0&rewriteMime=image%2F*&url=http%3a%2f%2f2.p.mpcdn.net%2f50%2f531513%2f1.jpg&imgmax=30000
string extension = path.Split('.').FirstOrDefault(x => Enum.GetNames(typeof(ImageExtensions)).Contains(x));
string extension = path.Split('.').FirstOrDefault(x => Enum.GetNames(typeof(ImageExtensions)).Contains(x, StringComparer.OrdinalIgnoreCase));
if (extension == null)
{
nameInParam = !nameInParam;
extension = uri.PathAndQuery.Split('.', '&').FirstOrDefault(x => Enum.GetNames(typeof(ImageExtensions)).Contains(x));
extension = uri.PathAndQuery.Split('.', '&').FirstOrDefault(x => Enum.GetNames(typeof(ImageExtensions)).Contains(x, StringComparer.OrdinalIgnoreCase));
}

// Some names - just a gibberish text which is TOO LONG
Expand Down
17 changes: 0 additions & 17 deletions MangaRipper.Core/Extensions/ExtensionClearName.cs

This file was deleted.

38 changes: 38 additions & 0 deletions MangaRipper.Core/Extensions/ExtensionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;

namespace MangaRipper.Core.Extensions
{
public static class ExtensionHelper
{
/// <summary>
/// Remove characters that cannot using to name folder, file.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string RemoveFileNameInvalidChar(this string input)
{
return input.Replace("\\", "").Replace("/", "").Replace(":", "")
.Replace("*", "").Replace("?", "").Replace("\"", "")
.Replace("<", "").Replace(">", "").Replace("|", "");
}

/// <summary>
/// Directory.Move in C# cannot move across disk volume.
/// So we have this SUPER MOVE!!! :)
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="destinationPath"></param>
public static void SuperMove(string sourcePath, string destinationPath)
{
CopyFolderAndAllSubItems(new DirectoryInfo(sourcePath), new DirectoryInfo(destinationPath));
}

public static void CopyFolderAndAllSubItems(DirectoryInfo source, DirectoryInfo destination)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFolderAndAllSubItems(dir, destination.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(destination.FullName, file.Name));
}
}
}
2 changes: 1 addition & 1 deletion MangaRipper.Core/MangaRipper.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<Compile Include="Providers\FrameworkProvider.cs" />
<Compile Include="Interfaces\IMangaService.cs" />
<Compile Include="Services\DownloadService.cs" />
<Compile Include="Extensions\ExtensionClearName.cs" />
<Compile Include="Extensions\ExtensionHelper.cs" />
<Compile Include="Services\PluginService.cs" />
<Compile Include="DataTypes\OutputFormat.cs" />
<Compile Include="Helpers\PackageCbzHelper.cs" />
Expand Down
14 changes: 5 additions & 9 deletions MangaRipper.Core/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -26,7 +25,7 @@ public class DownloadService
private const string ClearanceCookieName = "cf_clearance";
private const string IdCookieName = "__cfduid";
private const int MaxRetries = 3;
private int retries;
private int _retries;

public CookieCollection Cookies { get; set; }

Expand Down Expand Up @@ -64,7 +63,7 @@ private HttpWebRequest CreateRequest(string url)
public async Task<string> DownloadStringAsync(string url)
{
Logger.Info("> DownloadStringAsync: {0}", url);
retries = 0;
_retries = 0;
return await WorkWithStreams(url);
}

Expand Down Expand Up @@ -103,7 +102,7 @@ public async Task<string> DownloadStringAsync(IEnumerable<string> urls, IProgres
public async Task DownloadFileAsync(string url, string fileName, CancellationToken cancellationToken)
{
Logger.Info("> DownloadFileAsync begin: {0} - {1}", url, fileName);
retries = 0;
_retries = 0;
var result = await WorkWithStreams(url, fileName, cancellationToken);
Logger.Info("> DownloadFileAsync result: {0} - {1}", url, result);
}
Expand Down Expand Up @@ -154,10 +153,10 @@ public async Task DownloadFileAsync(string url, string fileName, CancellationTok
// if we can't access server, and in Headers we found "cloudflare-nginx" - Solve the challenge
if (response.Headers["Server"] == CloudFlareServerName)
{
while ((MaxRetries < 0 || retries <= MaxRetries) && CookiesCollection == null)
while ((_retries <= MaxRetries) && CookiesCollection == null)
{
await SolverCloudFlare(response, html);
retries++;
_retries++;
html = await WorkWithStreams(url, fileName, cancellationToken);
}
}
Expand Down Expand Up @@ -242,9 +241,6 @@ private void SetTheCookie(string cookies)
CookiesCollection = null;
}
}

#endregion


}
}
22 changes: 10 additions & 12 deletions MangaRipper.Core/Services/PluginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,20 @@ private IEnumerable<IMangaService> CreateServices()
var result = new List<IMangaService>();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var t in a.GetTypes())
var pluginTypes = a.GetTypes().Where(t => t.IsSubclassOf(typeof(MangaService)));
try
{
if (t.GetInterface(nameof(IMangaService)) != null)
foreach (var pluginType in pluginTypes)
{
try
{
var pluginclass = Activator.CreateInstance(t) as IMangaService;
if (pluginclass != null)
result.Add(pluginclass);
}
catch (Exception ex)
{
Logger.Error(ex);
}
var pluginclass = Activator.CreateInstance(pluginType) as IMangaService;
if (pluginclass != null)
result.Add(pluginclass);
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
}
return result;
}
Expand Down

0 comments on commit 03ca348

Please sign in to comment.