private void downloadAttachment(AttachmentEntry attachment, File rootDirectory, EntryStore entryStore, SitesService sitesService) { BasePageEntry<?> parent = entryStore.getParent(attachment.getId()); if (parent != null) { File relativePath = getPath(parent, entryStore); if (relativePath != null) { File folder = new File(rootDirectory, relativePath.getPath()); folder.mkdirs(); File file = new File(folder, attachment.getTitle().getPlainText()); attachmentDownloader.download(attachment, file, sitesService); } } }
/** * Downloads the given attachment to the given file name. */ @Override public void download(AttachmentEntry attachment, File file, SitesService sitesService) { checkNotNull(attachment); checkNotNull(file); MediaContent mediaContent = new MediaContent(); mediaContent.setUri(((OutOfLineContent) attachment.getContent()).getUri()); try { MediaSource mediaSource = sitesService.getMedia(mediaContent); InputStream inStream = mediaSource.getInputStream(); OutputStream outStream = new FileOutputStream(file); byte[] buf = new byte[4*1024]; int bytesRead; while((bytesRead = inStream.read(buf)) != -1) { outStream.write(buf, 0, bytesRead); } inStream.close(); outStream.close(); } catch (IOException e) { LOGGER.log(Level.WARNING, "Error downloading attachment: " + attachment.getTitle().getPlainText(), e); } catch (ServiceException e) { LOGGER.log(Level.WARNING, "Error downloading attachment: " + attachment.getTitle().getPlainText(), e); } } }
+ attachment.getTitle().getPlainText() + '.'); downloadAttachment(attachment, rootDirectory, entryStore, sitesService); progressListener.setProgress(((double) ++currentEntries) / totalEntries);