Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle accented filenames on internal files #23

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/src/readers/chapter_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import '../ref_entities/epub_chapter_ref.dart';
import '../ref_entities/epub_text_content_file_ref.dart';
import '../schema/navigation/epub_navigation_point.dart';

import 'content_reader.dart';

class ChapterReader {
static List<EpubChapterRef> getChapters(EpubBookRef bookRef) {
if (bookRef.Schema!.Navigation == null) {
Expand All @@ -16,10 +18,10 @@ class ChapterReader {
EpubBookRef bookRef, List<EpubNavigationPoint> navigationPoints) {
var result = <EpubChapterRef>[];
// navigationPoints.forEach((EpubNavigationPoint navigationPoint) {
for (var navigationPoint in navigationPoints){
for (var navigationPoint in navigationPoints) {
String? contentFileName;
String? anchor;
if (navigationPoint.Content?.Source ==null) continue;
if (navigationPoint.Content?.Source == null) continue;
var contentSourceAnchorCharIndex =
navigationPoint.Content!.Source!.indexOf('#');
if (contentSourceAnchorCharIndex == -1) {
Expand All @@ -31,7 +33,7 @@ class ChapterReader {
anchor = navigationPoint.Content!.Source!
.substring(contentSourceAnchorCharIndex + 1);
}
contentFileName = Uri.decodeFull(contentFileName!);
contentFileName = accentedFileChecker(contentFileName!);
EpubTextContentFileRef? htmlContentFileRef;
if (!bookRef.Content!.Html!.containsKey(contentFileName)) {
throw Exception(
Expand All @@ -47,7 +49,8 @@ class ChapterReader {
getChaptersImpl(bookRef, navigationPoint.ChildNavigationPoints!);

result.add(chapterRef);
};
}
;
return result;
}
}
27 changes: 25 additions & 2 deletions lib/src/readers/content_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import '../ref_entities/epub_content_ref.dart';
import '../ref_entities/epub_text_content_file_ref.dart';
import '../schema/opf/epub_manifest_item.dart';

//Accented characters throw a Invalid argument(s): Illegal percent encoding in URI on Uri.decodeFull.
//By checking if it throws an error and returning the filename again if so, you can use accented chars in
//epub internal filenames.
String accentedFileChecker(String incomingFileName) {
Copy link

@jasmeet0817 jasmeet0817 Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be simpler:

String decodeFileName(String incomingFileName) {
try {
return Uri.decodeFull(incomingFileName);
} catch (e) {
return incomingFileName;
}
}

bool errorThrown = false;
String fileName = '';
String decodedFileName = '';

try {
decodedFileName = Uri.decodeFull(incomingFileName);
} catch (e) {
errorThrown = true;
} finally {
if (errorThrown) {
fileName = incomingFileName;
} else {
fileName = decodedFileName;
}
}
return fileName;
}
//////

class ContentReader {
static EpubContentRef parseContentMap(EpubBookRef bookRef) {
var result = EpubContentRef();
Expand All @@ -30,7 +53,7 @@ class ContentReader {
case EpubContentType.DTBOOK_NCX:
var epubTextContentFile = EpubTextContentFileRef(bookRef);
{
epubTextContentFile.FileName = Uri.decodeFull(fileName!);
epubTextContentFile.FileName = accentedFileChecker(fileName!);
epubTextContentFile.ContentMimeType = contentMimeType;
epubTextContentFile.ContentType = contentType;
}
Expand Down Expand Up @@ -62,7 +85,7 @@ class ContentReader {
default:
var epubByteContentFile = EpubByteContentFileRef(bookRef);
{
epubByteContentFile.FileName = Uri.decodeFull(fileName!);
epubByteContentFile.FileName = accentedFileChecker(fileName!);
epubByteContentFile.ContentMimeType = contentMimeType;
epubByteContentFile.ContentType = contentType;
}
Expand Down