/** * 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; }
/** * True if the file format related to the passed extension can be download * * @param extension * @return */ private boolean isAllowedFormat(String extension) { // If no extension, not possible to recognized the format if ("".equals(extension.trim())) return false; // check in white list, if found then allowed for (String s : formatWhiteList.split(",")) if (StorageUtils.compareExtension(extension, s.trim())) return true; // check black list, if found then forbidden for (String s : formatBlackList.split(",")) if (StorageUtils.compareExtension(extension, s.trim())) return false; // Not found in both list: if white list is empty, allowed return "".equals(formatWhiteList.trim()); }
/** * Write an {@link InputStream} in a {@link File} * * @param tmp * @param fis * @return * @throws IOException */ private File writeInTmpFile(File tmp, InputStream fis) throws IOException { FileOutputStream fos = new FileOutputStream(tmp); try { StorageUtils.writeInOut(fis, fos, true); return tmp; } catch (Exception e) { throw new RuntimeException("Error writing uploaded File in temp file", e); } finally { fos.close(); fis.close(); } }
/** * 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) + ")"); } }