}
public void appendInternalImageLink(String hrefImageLink, String srcImageLink, ImageFormat imageFormat) {
try {
String imageName = imageFormat.getFilename();
ImageData imageData = fWikiDB.selectImage(imageName);
if (imageData != null) {
File file = new File(imageData.getFilename());
if (file.exists()) {
super.appendInternalImageLink(hrefImageLink, "file:///" + imageData.getFilename(), imageFormat);
return;
}
}
String imageNamespace = fNamespace.getImage().getPrimaryText();
setDefaultThumbWidth(imageFormat);
String[] listOfTitleStrings = { imageNamespace + ":" + imageName };
fUser.login();
List<Page> listOfPages;
if (imageFormat.getWidth() > 0) {
listOfPages = fUser.queryImageinfo(listOfTitleStrings, imageFormat.getWidth());
} else {
listOfPages = fUser.queryImageinfo(listOfTitleStrings);
}
if (listOfPages.size() > 0) {
Page page = listOfPages.get(0);
imageData = new ImageData(imageName);
// download the image to fImageDirectoryName directory
String imageUrl;
if (imageFormat.getWidth() > 0) {
imageUrl = page.getImageThumbUrl();
} else {
imageUrl = page.getImageUrl();
}
String urlImageName = Encoder.encodeTitleLocalUrl(page.getTitle());
if (imageUrl != null) {
int index = imageUrl.lastIndexOf('/');
if (index > 0) {
urlImageName = Encoder.encodeTitleLocalUrl(imageUrl.substring(index + 1));
}
}
if (fImageDirectoryName != null) {
String filename = fImageDirectoryName + urlImageName;
File file = new File(filename);
if (!file.exists()) {
// if the file doesn't exist try to download from Wikipedia
try (OutputStream os = new FileOutputStream(filename)) {
page.downloadImageUrl(os, imageUrl);
} catch (IOException e) {
logger.warn(null, e);
}
}
imageData.setUrl(imageUrl);
imageData.setFilename(filename);
fWikiDB.insertImage(imageData);
super.appendInternalImageLink(hrefImageLink, "file:///" + filename, imageFormat);
}
}
} catch (SQLException e) {