-
-
Notifications
You must be signed in to change notification settings - Fork 18
Save the best possible img from custom layout to standard (BL-16086) #7806
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5228,6 +5228,21 @@ public string GetCoverImagePathAndElt(out SafeXmlElement coverImgElt) | |
| if (outsideFrontCover == null) | ||
| return null; | ||
|
|
||
| coverImgElt = GetCoverImageElt(outsideFrontCover, StoragePageFolder); | ||
| if (coverImgElt == null) | ||
| return null; | ||
|
|
||
| return GetImagePath(coverImgElt); | ||
|
Comment on lines
+5231
to
+5235
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The refactored This also changes observable behavior: when the designated cover image references a non-existent file and a placeholder element is also present on the cover page, the old code would return the placeholder path (via Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| internal static SafeXmlElement GetCoverImageElt( | ||
| SafeXmlElement outsideFrontCover, | ||
| string bookFolderPath = null | ||
| ) | ||
| { | ||
| if (outsideFrontCover == null) | ||
| return null; | ||
|
|
||
| // Prefer the designated cover image if it is present and not placeholder. | ||
| var designatedCoverImage = outsideFrontCover | ||
| .SafeSelectNodes( | ||
|
|
@@ -5236,22 +5251,30 @@ public string GetCoverImagePathAndElt(out SafeXmlElement coverImgElt) | |
| .Cast<SafeXmlElement>() | ||
| .FirstOrDefault(); | ||
|
|
||
| SafeXmlElement bestNonExistingNonPlaceholderElt = null; | ||
| SafeXmlElement bestPlaceholderElt = null; | ||
| string bestPlaceholderPath = null; | ||
| string bestPlaceholderSource = null; | ||
|
|
||
| if (designatedCoverImage != null) | ||
| { | ||
| var designatedPath = GetImagePath(designatedCoverImage); | ||
| if (designatedPath != null) | ||
| var designatedSource = GetImageSourceForCoverSelection(designatedCoverImage); | ||
| if (!string.IsNullOrWhiteSpace(designatedSource)) | ||
| { | ||
| if (!ImageUtils.IsPlaceholderImageFilename(designatedPath)) | ||
| if (!ImageUtils.IsPlaceholderImageFilename(designatedSource)) | ||
| { | ||
| coverImgElt = designatedCoverImage; | ||
| return designatedPath; | ||
| if ( | ||
| bookFolderPath == null | ||
| || ImageExistsInFolder(designatedSource, bookFolderPath) | ||
| ) | ||
| return designatedCoverImage; | ||
| // File doesn't exist on disk; remember it but keep looking for one that does. | ||
| bestNonExistingNonPlaceholderElt = designatedCoverImage; | ||
| } | ||
| else | ||
| { | ||
| bestPlaceholderElt = designatedCoverImage; | ||
| bestPlaceholderSource = designatedSource; | ||
| } | ||
|
|
||
| bestPlaceholderElt = designatedCoverImage; | ||
| bestPlaceholderPath = designatedPath; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -5271,25 +5294,43 @@ var candidate in outsideFrontCover | |
| if (candidate == designatedCoverImage) | ||
| continue; | ||
|
|
||
| var candidatePath = GetImagePath(candidate); | ||
| if (candidatePath == null) | ||
| var candidateSource = GetImageSourceForCoverSelection(candidate); | ||
| if (string.IsNullOrWhiteSpace(candidateSource)) | ||
| continue; | ||
|
|
||
| if (!ImageUtils.IsPlaceholderImageFilename(candidatePath)) | ||
| if (!ImageUtils.IsPlaceholderImageFilename(candidateSource)) | ||
| { | ||
| coverImgElt = candidate; | ||
| return candidatePath; | ||
| } | ||
| if ( | ||
| bookFolderPath == null | ||
| || ImageExistsInFolder(candidateSource, bookFolderPath) | ||
| ) | ||
| return candidate; | ||
|
|
||
| if (bestPlaceholderPath == null) | ||
| if (bestNonExistingNonPlaceholderElt == null) | ||
| bestNonExistingNonPlaceholderElt = candidate; | ||
| } | ||
| else if (bestPlaceholderSource == null) | ||
| { | ||
| bestPlaceholderElt = candidate; | ||
| bestPlaceholderPath = candidatePath; | ||
| bestPlaceholderSource = candidateSource; | ||
| } | ||
| } | ||
|
|
||
| coverImgElt = bestPlaceholderElt; | ||
| return bestPlaceholderPath; | ||
| return bestNonExistingNonPlaceholderElt ?? bestPlaceholderElt; | ||
| } | ||
|
|
||
| private static bool ImageExistsInFolder(string source, string bookFolderPath) | ||
| { | ||
| return RobustFile.Exists(Path.Combine(bookFolderPath, Uri.UnescapeDataString(source))); | ||
| } | ||
|
|
||
| private static string GetImageSourceForCoverSelection(SafeXmlElement imageElement) | ||
| { | ||
| var imageUrl = HtmlDom.GetImageElementUrl(imageElement); | ||
| if (!string.IsNullOrWhiteSpace(imageUrl.PathOnly.NotEncoded)) | ||
| return imageUrl.PathOnly.NotEncoded; | ||
|
|
||
| return imageUrl.UrlEncoded; | ||
| } | ||
|
|
||
| private string GetImagePath(SafeXmlElement imageElement) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.