Example #1
0
 /**
  * Utility that deletes any temporary uploads that were performed during editing, but were not
  * saved.
  *
  * @throws IOException if there was a problem reading the uploads directory or deleting any
  *     temporary uploads
  */
 private void deleteTempUploads() throws IOException {
   if (selected != null) {
     for (Variant variant : selected.getVariants()) {
       Collection<File> tmpFiles =
           FileUtils.listFiles(
               UploadsServlet.getUploadsDirectory(),
               FileFilterUtils.prefixFileFilter("tmp-" + variant.getUuid()),
               null);
       for (File tmpFile : tmpFiles) {
         FileUtils.forceDelete(tmpFile);
       }
     }
   }
 }
Example #2
0
 /**
  * JSF method to upload a new image file.
  *
  * @param uploadedImage a PrimeFaces upload
  */
 public void setUploadedImage(final UploadedFile uploadedImage) {
   BufferedInputStream in = null;
   BufferedOutputStream out = null;
   try {
     // Determine if upload has a valid MIME type
     if (!uploadedImage.getContentType().startsWith("image")) {
       throw new IOException("Not a valid image type");
     }
     // Determine temporary image name
     String ext = FilenameUtils.getExtension(uploadedImage.getFileName());
     currentVariant.setItemImage(
         "tmp-" + currentVariant.getUuid() + "-" + new Date().getTime() + "." + ext);
     File path = new File(UploadsServlet.getUploadsDirectory(), currentVariant.getItemImage());
     // Write file contents to disk
     in = new BufferedInputStream(uploadedImage.getInputstream(), BUFFER_SIZE);
     out = new BufferedOutputStream(new FileOutputStream(path), BUFFER_SIZE);
     byte[] buffer = new byte[BUFFER_SIZE];
     int length = -1;
     while ((length = in.read(buffer)) >= 0) {
       out.write(buffer, 0, length);
     }
     uploadedImages.put(currentVariant, uploadedImage);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } finally {
     try {
       // Close streams
       if (in != null) {
         in.close();
       }
       if (out != null) {
         out.flush();
         out.close();
       }
     } catch (IOException e) {
       // Ignore, only closing streams
     }
   }
 }