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.");
}
}
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):
}
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();
}
}
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;
}
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");
}
Aggregations