Skip to content

Commit

Permalink
Updated chughtai downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
umerfaruk committed Sep 28, 2024
1 parent 6924df2 commit 2854641
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/Inshapardaz.Api/Views/ChughtaiDownloadView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class ChughtaiDownloadView
{
[Required]
public string BookUrl { get; set; }
[Required]
public string SessionId { get; set; }
public bool ConvertToPdf { get; set; }
}
11 changes: 0 additions & 11 deletions src/Inshapardaz.Api/appsettings.Development.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/Inshapardaz.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
},
"FrontEndUrl": "",
"Allowed_Origins": "",
"DefaultLibraryId" : 7
"DefaultLibraryId" : 1
}
}
2 changes: 2 additions & 0 deletions src/Inshapardaz.Domain/Adapters/Configuration/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public record Settings
public string FrontEndUrl { get; init; }

public int DefaultLibraryId { get; init; }

public bool SaveDownloadsToStorage { get; init; } = false;
}
2 changes: 1 addition & 1 deletion src/Inshapardaz.Domain/Inshapardaz.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Chughtai.Downloader" Version="1.0.0.15" />
<PackageReference Include="Chughtai.Downloader" Version="1.0.0.16" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
<PackageReference Include="HtmlToOpenXml.dll" Version="2.4.2" />
<PackageReference Include="MailKit" Version="4.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,29 @@ private async Task DownloadBook(BookModel book, DownloadChughtaiBookRequest comm
{
var exporter = new Chughtai.Downloader.BookDownloader(_logger);
var path = $"../data/downloads/{Guid.NewGuid():D}";
var filePath = await exporter.DownloadBook(command.Url, 10, command.SessionId, command.CreatePdf ? OutputType.Pdf : OutputType.Images, path, cancellationToken);
string filePath = null;
if (string.IsNullOrWhiteSpace(command.SessionId))
{
filePath = await exporter.Download(command.Url, 10, command.CreatePdf ? OutputType.Pdf : OutputType.Images, path, cancellationToken);
}
else
{
filePath = await exporter.DownloadBook(command.Url, 10, command.SessionId, command.CreatePdf ? OutputType.Pdf : OutputType.Images, path, cancellationToken);
}

var bookInfo = await exporter.GetBookInformation(command.Url, cancellationToken);

try
{
if (book == null)
{
book = await CreateNewBook(bookInfo, command.Url, cancellationToken);
}

var result = new DownloadChughtaiBookRequest.Result
{

FileName = Path.GetFileName(filePath),
MimeType = command.CreatePdf ? MimeTypes.Pdf : MimeTypes.Zip
};

if (command.CreatePdf)
{
result.File = await System.IO.File.ReadAllBytesAsync(filePath, cancellationToken);

await SaveBookPdfContents(book, filePath, result.File, cancellationToken);
}
else
{
Expand All @@ -202,11 +203,26 @@ private async Task DownloadBook(BookModel book, DownloadChughtaiBookRequest comm
{
zipFilePath.TryDeleteFile();
}

await SaveBookPages(book, filePath, cancellationToken);
}

command.DownloadResult = result;

if (_settings.SaveDownloadsToStorage)
{
if (book == null)
{
book = await CreateNewBook(bookInfo, command.Url, cancellationToken);
}

if (command.CreatePdf)
{
await SaveBookPdfContents(book, filePath, result.File, cancellationToken);
}
else
{
await SaveBookPages(book, filePath, cancellationToken);
}
}
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ private async Task DownloadBook(BookModel book, DownloadRekhtaBookRequest comman

try
{
if (book == null)
{
book = await CreateNewBook(bookInfo, command.Url, cancellationToken);
}

var result = new DownloadRekhtaBookRequest.Result()
{

Expand All @@ -185,8 +180,6 @@ private async Task DownloadBook(BookModel book, DownloadRekhtaBookRequest comman
if (command.CreatePdf)
{
result.File = await System.IO.File.ReadAllBytesAsync(filePath, cancellationToken);

await SaveBookPdfContents(book, filePath, result.File, cancellationToken);
}
else
{
Expand All @@ -202,11 +195,27 @@ private async Task DownloadBook(BookModel book, DownloadRekhtaBookRequest comman
{
zipFilePath.TryDeleteFile();
}

await SaveBookPages(book, filePath, cancellationToken);
}

command.DownloadResult = result;


if (_settings.SaveDownloadsToStorage)
{
if (book == null)
{
book = await CreateNewBook(bookInfo, command.Url, cancellationToken);
}

if (command.CreatePdf)
{
await SaveBookPdfContents(book, filePath, result.File, cancellationToken);
}
else
{
await SaveBookPages(book, filePath, cancellationToken);
}
}
}
finally
{
Expand Down

0 comments on commit 2854641

Please sign in to comment.