private static File downloadRemoteFile(String revision, String remotePath, String localPath) { DbxClientV2 client = getDbxClient(); FileOutputStream outputStream = null; File outputFile = new File(localPath); try { outputStream = new FileOutputStream(outputFile); client.files().download(remotePath, revision).download(outputStream); } catch (DbxException | IOException ex) { Exceptions.printStackTrace(ex); } finally { IOUtils.closeQuietly(outputStream); } return outputFile; }
@Override protected Integer doInBackground(Void... params) { try { ListFolderResult filderContents = DropboxClientFactory.getClient().files().listFolder(""); dropBoxEntries = filderContents.getEntries(); return 0; } catch (InvalidAccessTokenException e) { Log.e(TAG, "InvalidAccessTokenException downloading database", e); return ERROR_DROPBOX_INVALID_TOKEN; } catch (DbxException e) { Log.e(TAG, "DbxException downloading database", e); return ERROR_DROPBOX; } }
private FileMetadata putSingleFile(File inputFile, String dropboxPath, DropboxUploadMode mode) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileMetadata uploadedFile; try { WriteMode uploadMode; if (mode == DropboxUploadMode.force) { uploadMode = WriteMode.OVERWRITE; } else { uploadMode = WriteMode.ADD; } uploadedFile = client.files().uploadBuilder(dropboxPath).withMode(uploadMode).uploadAndFinish(inputStream, inputFile.length()); return uploadedFile; } finally { IOHelper.close(inputStream); } }
@Override public void writeTo(OutputStream outputStream) { try { String fileStr = SEPARATOR + imageDir + SEPARATOR + id + SEPARATOR + fileName; DbxRequestConfig config = new DbxRequestConfig(APP_IDENTIFIER); DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN); client.files().download(fileStr).download(outputStream); } catch (Exception e) { logger.error(e.getMessage()); throw new ResourceNotFoundException("Image Not Found : " + id + "/" + fileName); } } };
private Map<String, Object> downloadFilesInFolder(String path) throws DropboxException { try { ListFolderResult folderResult = client.files().listFolder(path.equals("/") ? "" : path); Map<String, Object> returnMap = new LinkedHashMap<>(); for (Metadata entry : folderResult.getEntries()) { returnMap.put(entry.getPathDisplay(), downloadSingleFile(entry.getPathDisplay()).getValue()); } return returnMap; } catch (ListFolderErrorException e) { try { DbxDownloader<FileMetadata> listing = client.files().download(path); if (listing == null) { return Collections.emptyMap(); } else { LOG.debug("downloading a single file..."); Map.Entry<String, Object> entry = downloadSingleFile(path); return Collections.singletonMap(entry.getKey(), entry.getValue()); } } catch (DbxException dbxException) { throw new DropboxException(dbxException); } } catch (DbxException e) { throw new DropboxException(e); } }
/** * Rename a remote path with the new path location. * * @param remotePath the existing remote path to be renamed * @param newRemotePath the new remote path substituting the old one * @return a result object with the result of the move operation. * @throws DropboxException */ public DropboxMoveResult move(String remotePath, String newRemotePath) throws DropboxException { try { client.files().moveV2(remotePath, newRemotePath); return new DropboxMoveResult(remotePath, newRemotePath); } catch (DbxException e) { throw new DropboxException(remotePath + " does not exist or cannot obtain metadata", e); } }
/** * Delete every files and subdirectories inside the remote directory. In * case the remotePath is a file, delete the file. * * @param remotePath the remote location to delete * @return a result object with the result of the delete operation. * @throws DropboxException */ public DropboxDelResult del(String remotePath) throws DropboxException { try { client.files().deleteV2(remotePath); } catch (DbxException e) { throw new DropboxException(remotePath + " does not exist or cannot obtain metadata", e); } return new DropboxDelResult(remotePath); }
@Override protected Integer doInBackground(Void... params) { int result = UPLOAD_OK; FileInputStream inputStream = null; try { File databaseFile = getPasswordDatabase().getDatabaseFile(); inputStream = new FileInputStream(databaseFile); DropboxClientFactory.getClient().files() .uploadBuilder("/" + databaseFile.getName()) .withMode(WriteMode.OVERWRITE) .uploadAndFinish(inputStream); } catch (IOException e) { Log.e(TAG, "IOException during database upload", e); result = ERROR_IO; } catch (DbxException e) { Log.e(TAG, "DbxException downloading database", e); return ERROR_DROPBOX; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { Log.e(TAG, "IOException during database upload", e); return ERROR_IO; } } } return result; }
private FileMetadata putSingleBody(Exchange exchange, String dropboxPath, DropboxUploadMode mode) throws Exception { byte[] data = exchange.getIn().getMandatoryBody(byte[].class); InputStream is = new ByteArrayInputStream(data); try { FileMetadata uploadedFile; WriteMode uploadMode; if (mode == DropboxUploadMode.force) { uploadMode = WriteMode.OVERWRITE; } else { uploadMode = WriteMode.ADD; } uploadedFile = client.files().uploadBuilder(dropboxPath).withMode(uploadMode).uploadAndFinish(is, data.length); return uploadedFile; } finally { IOHelper.close(is); } }
List<String> listFiles() throws Exception { if (authSession()) { try { List<String> files = new ArrayList<String>(); ListFolderResult listFolderResult = dropboxClient.files().listFolder(""); for (Metadata metadata : listFolderResult.getEntries()) { String name = metadata.getName(); if (name.endsWith(".backup")) { files.add(name); } } Collections.sort(files, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s2.compareTo(s1); } }); return files; } catch (Exception e) { Log.e("Financisto", "Dropbox: Something wrong", e); throw new ImportExportException(R.string.dropbox_error, e); } } else { throw new ImportExportException(R.string.dropbox_auth_error); } }
private FileMetadata dropBoxSave(String uploadPath, InputStream inputStream)throws Exception{ DbxRequestConfig config = new DbxRequestConfig(APP_IDENTIFIER); DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN); return client.files().uploadBuilder(uploadPath).uploadAndFinish(inputStream); }
private Map.Entry<String, Object> downloadSingleFile(String path) throws DropboxException { try { OutputStreamBuilder target = OutputStreamBuilder.withExchange(exchange); DbxDownloader<FileMetadata> downloadedFile = client.files().download(path); if (downloadedFile != null) { downloadedFile.download(target); LOG.debug("downloaded path={}", path); return new AbstractMap.SimpleEntry<>(path, target.build()); } else { return null; } } catch (DbxException e) { throw new DropboxException(path + " does not exist or cannot obtain metadata", e); } catch (IOException e) { throw new DropboxException(path + " cannot obtain a stream", e); } } }
public FileMetadata uploadFile(File file) throws Exception { if (authSession()) { try { InputStream is = new FileInputStream(file); try { FileMetadata fileMetadata = dropboxClient.files().uploadBuilder("/" + file.getName()).withMode(WriteMode.ADD).uploadAndFinish(is); Log.i("Financisto", "Dropbox: The uploaded file's rev is: " + fileMetadata.getRev()); return fileMetadata; } finally { IOUtil.closeInput(is); } } catch (Exception e) { Log.e("Financisto", "Dropbox: Something wrong", e); throw new ImportExportException(R.string.dropbox_error, e); } } else { throw new ImportExportException(R.string.dropbox_auth_error); } }
public InputStream getFileAsStream(String backupFile) throws Exception { if (authSession()) { try { return dropboxClient.files().downloadBuilder("/" + backupFile).start().getInputStream(); } catch (Exception e) { Log.e("Financisto", "Dropbox: Something wrong", e); throw new ImportExportException(R.string.dropbox_error, e); } } else { throw new ImportExportException(R.string.dropbox_auth_error); } } }
/** * Put or upload a new file or an entire directory to dropbox * * @param localPath the file path or the dir path on the local filesystem * @param remotePath the remote path destination on dropbox * @param mode how a file should be saved on dropbox; in case of "add" the * new file will be renamed in case a file with the same name * already exists on dropbox. in case of "force" the file already * existing with the same name will be overridden. * @return a result object reporting for each remote path the result of the * operation. * @throws DropboxException */ public DropboxFileUploadResult put(String localPath, String remotePath, DropboxUploadMode mode) throws DropboxException { // in case the remote path is not specified, the remotePath = localPath String dropboxPath = remotePath == null ? localPath : remotePath; boolean isPresent = true; try { client.files().getMetadata(dropboxPath); } catch (DbxException e) { isPresent = false; } if (localPath != null) { return putFile(localPath, mode, dropboxPath, isPresent); } else { return putBody(exchange, mode, dropboxPath, isPresent); } }
LOG.debug("Search no query"); try { listing = client.files().search(remotePath, null); searchMatches = listing.getMatches(); return new DropboxSearchResult(searchMatches); LOG.debug("Search by query: {}", query); try { listing = client.files().search(remotePath, query); searchMatches = listing.getMatches(); return new DropboxSearchResult(searchMatches);
private void restoreThisRevisionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restoreThisRevisionButtonActionPerformed DbxClientV2 client = DbxUtil.getDbxClient(); if (client == null) { return; } if (displayedRevision != null) { try { FileMetadata restoredFileMetadata = client.files().restore(displayedRevision.getPath(), displayedRevision.getRevision()); File restored = DbxUtil.downloadRemoteFile(restoredFileMetadata, ApplicationUtils.getTempSourceFile()); etc.setEditorContent(FileUtils.readFileToString(restored)); etc.requestActive(); } catch (DbxException e) { DbxUtil.showDbxAccessDeniedPrompt(); } catch (IOException e) { Exceptions.printStackTrace(e); } } }//GEN-LAST:event_restoreThisRevisionButtonActionPerformed
@Before public void setUp() { try { testDoc = new DocumentImpl(); testDoc.setName("testDoc.txt"); testDoc.setIdentifier("testDoc"); testDoc.setLastModified(new Date()); testDoc.setContent(new String("test doc content").getBytes()); InputStream testInputStream = IOUtils.toInputStream("test doc content", "UTF-8"); when(auth.authorize(anyString(), anyString())).thenReturn(client); when(client.files()).thenReturn(fileRequests); // upload when(fileRequests.uploadBuilder(anyString())).thenReturn(uploadBuilder); when(uploadBuilder.withMode(any(WriteMode.class))).thenReturn(uploadBuilder); when(uploadBuilder.withClientModified(any(Date.class))).thenReturn(uploadBuilder); when(uploadBuilder.uploadAndFinish(any(java.io.InputStream.class))).thenReturn(metaData); // download when(fileRequests.downloadBuilder(anyString())).thenReturn(downloadBuilder); when(downloadBuilder.start()).thenReturn(downloader); when(downloader.getInputStream()).thenReturn(testInputStream); } catch (Exception e) { fail(e.getMessage()); } }
DbxDownloader<FileMetadata> downloader = client.files().download(entry.getPath(), entry.getRevision()); downloader.download(outputStream);