/** * Upload a file from the web * * @return */ public String uploadFromLink() { try { externalUrl = URLDecoder.decode(externalUrl, "UTF-8"); URL url = new URL(externalUrl); title = findFileName(url); File tmp = createTmpFile(); try { StorageController externalController = new StorageController("external"); FileOutputStream fos = new FileOutputStream(tmp); externalController.read(url.toString(), fos, true); Item item = uploadFile(tmp); if (item != null) { UserController uc = new UserController(null); user = uc.retrieve(getUser().getEmail()); ItemController ic = new ItemController(user); ic.create(item, collection.getId()); } externalUrl = ""; } finally { FileUtils.deleteQuietly(tmp); } } catch (Exception e) { logger.error("Error uploading file from link: " + externalUrl, e); fFiles.add(e.getMessage() + ": " + title); } return ""; }
/** * 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; }
/** * Rmove the files which don't have been created with an {@link Item}. Ca happen if the upload is * interrupted */ private void removeFiles() { if (itemList != null) { for (Item item : itemList) { storageController.delete(item.getStorageId()); } } itemList = new ArrayList<Item>(); }
/** * Replace the File of an Item * * @param item * @param uploadResult * @return * @throws Exception */ private Item replaceFileOfItem(Item item, File file) throws Exception { storageController.update(item.getFullImageUrl().toString(), file); item.setChecksum(storageController.calculateChecksum(file)); replaceWebResolutionAndThumbnailOfItem(item, file); return item; }
/** * Replace the web resolution and thumbnail files with the one the the upload result * * @param item * @param uploadResult * @return * @throws Exception */ private Item replaceWebResolutionAndThumbnailOfItem(Item item, File file) throws Exception { storageController.update(item.getWebImageUrl().toString(), file); storageController.update(item.getThumbnailImageUrl().toString(), file); return item; }