Skip to content

Commit

Permalink
adding override file to the epub file
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattumang7 committed Aug 30, 2024
1 parent cbd70f4 commit fe5927c
Show file tree
Hide file tree
Showing 3 changed files with 2,447 additions and 3 deletions.
59 changes: 57 additions & 2 deletions MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Xml.Linq;


namespace SafariBooksDownload
Expand Down Expand Up @@ -110,14 +111,24 @@ private async void downloadBook(object sender, EventArgs e)
if (file.full_path.EndsWith(".opf"))
{
opfPath = file.full_path;
break;
}
}



var s = await DownloadFileAsync(selectedBook, chapters, localEpubFolder );
var s = await DownloadFileAsync(selectedBook, chapters, localEpubFolder );

var containeXMLPath = Path.Join(localEpubFolder, "/META-INF/container.xml");

// put additional file
//var sourceCssFilePath = Path.Combine(FileSystem.AppDataDirectory, "Resources", "Raw", );
var targetOverrideCSSFilePath = Path.Join(localEpubFolder, "override_v1.css");
var sourceFileContent = await ReadTextFileAsync("override_v1.css");
File.WriteAllText(targetOverrideCSSFilePath, sourceFileContent);

await AddOverrideCSSToManifest(Path.Join (localEpubFolder, opfPath));

// create meta-inf folder.
string directoryPath = Path.GetDirectoryName(containeXMLPath);
if (!Directory.Exists(directoryPath))
Expand Down Expand Up @@ -170,6 +181,49 @@ private async void downloadBook(object sender, EventArgs e)
}
}

public async Task<string> ReadTextFileAsync(string fileName)
{
try
{
using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(fileName);
using StreamReader reader = new StreamReader(fileStream);
return await reader.ReadToEndAsync();
}
catch (Exception ex)
{
// Handle exceptions as needed
return $"Error reading file: {ex.Message}";
}
}

public async Task AddOverrideCSSToManifest(string filePath)
{

if (!File.Exists(filePath))
{
throw new FileNotFoundException(filePath);
}

// Load the XML document
XDocument xmlDoc = XDocument.Load(filePath);

// Create a new item element
XElement newItem = new XElement("item",
new XAttribute("id", "newItemId"),
new XAttribute("href", "OEBPS/Text/newitem.html"),
new XAttribute("media-type", "application/xhtml+xml")
);

// Add the new item to the manifest
xmlDoc.Root.Element("{http://www.idpf.org/2007/opf}manifest").Add(newItem);

// Save the updated XML document
using (var stream = File.Create(filePath))
{
xmlDoc.Save(stream);
}
}

private async Task<List<ChappterInfo>> fetchChapterInfo(Book selectedBook)
{
List<ChappterInfo> chappterInfos = new List<ChappterInfo>();
Expand Down Expand Up @@ -280,7 +334,8 @@ private async Task<string> DownloadFileAsync(Book selectedBook, List<ChappterInf
" http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd\"" +
" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" +
"<head>\n" +
"<meta charset=\"utf-8\">" +
"<meta charset=\"utf-8\"> \n" +
"<link href=\"override_v1.css\" rel=\"stylesheet\" type=\"text/css\" /> \n" +
$"{extraCSSInfo}\n" +
"<style type=\"text/css\">" +
"body{{margin:1em;background-color:transparent!important;}}" +
Expand Down
2 changes: 1 addition & 1 deletion PathAdjuster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PathAdjuster
public PathAdjuster(string productId)
{
_baseUrl = "/api/v2/epubs/urn:orm:book:" + productId + "/files/";
_localBasePath = "";
_localBasePath = "/";
}

public string AdjustPathsInHtml(string htmlContent)
Expand Down
Loading

0 comments on commit fe5927c

Please sign in to comment.