| 
 1 | 
  
 | package photospace.web.spring; | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 | import java.io.*; | 
| 
 4 | 
  
 | import java.util.*; | 
| 
 5 | 
  
 | import javax.servlet.http.*; | 
| 
 6 | 
  
 | import org.apache.commons.lang.*; | 
| 
 7 | 
  
 | import org.apache.commons.logging.*; | 
| 
 8 | 
  
 | import org.springframework.validation.*; | 
| 
 9 | 
  
 | import org.springframework.web.multipart.*; | 
| 
 10 | 
  
 | import org.springframework.web.servlet.*; | 
| 
 11 | 
  
 | import org.springframework.web.servlet.mvc.*; | 
| 
 12 | 
  
 | import org.springframework.web.servlet.view.*; | 
| 
 13 | 
  
 | import photospace.vfs.*; | 
| 
 14 | 
  
 |  | 
| 
 15 | 
  
 | public class UploadForm | 
| 
 16 | 
  
 |   extends SimpleFormController | 
| 
 17 | 
  
 | { | 
| 
 18 | 
  
 |  | 
| 
 19 | 
  
 |   private static final Log log = LogFactory.getLog(UploadForm.class); | 
| 
 20 | 
  
 |  | 
| 
 21 | 
  
 |     private FileSystem filesystem; | 
| 
 22 | 
  
 |  | 
| 
 23 | 
 0
 |   public UploadForm()
 | 
| 
 24 | 
  
 |   { | 
| 
 25 | 
 0
 |         setSessionForm(false);
 | 
| 
 26 | 
 0
 |         setBindOnNewForm(true);
 | 
| 
 27 | 
 0
 |     setFormView("/upload");
 | 
| 
 28 | 
  
 |   } | 
| 
 29 | 
  
 |  | 
| 
 30 | 
 0
 |     protected Object formBackingObject(HttpServletRequest request) throws Exception
 | 
| 
 31 | 
  
 |   { | 
| 
 32 | 
 0
 |         return new UploadCommand();
 | 
| 
 33 | 
  
 |     } | 
| 
 34 | 
  
 |  | 
| 
 35 | 
 0
 |   protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception
 | 
| 
 36 | 
  
 |   { | 
| 
 37 | 
 0
 |     UploadCommand upload = (UploadCommand) command;
 | 
| 
 38 | 
  
 |  | 
| 
 39 | 
 0
 |     File base = filesystem.getDirectory(upload.getPath());
 | 
| 
 40 | 
 0
 |     if (!StringUtils.isEmpty(upload.getNewFolderName()))
 | 
| 
 41 | 
  
 |     { | 
| 
 42 | 
 0
 |       base = new File(base, upload.getNewFolderName());
 | 
| 
 43 | 
 0
 |       base.mkdir();
 | 
| 
 44 | 
  
 |     } | 
| 
 45 | 
  
 |  | 
| 
 46 | 
 0
 |     List paths = new ArrayList();
 | 
| 
 47 | 
 0
 |     for (int i = 0; i < upload.getFiles().length; i++)
 | 
| 
 48 | 
  
 |     { | 
| 
 49 | 
 0
 |       MultipartFile file = upload.getFiles()[i];
 | 
| 
 50 | 
 0
 |       if (file == null) continue;
 | 
| 
 51 | 
 0
 |       if (file.getSize() == 0) continue;
 | 
| 
 52 | 
  
 |  | 
| 
 53 | 
 0
 |       String name = getFileName(file.getOriginalFilename());
 | 
| 
 54 | 
 0
 |       log.debug("Handling upload " + name);
 | 
| 
 55 | 
  
 |  | 
| 
 56 | 
 0
 |       File jpeg = new File(base, name);
 | 
| 
 57 | 
  
 |  | 
| 
 58 | 
 0
 |       try
 | 
| 
 59 | 
  
 |       { | 
| 
 60 | 
 0
 |         file.transferTo(jpeg);
 | 
| 
 61 | 
 0
 |         paths.add(filesystem.getPath(jpeg));
 | 
| 
 62 | 
  
 |       } | 
| 
 63 | 
  
 |       catch (Exception e) | 
| 
 64 | 
  
 |       { | 
| 
 65 | 
 0
 |         errors.rejectValue("file" + i, null, e.getMessage());
 | 
| 
 66 | 
 0
 |         log.error("Exception trying to upload to " + jpeg, e);
 | 
| 
 67 | 
 0
 |         if (jpeg.exists()) jpeg.delete();
 | 
| 
 68 | 
  
 |       } | 
| 
 69 | 
  
 |     } | 
| 
 70 | 
  
 |  | 
| 
 71 | 
 0
 |     request.getSession().setAttribute("message", getMessageSourceAccessor().getMessage("message.upload.success",
 | 
| 
 72 | 
  
 |                                                                                        new Object[] { new Integer(paths.size()) })); | 
| 
 73 | 
  
 |  | 
| 
 74 | 
 0
 |     String redirect = "/web/admin/edit?";
 | 
| 
 75 | 
 0
 |     for (Iterator i = paths.iterator(); i.hasNext();)
 | 
| 
 76 | 
  
 |     { | 
| 
 77 | 
 0
 |       redirect += "paths=" + (String) i.next() + "&";
 | 
| 
 78 | 
  
 |     } | 
| 
 79 | 
  
 |  | 
| 
 80 | 
 0
 |     return new ModelAndView(new RedirectView(redirect, true));
 | 
| 
 81 | 
  
 |   } | 
| 
 82 | 
  
 |  | 
| 
 83 | 
 0
 |   private String getFileName(String originalFilename)
 | 
| 
 84 | 
  
 |   { | 
| 
 85 | 
 0
 |     int index = originalFilename.lastIndexOf("/");
 | 
| 
 86 | 
 0
 |     if (index == -1) index = originalFilename.lastIndexOf("\\");
 | 
| 
 87 | 
 0
 |     if (index == -1) index = originalFilename.lastIndexOf(":");
 | 
| 
 88 | 
 0
 |     String name = index == -1 ? originalFilename : originalFilename.substring(index + 1);
 | 
| 
 89 | 
 0
 |     return name;
 | 
| 
 90 | 
  
 |   } | 
| 
 91 | 
  
 |  | 
| 
 92 | 
 0
 |   public void setFilesystem(FileSystem filesystem)
 | 
| 
 93 | 
  
 |   { | 
| 
 94 | 
 0
 |     this.filesystem = filesystem;
 | 
| 
 95 | 
  
 |   } | 
| 
 96 | 
  
 |  | 
| 
 97 | 
  
 | } |