}
// we only allow a single level of directories right now, so make
// sure that the path doesn't try to go multiple levels
if(relPath != null && (relPath.lastIndexOf('/') > relPath.indexOf('/'))) {
throw new FilePathException("Invalid path ["+path+"], "+
"trying to use nested directories.");
}
// convert "/" to filesystem specific file separator
if(relPath != null) {
relPath = relPath.replace('/', File.separatorChar);
}
// now form the absolute path
String filePath = weblogDir.getAbsolutePath();
if(relPath != null) {
filePath += File.separator + relPath;
}
// make sure path exists and is readable
File file = new File(filePath);
if(!file.exists()) {
throw new FileNotFoundException("Invalid path ["+path+"], "+
"directory doesn't exist.");
} else if(!file.canRead()) {
throw new FilePathException("Invalid path ["+path+"], "+
"cannot read from path.");
}
try {
// make sure someone isn't trying to sneek outside the uploads dir
if(!file.getCanonicalPath().startsWith(weblogDir.getCanonicalPath())) {
throw new FilePathException("Invalid path ["+path+"], "+
"trying to get outside uploads dir.");
}
} catch (IOException ex) {
// rethrow as FilePathException
throw new FilePathException(ex);
}
return file;
}