Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Add logging around image resolution

AuthorDaveJarvis <email>
Date2022-08-02 22:55:05 GMT-0700
Commitb74d82e438c94467251c815d6deba072a53a3c04
Parent6bb0e1d
Delta63 lines added, 34 lines removed, 29-line increase
src/main/resources/com/keenwrite/messages.properties
Main.status.image.request.error.cert=Could not accept certificate for ''{0}''
+Main.status.image.xhtml.image.download=Downloading ''{0}''
+Main.status.image.xhtml.image.resolve=Qualify path for ''{0}''
+Main.status.image.xhtml.image.found=Found image ''{0}''
+Main.status.image.xhtml.image.missing=Missing image ''{0}''
+
Main.status.font.search.missing=No font name starting with ''{0}'' was found
src/main/java/com/keenwrite/processors/ProcessorContext.java
public void setImageDir( final Supplier<File> imageDir ) {
assert imageDir != null;
- mImageDir = () -> imageDir.get().toPath();
+
+ mImageDir = () -> {
+ final var dir = imageDir.get();
+
+ return (dir == null ? USER_DIRECTORY : dir).toPath();
+ };
}
src/main/java/com/keenwrite/processors/XhtmlProcessor.java
*/
private Path exportImage( final String src ) throws Exception {
- Path imageFile = null;
+ return getProtocol( src ).isRemote()
+ ? downloadImage( src )
+ : resolveImage( src );
+ }
- final var protocol = getProtocol( src );
+ private Path downloadImage( final String src ) throws Exception {
+ final Path imageFile;
- // Download remote resources into temporary files.
- if( protocol.isRemote() ) {
- try( final var response = httpGet( src ) ) {
- final var mediaType = response.getMediaType();
+ clue( "Main.status.image.xhtml.image.download", src );
- imageFile = mediaType.createTempFile( APP_TITLE_LOWERCASE, true );
+ try( final var response = httpGet( src ) ) {
+ final var mediaType = response.getMediaType();
- try( final var image = response.getInputStream() ) {
- copy( image, imageFile, REPLACE_EXISTING );
- }
+ // Preserve image files if autoclean is turned off.
+ imageFile = mediaType.createTempFile( APP_TITLE_LOWERCASE, autoclean() );
- // Strip comments, superfluous whitespace, DOCTYPE, and XML
- // declarations.
- if( mediaType.isSvg() ) {
- DocumentParser.sanitize( imageFile );
- }
+ try( final var image = response.getInputStream() ) {
+ copy( image, imageFile, REPLACE_EXISTING );
+ }
+
+ // Strip comments, superfluous whitespace, DOCTYPE, and XML
+ // declarations.
+ if( mediaType.isSvg() ) {
+ DocumentParser.sanitize( imageFile );
}
}
- else {
- final var extensions = getImageOrder();
- var imagePath = getImagePath();
- var found = false;
- for( final var extension : extensions ) {
- final var filename = format(
- "%s%s%s", src, extension.isBlank() ? "" : ".", extension );
- imageFile = Path.of( imagePath, filename );
+ return imageFile;
+ }
- if( imageFile.toFile().exists() ) {
- found = true;
- break;
- }
+ private Path resolveImage( final String src ) throws Exception {
+ var imagePath = getImagePath();
+ var found = false;
+
+ Path imageFile = null;
+
+ clue( "Main.status.image.xhtml.image.resolve", src );
+
+ for( final var extension : getImageOrder() ) {
+ final var filename = format(
+ "%s%s%s", src, extension.isBlank() ? "" : ".", extension );
+ imageFile = Path.of( imagePath, filename );
+
+ if( imageFile.toFile().exists() ) {
+ found = true;
+ break;
}
+ }
- if( !found ) {
- imagePath = getDocumentDir().toString();
- imageFile = Path.of( imagePath, src );
+ if( !found ) {
+ imagePath = getDocumentDir().toString();
+ imageFile = Path.of( imagePath, src );
- if( !imageFile.toFile().exists() ) {
- throw new FileNotFoundException( imageFile.toString() );
- }
+ if( !imageFile.toFile().exists() ) {
+ final var filename = imageFile.toString();
+ clue( "Main.status.image.xhtml.image.missing", filename );
+
+ throw new FileNotFoundException( filename );
}
}
+
+ clue( "Main.status.image.xhtml.image.found", imageFile.toString() );
return imageFile;
private Locale getLocale() {
return mContext.getLocale();
+ }
+
+ private boolean autoclean() {
+ return mContext.getAutoClean();
}