* @param request the HttpServletRequest instance
* @param response the HttpServletResponse instance
* @return the name of the next view
*/
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
String type = getType();
String path = "";
String[] filenames = new String[10];
FileManager fileManager = new FileManager(blog, type);
try {
boolean isMultipart = FileUpload.isMultipartContent(request);
if (isMultipart) {
DiskFileUpload upload = new DiskFileUpload();
long sizeInBytes = PebbleContext.getInstance().getConfiguration().getFileUploadSize() * 1024; // convert to bytes
upload.setSizeMax(sizeInBytes);
upload.setSizeThreshold((int)sizeInBytes/4);
upload.setRepositoryPath(System.getProperty("java.io.tmpdir"));
List items;
try {
items = upload.parseRequest(request);
} catch (FileUploadBase.SizeLimitExceededException e) {
return new FileTooLargeView();
}
// find the form fields first
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem)it.next();
if (item.isFormField() && item.getFieldName().startsWith("filename")) {
int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1));
filenames[index] = item.getString();
log.debug("index is " + index + ", filename is " + filenames[index]);
} else if (item.isFormField() && item.getFieldName().equals("path")) {
path = item.getString();
}
}
// now the actual files
it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem)it.next();
if (!item.isFormField() && item.getSize() > 0 && item.getFieldName().startsWith("file")) {
int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1));
// if the filename hasn't been specified, use that from the file
// being uploaded
if (filenames[index] == null || filenames[index].length() == 0) {
filenames[index] = item.getName();
}
File destinationDirectory = fileManager.getFile(path);
File file = new File(destinationDirectory, filenames[index]);
if (!fileManager.isUnderneathRootDirectory(file)) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return null;
}
long itemSize = item.getSize()/1024;
if (FileManager.hasEnoughSpace(blog, itemSize)) {
log.debug("Writing file " + filenames[index] + ", size is " + item.getSize());
writeFile(fileManager, path, filenames[index], item);
// if it's a theme file, also create a copy in blog.dir/theme
if (type.equals(FileMetaData.THEME_FILE)) {
writeFile(new FileManager(blog, FileMetaData.BLOG_DATA), "/theme" + path, filenames[index], item);
}
} else {
return new NotEnoughSpaceView();
}
}
}
}
blog.info("Files uploaded.");
} catch (Exception e) {
throw new ServletException(e);
}
FileMetaData directory = fileManager.getFileMetaData(path);
return new RedirectView(blog.getUrl() + directory.getUrl());
}