NIHVIVO-874 Merge 5357 from the branch.

This commit is contained in:
jeb228 2010-07-21 17:15:22 +00:00
parent d565eca6b6
commit 60f6f401f3
9 changed files with 149 additions and 47 deletions

View file

@ -12,11 +12,12 @@ import com.hp.hpl.jena.rdf.model.Resource;
* Adjust any individual that has a thumbnail with no main image.
*/
public class AllThumbsAdjuster extends FsuScanner {
protected final File imageDirectory;
private ImageDirectoryWithBackup imageDirectoryWithBackup;
public AllThumbsAdjuster(FSUController controller) {
super(controller);
this.imageDirectory = controller.getImageDirectory();
this.imageDirectoryWithBackup = controller
.getImageDirectoryWithBackup();
}
/**
@ -56,8 +57,9 @@ public class AllThumbsAdjuster extends FsuScanner {
+ "' to match the thumbnail at '" + thumbFilename + "'");
try {
File thumbFile = new File(imageDirectory, thumbFilename);
File mainFile = new File(imageDirectory, mainFilename);
File thumbFile = imageDirectoryWithBackup
.getExistingFile(thumbFilename);
File mainFile = imageDirectoryWithBackup.getNewfile(mainFilename);
mainFile = checkNameConflicts(mainFile);
FileUtil.copyFile(thumbFile, mainFile);

View file

@ -16,11 +16,12 @@ import com.hp.hpl.jena.rdf.model.Statement;
* don't actually exist.
*/
public class DeadEndPropertyRemover extends FsuScanner {
protected final File imageDirectory;
private ImageDirectoryWithBackup imageDirectoryWithBackup;
public DeadEndPropertyRemover(FSUController controller) {
super(controller);
this.imageDirectory = controller.getImageDirectory();
this.imageDirectoryWithBackup = controller
.getImageDirectoryWithBackup();
}
/**
@ -58,12 +59,14 @@ public class DeadEndPropertyRemover extends FsuScanner {
for (Statement stmt : getStatements(resource, prop)) {
RDFNode node = stmt.getObject();
if (node.isLiteral()) {
String filename = ((Literal)node).getString();
File file = new File(imageDirectory, filename);
String filename = ((Literal) node).getString();
File file = imageDirectoryWithBackup.getExistingFile(filename);
if (!file.exists()) {
updateLog.warn(resource, "removing link to " + label + " '"
+ filename + "': file does not exist at '"
+ file.getAbsolutePath() + "'.");
updateLog.warn(
resource,
"removing link to " + label + " '" + filename
+ "': file does not exist at '"
+ file.getAbsolutePath() + "'.");
model.remove(stmt);
}
}

View file

@ -20,8 +20,8 @@ public interface FSUController {
/** The update log. */
FSULog getUpdateLog();
/** The directory where the old-style images were stored. */
File getImageDirectory();
/** The place to find or to create image files. */
ImageDirectoryWithBackup getImageDirectoryWithBackup();
/** The file storage system. */
FileStorage getFileStorage();

View file

@ -106,18 +106,21 @@ public class FileStorageUpdater implements FSUController {
private final FileStorage fileStorage;
private final FileModelHelper fileModelHelper;
private final File imageDirectory;
private final ImageDirectoryWithBackup imageDirectoryWithBackup;
private final File upgradeDirectory;
private FSULog updateLog;
public FileStorageUpdater(WebappDaoFactory wadf, Model model,
FileStorage fileStorage, File uploadDirectory) {
FileStorage fileStorage, File uploadDirectory,
File webappImageDirectory) {
this.model = model;
this.fileStorage = fileStorage;
this.fileModelHelper = new FileModelHelper(wadf);
this.imageDirectory = new File(uploadDirectory, "images");
this.upgradeDirectory = new File(uploadDirectory, "upgrade");
this.imageDirectoryWithBackup = new ImageDirectoryWithBackup(new File(
uploadDirectory, "images"), webappImageDirectory);
}
/**
@ -256,9 +259,8 @@ public class FileStorageUpdater implements FSUController {
}
@Override
public File getImageDirectory() {
return this.imageDirectory;
public ImageDirectoryWithBackup getImageDirectoryWithBackup() {
return this.imageDirectoryWithBackup;
}
@Override

View file

@ -12,13 +12,15 @@ import java.util.Collection;
* referenced.
*/
public class ImageDirectoryCleaner extends FsuScanner {
protected final File imageDirectory;
private final ImageDirectoryWithBackup imageDirectoryWithBackup;
protected final File translatedDirectory;
protected final File unreferencedDirectory;
public ImageDirectoryCleaner(FSUController controller) {
super(controller);
this.imageDirectory = controller.getImageDirectory();
this.imageDirectoryWithBackup = controller
.getImageDirectoryWithBackup();
this.translatedDirectory = controller.getTranslatedDirectory();
this.unreferencedDirectory = controller.getUnreferencedDirectory();
}
@ -33,7 +35,8 @@ public class ImageDirectoryCleaner extends FsuScanner {
updateLog.section("Cleaning the old image directory of "
+ "files that were not referenced.");
removeRemainingFiles(imageDirectory);
removeRemainingFiles(imageDirectoryWithBackup
.getPrimaryImageDirectory());
}
/**
@ -41,15 +44,22 @@ public class ImageDirectoryCleaner extends FsuScanner {
*/
private void removeTranslatedFiles(Collection<String> translatedFiles) {
for (String path : translatedFiles) {
updateLog.log("moving image file '" + path
+ "' to the 'translated' directory.");
File oldFile = new File(imageDirectory, path);
File deletedFile = new File(translatedDirectory, path);
try {
FileUtil.moveFile(oldFile, deletedFile);
} catch (IOException e) {
updateLog.error("Failed to move translated file '"
+ oldFile.getAbsolutePath() + "'");
File oldFile = new File(
imageDirectoryWithBackup.getPrimaryImageDirectory(), path);
if (oldFile.exists()) {
updateLog.log("moving image file '" + path
+ "' to the 'translated' directory.");
File deletedFile = new File(translatedDirectory, path);
try {
FileUtil.moveFile(oldFile, deletedFile);
} catch (IOException e) {
updateLog.error("Failed to move translated file '"
+ oldFile.getAbsolutePath() + "'");
}
} else {
updateLog.log("Not moving image file '" + path
+ "' to the 'translated' directory -- "
+ "found it in the backup directory.");
}
}
}
@ -73,8 +83,9 @@ public class ImageDirectoryCleaner extends FsuScanner {
}
}
} catch (IOException e) {
updateLog.error("Failed to clean images directory '"
+ directory.getAbsolutePath() + "'", e);
updateLog.error(
"Failed to clean images directory '"
+ directory.getAbsolutePath() + "'", e);
}
}
@ -89,8 +100,9 @@ public class ImageDirectoryCleaner extends FsuScanner {
File newFile = new File(targetDirectory, file.getName());
FileUtil.moveFile(file, newFile);
} catch (IOException e) {
updateLog.error("Can't move unreferenced file '"
+ file.getAbsolutePath() + "'", e);
updateLog.error(
"Can't move unreferenced file '" + file.getAbsolutePath()
+ "'", e);
}
}
@ -99,7 +111,8 @@ public class ImageDirectoryCleaner extends FsuScanner {
* corresponding directory in the "unreferenced" area.
*/
private File makeCorrespondingDirectory(File directory) throws IOException {
String imagesPath = imageDirectory.getAbsolutePath();
String imagesPath = imageDirectoryWithBackup.getPrimaryImageDirectory()
.getAbsolutePath();
String thisPath = directory.getAbsolutePath();
if (!thisPath.startsWith(imagesPath)) {

View file

@ -0,0 +1,77 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.filestorage.updater;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A way to look for files in TOMCAT_WEBAPP/vivo/images, if they are not found
* in upload.directory/images.
*/
public class ImageDirectoryWithBackup {
private static final Log log = LogFactory
.getLog(ImageDirectoryWithBackup.class);
/** The primary image directory, where we do most of the manipulation. */
private final File uploadImageDirectory;
/**
* If we are looking for a file and don't find it in the primary directory,
* look for it here.
*/
private final File webappImageDirectory;
/**
* Be careful! webappImageDirectory may be null.
*/
public ImageDirectoryWithBackup(File uploadImageDirectory,
File webappImageDirectory) {
this.uploadImageDirectory = uploadImageDirectory;
this.webappImageDirectory = webappImageDirectory;
}
/**
* When looking to read a file, start by looking in the
* {@link #uploadImageDirectory}.
*
* If the file isn't found there, look in the {@link #webappImageDirectory}
* as a fallback.
*
* If not there either, return the pointer to the nonexistent file in the
* {@link #uploadImageDirectory}.
*/
File getExistingFile(String relativePath) {
File file1 = new File(uploadImageDirectory, relativePath);
if (file1.exists()) {
log.trace("Found file: " + file1.getAbsolutePath());
return file1;
}
if (webappImageDirectory != null) {
File file2 = new File(webappImageDirectory, relativePath);
if (file2.exists()) {
log.trace("Found file: " + file2.getAbsolutePath());
return file2;
}
}
log.trace("Didn't find file: " + file1.getAbsolutePath());
return file1;
}
/**
* New files will always be created in the primary directory.
*/
File getNewfile(String relativePath) {
return new File(uploadImageDirectory, relativePath);
}
/**
* You can get a direct reference to the primary image directory, but it
* should only be used for directory-base operations, like final cleanup.
*/
public File getPrimaryImageDirectory() {
return uploadImageDirectory;
}
}

View file

@ -26,13 +26,14 @@ import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorage;
* yet, in case someone else is referring to them also.
*/
public class ImageSchemaTranslater extends FsuScanner {
protected final File imageDirectory;
private final ImageDirectoryWithBackup imageDirectoryWithBackup;
protected final FileModelHelper fileModelHelper;
protected final FileStorage fileStorage;
public ImageSchemaTranslater(FSUController controller) {
super(controller);
this.imageDirectory = controller.getImageDirectory();
this.imageDirectoryWithBackup = controller
.getImageDirectoryWithBackup();
this.fileStorage = controller.getFileStorage();
this.fileModelHelper = controller.getFileModelHelper();
}
@ -126,7 +127,7 @@ public class ImageSchemaTranslater extends FsuScanner {
*/
private Individual translateFile(Resource resource, String path,
String label) {
File oldFile = new File(imageDirectory, path);
File oldFile = imageDirectoryWithBackup.getExistingFile(path);
String filename = getSimpleFilename(path);
String mimeType = guessMimeType(resource, filename);

View file

@ -17,11 +17,12 @@ import com.hp.hpl.jena.rdf.model.Resource;
* Adjust any individual that has a main image but no thumbnail.
*/
public class NoThumbsAdjuster extends FsuScanner {
protected final File imageDirectory;
private ImageDirectoryWithBackup imageDirectoryWithBackup;
public NoThumbsAdjuster(FSUController controller) {
super(controller);
this.imageDirectory = controller.getImageDirectory();
this.imageDirectoryWithBackup = controller
.getImageDirectoryWithBackup();
}
/**
@ -59,8 +60,8 @@ public class NoThumbsAdjuster extends FsuScanner {
updateLog.log(resource, "creating a thumbnail at '" + thumbFilename
+ "' from the main image at '" + mainFilename + "'");
File mainFile = new File(imageDirectory, mainFilename);
File thumbFile = new File(imageDirectory, thumbFilename);
File mainFile = imageDirectoryWithBackup.getExistingFile(mainFilename);
File thumbFile = imageDirectoryWithBackup.getNewfile(thumbFilename);
thumbFile = checkNameConflicts(thumbFile);
try {
generateThumbnailImage(mainFile, thumbFile,

View file

@ -17,7 +17,6 @@ import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDao;
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorage;
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
import edu.cornell.mannlib.vitro.webapp.filestorage.updater.FileStorageUpdater;
@ -97,8 +96,12 @@ public class UpdateUploadedFiles implements ServletContextListener {
+ "' does not exist.");
}
String webappImagePath = ctx.getRealPath("images");
File webappImageDirectory = (webappImagePath == null) ? null
: new File(webappImagePath);
FileStorageUpdater fsu = new FileStorageUpdater(wadf, jenaOntModel,
fileStorage, uploadDirectory);
fileStorage, uploadDirectory, webappImageDirectory);
fsu.update();
} catch (Exception e) {
log.error("Unknown problem", e);