Dave Jarvis' Repositories

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

Fix locating image files

AuthorDaveJarvis <email>
Date2020-07-02 20:56:59 GMT-0700
Commite36fac13f6d7e5317b2f1b34940cdd61c3490265
Parentd183ff6
Delta30 lines added, 19 lines removed, 11-line increase
src/main/java/com/scrivenvar/processors/markdown/ImageLinkExtension.java
import java.nio.file.Path;
-import static com.scrivenvar.Constants.DEFAULT_DIRECTORY;
import static java.lang.String.format;
private class ImageLinkResolver implements LinkResolver {
private final UserPreferences mUserPref = getUserPreferences();
- private final String mImagePrefix =
- mUserPref.getImagesDirectory().toString();
- private final String mImageSuffixes = mUserPref.getImagesOrder();
+ private final File mImagesUserPrefix = mUserPref.getImagesDirectory();
+ private final String mImageExtensions = mUserPref.getImagesOrder();
public ImageLinkResolver() {
try {
- final String imageFile = format( "%s/%s", getImagePrefix(), url );
- final String suffixes = getImageSuffixes();
- final Path editPath = getEditPath();
+ final Path imagePrefix = getImagePrefix().toPath();
+
+ // Path to the file being edited.
+ Path editPath = getEditPath();
+
+ // If there is no parent path to the file, it means the file has not
+ // been saved. Default to using the value from the user's preferences.
+ // The user's preferences will be defaulted to a the application's
+ // starting directory.
+ if( editPath == null ) {
+ editPath = imagePrefix;
+ }
+ else {
+ editPath = Path.of( editPath.toString(), imagePrefix.toString() );
+ }
+
+ final Path imagePathPrefix = Path.of( editPath.toString(), url );
+ final String suffixes = getImageExtensions();
boolean missing = true;
for( final String ext : Splitter.on( ' ' ).split( suffixes ) ) {
- final String imagePath = format(
- "%s/%s.%s", editPath, imageFile, ext );
+ final String imagePath = format( "%s.%s", imagePathPrefix, ext );
final File file = new File( imagePath );
if( missing ) {
- throw new FileNotFoundException( imageFile + ".*" );
+ throw new FileNotFoundException( imagePathPrefix + ".*" );
}
final String protocol = ProtocolResolver.getProtocol( url );
if( "file".equals( protocol ) ) {
url = "file://" + url;
}
+
+ getNotifier().clear();
return link.withStatus( LinkStatus.VALID ).withUrl( url );
} catch( final Exception e ) {
- getNotifier().notify( e );
+ getNotifier().notify( "File not found: " + e.getLocalizedMessage() );
}
return link;
}
- private String getImagePrefix() {
- return mImagePrefix;
+ private File getImagePrefix() {
+ return mImagesUserPrefix;
}
- private String getImageSuffixes() {
- return mImageSuffixes;
+ private String getImageExtensions() {
+ return mImageExtensions;
}
- @NotNull
private Path getEditPath() {
- final Path p = mPath.getParent();
-
- return p == null ? DEFAULT_DIRECTORY : p;
+ return mPath.getParent();
}
}