Exemplo n.º 1
0
  public void uploadImage(UploadEvent evento) throws FileNotFoundException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
    String caminhoReal = sc.getRealPath("/");
    String extensao = "";
    UploadItem item = evento.getUploadItem();
    String fileName = item.getFileName();
    String ext[] = fileName.split("\\.");
    int i = ext.length;

    if (i > 1) {
      extensao = ext[i - 1];
    }

    Long tempo = System.currentTimeMillis();
    OutputStream out =
        new FileOutputStream(caminhoReal + "/ImagensPizza/" + "img" + tempo + "." + extensao);
    setImagePath("/ImagensPizza/" + "img" + tempo + "." + extensao);

    try {
      out.write(item.getData());
      out.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 2
0
 public void listener(UploadEvent event) throws Exception {
   UploadItem item = event.getUploadItem();
   File file = new File();
   file.setLength(item.getData().length);
   file.setName(item.getFileName());
   file.setData(item.getData());
   files.add(file);
   uploadsAvailable--;
 }
Exemplo n.º 3
0
 private Image constructImage(UploadItem item) {
   Image image = new Image();
   image.setUploaded(new Date());
   image.setDescription(item.getFileName());
   image.setName(item.getFileName());
   image.setSize(item.getFileSize());
   image.setPath(item.getFileName());
   image.setAllowComments(true);
   return image;
 }
Exemplo n.º 4
0
 /**
  * Listenet, that invoked during file upload process. Only registered users can upload images.
  *
  * @param event - event, indicated that file upload started
  */
 @Restrict("#{s:hasRole('admin')}")
 public void listener(UploadEvent event) {
   UploadItem item = event.getUploadItem();
   // Construct image from item
   Image image = constructImage(item);
   try {
     // Extract metadata(size, camera model etc..)
     extractMetadata(item, image);
   } catch (Exception e1) {
     addError(item, image, Constants.FILE_PROCESSING_ERROR);
     return;
   }
   image.setAlbum(model.getSelectedAlbum());
   if (image.getAlbum() == null) {
     addError(item, image, Constants.NO_ALBUM_TO_DOWNLOAD_ERROR);
     return;
   }
   try {
     // Check if image with given name already exist
     if (imageAction.isImageWithThisPathExist(image.getAlbum(), image.getPath())) {
       // If exist generate new path for image
       String newPath = generateNewPath(image);
       image.setPath(newPath);
       image.setName(newPath);
     }
     // Save to database
     imageAction.addImage(image);
   } catch (Exception e) {
     addError(item, image, Constants.IMAGE_SAVING_ERROR);
     return;
   }
   // Save to disk
   if (!fileManager.addImage(image.getFullPath(), item.getFile().getPath())) {
     addError(item, image, Constants.FILE_SAVE_ERROR);
     return;
   }
   // Prepare to show in UI
   fileWrapper.getFiles().add(image);
   Events.instance().raiseEvent(Constants.IMAGE_ADDED_EVENT, image);
   // Delete temporary file
   item.getFile().delete();
 }
Exemplo n.º 5
0
 private void extractMetadata(UploadItem item, Image image) {
   InputStream in = null;
   try {
     in = new FileInputStream(item.getFile());
     Metadata metadata = JpegMetadataReader.readMetadata(in);
     Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
     Directory jpgDirectory = metadata.getDirectory(JpegDirectory.class);
     setupCameraModel(image, exifDirectory);
     setupDimensions(image, exifDirectory, jpgDirectory);
     setupCreatedDate(image, exifDirectory);
   } catch (Exception e) {
     addError(item, image, Constants.IMAGE_SAVING_ERROR);
   } finally {
     try {
       in.close();
     } catch (IOException e) {
       addError(item, image, Constants.IMAGE_SAVING_ERROR);
     }
   }
 }
Exemplo n.º 6
0
 private void addError(UploadItem item, Image image, String error) {
   fileWrapper.onFileUploadError(image, error);
   item.getFile().delete();
 }