msgs.add("Migration complete!");
return msgs;
}
private void upgradeUploadsDir(Weblog weblog, User user, File oldDir, MediaFileDirectory newDir) {
RollerMessages messages = new RollerMessages();
log.debug("Upgrading dir: " + oldDir.getAbsolutePath());
if (newDir == null) {
log.error("newDir cannot be null");
return;
}
// loop through files and directories
int dirCount = 0;
int fileCount = 0;
File[] files = oldDir.listFiles();
for (int i = 0; i < files.length; i++) {
// a directory: go recursive
if (files[i].isDirectory()) {
if (newDir.hasDirectory(files[i].getName())) {
// already have a mediafile directory for that
upgradeUploadsDir(weblog, user, files[i],
newDir.getChildDirectory(files[i].getName()));
} else {
// need to create a new mediafile directory
MediaFileDirectory subDir = null;
try {
subDir = newDir.createNewDirectory(files[i].getName());
roller.getMediaFileManager().createMediaFileDirectory(subDir);
newDir.getChildDirectories().add(subDir);
roller.flush();
dirCount++;
} catch (WebloggerException ex) {
log.error("ERROR creating directory: "
+ newDir.getPath() + "/" + files[i].getName());
}
upgradeUploadsDir(weblog, user, files[i], subDir);
}
} else { // a file: create a database record for it
// check to make sure that file does not already exist
if (newDir.hasMediaFile(files[i].getName())) {
log.debug(" Skipping file that already exists: "
+ files[i].getName());
} else {
String originalPath =
("/".equals(newDir.getPath()) ? "" : newDir.getPath()) + "/" + files[i].getName();
log.debug(" Upgrade file with original path: " + originalPath);
MediaFile mf = new MediaFile();
try {
mf.setName(files[i].getName());
mf.setDescription(files[i].getName());
mf.setOriginalPath(originalPath);
mf.setDateUploaded(new Timestamp(files[i].lastModified()));
mf.setLastUpdated(new Timestamp(files[i].lastModified()));
mf.setDirectory(newDir);
mf.setWeblog(weblog);
mf.setCreatorUserName(user.getUserName());
mf.setSharedForGallery(Boolean.FALSE);
mf.setLength(files[i].length());
mf.setInputStream(new FileInputStream(files[i]));
mf.setContentType(Utilities.getContentTypeFromFileName(files[i].getName()));
newDir.getMediaFiles().add(mf);
this.roller.getMediaFileManager().createMediaFile(weblog, mf, messages);
log.info(messages.toString());
fileCount++;
} catch (WebloggerException ex) {
log.error("ERROR writing file to new storage system: "