Search in sources :

Example 1 with UploadedFile

use of org.primefaces.model.UploadedFile in project dataverse by IQSS.

the class DatasetWidgetsPage method handleImageFileUpload.

public void handleImageFileUpload(FileUploadEvent event) {
    logger.fine("handleImageFileUpload clicked");
    UploadedFile uploadedFile = event.getFile();
    try {
        updateDatasetThumbnailCommand = new UpdateDatasetThumbnailCommand(dvRequestService.getDataverseRequest(), dataset, UpdateDatasetThumbnailCommand.UserIntent.setNonDatasetFileAsThumbnail, null, uploadedFile.getInputstream());
    } catch (IOException ex) {
        String error = "Unexpected error while uploading file.";
        logger.warning("Problem uploading dataset thumbnail to dataset id " + dataset.getId() + ". " + error + " . Exception: " + ex);
        updateDatasetThumbnailCommand = null;
        return;
    }
    File file = null;
    try {
        file = FileUtil.inputStreamToFile(uploadedFile.getInputstream());
    } catch (IOException ex) {
        Logger.getLogger(DatasetWidgetsPage.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }
    String base64image = ImageThumbConverter.generateImageThumbnailFromFileAsBase64(file, ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
    if (base64image != null) {
        datasetThumbnail = new DatasetThumbnail(base64image, datasetFileThumbnailToSwitchTo);
    } else {
        Logger.getLogger(DatasetWidgetsPage.class.getName()).log(Level.SEVERE, "Failed to produce a thumbnail from the uploaded dataset logo.");
    }
}
Also used : DatasetThumbnail(edu.harvard.iq.dataverse.dataset.DatasetThumbnail) UploadedFile(org.primefaces.model.UploadedFile) UpdateDatasetThumbnailCommand(edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetThumbnailCommand) IOException(java.io.IOException) File(java.io.File) UploadedFile(org.primefaces.model.UploadedFile)

Example 2 with UploadedFile

use of org.primefaces.model.UploadedFile in project dataverse by IQSS.

the class EditDatafilesPage method handleLabelsFileUpload.

public void handleLabelsFileUpload(FileUploadEvent event) {
    logger.fine("entering handleUpload method.");
    UploadedFile file = event.getFile();
    if (file != null) {
        InputStream uploadStream = null;
        try {
            uploadStream = file.getInputstream();
        } catch (IOException ioex) {
            logger.info("the file " + file.getFileName() + " failed to upload!");
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "upload failure", "the file " + file.getFileName() + " failed to upload!");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return;
        }
        savedLabelsTempFile = saveTempFile(uploadStream);
        logger.fine(file.getFileName() + " is successfully uploaded.");
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
// process file (i.e., just save it in a temp location; for now):
}
Also used : UploadedFile(org.primefaces.model.UploadedFile) InputStream(java.io.InputStream) IOException(java.io.IOException) FacesMessage(javax.faces.application.FacesMessage)

Example 3 with UploadedFile

use of org.primefaces.model.UploadedFile in project dataverse by IQSS.

the class EditDatafilesPage method handleFileUpload.

/**
 * Handle native file replace
 * @param event
 * @throws java.io.IOException
 */
public void handleFileUpload(FileUploadEvent event) throws IOException {
    if (!uploadInProgress) {
        uploadInProgress = true;
    }
    if (event == null) {
        throw new NullPointerException("event cannot be null");
    }
    UploadedFile uFile = event.getFile();
    if (uFile == null) {
        throw new NullPointerException("uFile cannot be null");
    }
    /**
     * For File Replace, take a different code path
     */
    if (isFileReplaceOperation()) {
        handleReplaceFileUpload(event, uFile.getInputstream(), uFile.getFileName(), uFile.getContentType(), event, null);
        if (fileReplacePageHelper.hasContentTypeWarning()) {
            RequestContext context = RequestContext.getCurrentInstance();
            RequestContext.getCurrentInstance().update("datasetForm:fileTypeDifferentPopup");
            context.execute("PF('fileTypeDifferentPopup').show();");
        }
        return;
    }
    List<DataFile> dFileList = null;
    try {
        // Note: A single uploaded file may produce multiple datafiles -
        // for example, multiple files can be extracted from an uncompressed
        // zip file.
        dFileList = FileUtil.createDataFiles(workingVersion, uFile.getInputstream(), uFile.getFileName(), uFile.getContentType(), systemConfig);
    } catch (IOException ioex) {
        logger.warning("Failed to process and/or save the file " + uFile.getFileName() + "; " + ioex.getMessage());
        return;
    }
    /*catch (FileExceedsMaxSizeException ex) {
            logger.warning("Failed to process and/or save the file " + uFile.getFileName() + "; " + ex.getMessage());
            return;
        }*/
    // -----------------------------------------------------------
    // These raw datafiles are then post-processed, in order to drop any files
    // already in the dataset/already uploaded, and to correct duplicate file names, etc.
    // -----------------------------------------------------------
    String warningMessage = processUploadedFileList(dFileList);
    if (warningMessage != null) {
        uploadWarningMessage = warningMessage;
        FacesContext.getCurrentInstance().addMessage(event.getComponent().getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "upload warning", warningMessage));
        // save the component id of the p:upload widget, so that we could
        // send an info message there, from elsewhere in the code:
        uploadComponentId = event.getComponent().getClientId();
    }
}
Also used : UploadedFile(org.primefaces.model.UploadedFile) RequestContext(org.primefaces.context.RequestContext) IOException(java.io.IOException) FacesMessage(javax.faces.application.FacesMessage)

Example 4 with UploadedFile

use of org.primefaces.model.UploadedFile in project dataverse by IQSS.

the class ThemeWidgetFragment method cleanupTempDirectory.

@PreDestroy
public /**
 *  Cleanup by deleting temp directory and uploaded files
 */
void cleanupTempDirectory() {
    try {
        if (tempDir != null) {
            for (File f : tempDir.listFiles()) {
                Files.deleteIfExists(f.toPath());
            }
            Files.deleteIfExists(tempDir.toPath());
        }
    } catch (IOException e) {
        // improve error handling
        throw new RuntimeException("Error deleting temp directory", e);
    }
    uploadedFile = null;
    tempDir = null;
}
Also used : IOException(java.io.IOException) UploadedFile(org.primefaces.model.UploadedFile) File(java.io.File) PreDestroy(javax.annotation.PreDestroy)

Example 5 with UploadedFile

use of org.primefaces.model.UploadedFile in project dataverse by IQSS.

the class ThemeWidgetFragment method handleImageFileUpload.

/**
 * Copy uploaded file to temp area, until we are ready to save
 * Copy filename into Dataverse logo
 * @param event
 */
public void handleImageFileUpload(FileUploadEvent event) {
    logger.finer("entering fileUpload");
    if (this.tempDir == null) {
        createTempDir();
        logger.finer("created tempDir");
    }
    UploadedFile uFile = event.getFile();
    try {
        uploadedFile = new File(tempDir, uFile.getFileName());
        if (!uploadedFile.exists()) {
            uploadedFile.createNewFile();
        }
        logger.finer("created file");
        Files.copy(uFile.getInputstream(), uploadedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        logger.finer("copied inputstream to file");
        editDv.getDataverseTheme().setLogo(uFile.getFileName());
    } catch (IOException e) {
        logger.finer("caught IOException");
        logger.throwing("ThemeWidgetFragment", "handleImageFileUpload", e);
        // improve error handling
        throw new RuntimeException("Error uploading logo file", e);
    }
    // If needed, set the default values for the logo
    if (editDv.getDataverseTheme().getLogoFormat() == null) {
        editDv.getDataverseTheme().setLogoFormat(DataverseTheme.ImageFormat.SQUARE);
    }
    logger.finer("end handelImageFileUpload");
}
Also used : UploadedFile(org.primefaces.model.UploadedFile) IOException(java.io.IOException) UploadedFile(org.primefaces.model.UploadedFile) File(java.io.File)

Aggregations

IOException (java.io.IOException)14 UploadedFile (org.primefaces.model.UploadedFile)14 FacesMessage (javax.faces.application.FacesMessage)11 File (java.io.File)5 InputStream (java.io.InputStream)4 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)4 SKOSCreationException (org.semanticweb.skos.SKOSCreationException)4 ResourceBundle (java.util.ResourceBundle)3 BufferedImage (java.awt.image.BufferedImage)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 ImagesHelper (mom.trd.opentheso.bdd.helper.ImagesHelper)2 ImportSkosHelper (mom.trd.opentheso.core.imports.helper.ImportSkosHelper)2 ImportTabulateHelper (mom.trd.opentheso.core.imports.helper.ImportTabulateHelper)2 ReadFileSKOS (mom.trd.opentheso.core.imports.old.ReadFileSKOS)2 DatasetThumbnail (edu.harvard.iq.dataverse.dataset.DatasetThumbnail)1 UpdateDatasetThumbnailCommand (edu.harvard.iq.dataverse.engine.command.impl.UpdateDatasetThumbnailCommand)1 FileInputStream (java.io.FileInputStream)1