/** * Upload one File and create the {@link de.mpg.imeji.logic.vo.Item} * * @param bytes * @throws Exception */ private Item uploadFile(File file) { try { if (!StorageUtils.hasExtension(title)) title += StorageUtils.guessExtension(file); validateName(); storageController = new StorageController(); Item item = null; if (importImageToFile) { item = replaceWebResolutionAndThumbnailOfItem(findItemByFileName(title), file); } else if (uploadFileToItem) { item = replaceFileOfItem(findItemByFileName(title), file); } else { UploadResult uploadResult = storageController.upload(title, file, id); item = ImejiFactory.newItem( collection, user, uploadResult.getId(), title, URI.create(uploadResult.getOrginal()), URI.create(uploadResult.getThumb()), URI.create(uploadResult.getWeb())); item.setChecksum(uploadResult.getChecksum()); } sNum += 1; sFiles.add(title); return item; } catch (Exception e) { fNum += 1; fFiles.add(" File " + title + " not uploaded: " + e.getMessage()); logger.error("Error uploading item: ", e); e.printStackTrace(); } return null; }
/** * Throws an {@link Exception} if the file ca be upload. Works only if the file has an extension * (therefore, for file without extension, the validation will only occur when the file has been * stored locally) */ private void validateName() { if (StorageUtils.hasExtension(title)) { if (checkNameUnique) { // if the checkNameUnique is checked, check that two files with the same name is not // possible if ((importImageToFile || uploadFileToItem) && filenameExistsInCurrentUpload()) throw new RuntimeException("There is already at least one item with the filename "); else if (!((importImageToFile || uploadFileToItem)) && filenameExistsInCollection(title) || filenameExistsInCurrentUpload()) throw new RuntimeException( "There is already at least one item with the filename " + FilenameUtils.getBaseName(title)); } if (!isAllowedFormat(FilenameUtils.getExtension(title))) throw new RuntimeException( sessionBean.getMessage("upload_format_not_allowed") + " (" + FilenameUtils.getExtension(title) + ")"); } }