private void visitEntry(final Entry entry, DropboxEntryVisitor visitor) { if (null != entry.contents) { logger.debug(entry.path + "contents size: " + entry.contents.size()); for (Entry child : entry.contents) { if (!child.isDeleted) { if (!child.isDir) { visitor.visitFile(child); } else { try { visitor.preVisitDirectory(child); } catch (IOException e) { logger.error(e.getMessage()); } try { Entry childWithContents = api.metadata( child.path, 0, MetaHandler.getHash(child.path), true, MetaHandler.getRev(child.path)); visitEntry(childWithContents, visitor); } catch (DropboxException e) { logger.error(e.getMessage()); } visitor.postVisitDirectory(child); } } } } }
public boolean pull() { // TODO Auto-generated method stub if (!isLinked()) return false; File file; FileOutputStream outputStream = null; try { Entry entry = mDBApi.metadata("/", 0, null, true, null); for (Entry e : entry.contents) { if (!e.isDir) { app.getGui().setStatus("Downloading..."); file = new File(NoteManager.defaultPath.concat(e.fileName())); outputStream = new FileOutputStream(file); DropboxFileInfo info = mDBApi.getFile("/".concat(e.fileName()), null, outputStream, null); app.getGui().setStatus("Downloaded ".concat(info.getMetadata().fileName())); outputStream.close(); } } return true; } catch (DropboxException e) { e.printStackTrace(); app.getGui().setStatus("Couldn't connect to server."); } catch (FileNotFoundException e) { e.printStackTrace(); app.getGui().setStatus("Files not found."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); app.getGui().setStatus("IO errors."); } return false; }
private void loadMetadata(DropboxFile file) { Log.d(TAG, "Loading metadata for " + file.getRemoteFile()); DropboxAPI.Entry metadata = null; try { metadata = dropboxApi.metadata(file.getRemoteFile(), 0, null, false, null); } catch (DropboxServerException se) { if (se.error == DropboxServerException._404_NOT_FOUND) { Log.d(TAG, "metadata NOT found! Returning NOT_FOUND status."); file.setStatus(DropboxFileStatus.NOT_FOUND); return; } throw new RemoteException("Server Exception: " + se.error + " " + se.reason, se); } catch (DropboxException e) { throw new RemoteException("Dropbox Exception: " + e.getMessage(), e); } Log.d(TAG, "Metadata retrieved. rev on Dropbox = " + metadata.rev); Log.d(TAG, "local rev = " + file.getOriginalRev()); file.setLoadedMetadata(metadata); if (metadata.rev.equals(file.getOriginalRev())) { // don't bother downloading if the rev is the same Log.d(TAG, "revs match. returning NOT_CHANGED status."); file.setStatus(DropboxFileStatus.NOT_CHANGED); } else if (metadata.isDeleted) { Log.d(TAG, "File marked as deleted on Dropbox! Returning NOT_FOUND status."); file.setStatus(DropboxFileStatus.NOT_FOUND); } else { Log.d(TAG, "revs don't match. returning FOUND status."); file.setStatus(DropboxFileStatus.FOUND); } }
private void syncStorage() { try { logger.debug("Synching storage."); final String rootDir = "/"; Entry folderEntry = api.metadata(rootDir, 0, MetaHandler.getHash(rootDir), true, MetaHandler.getRev(rootDir)); // TODO handle removed entries visitEntry( folderEntry, new DropboxEntryVisitor() { @Override public void preVisitDirectory(Entry entry) throws IOException { if (!MetaHandler.isStored(entry.path)) { logger.debug("Creating local dir: " + entry.path); Path filePath = FileSystems.getDefault().getPath(startDir.toString(), entry.path.substring(1)); WatchKey key = filePath .getParent() .register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.remove(key); key.cancel(); filePath.toFile().mkdir(); key = filePath .getParent() .register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); keys.put(key, filePath.getParent()); MetaHandler.setFile(entry.path, filePath.toFile(), entry.rev); } } @Override public void visitFile(Entry entry) { if (!MetaHandler.isStored(entry.path)) downloadFile(entry.path); else if (!MetaHandler.isCurrent(entry.path, entry.rev)) { Path fullPath = FileSystems.getDefault().getPath(startDir.toString(), entry.path.substring(1)); try { logger.debug("Uploading file " + entry.path); uploadFile(entry.path, fullPath); } catch (Exception e) { logger.error("Error uploading file: " + e.getMessage()); } } } }); } catch (DropboxException ex) { logger.error("Error syching root dir: " + ex.getMessage()); } }
/** * First of all create a backup of Money and Categories Database on the SD card. After that, get * those backups from the SD card and upload it to the DropBox after the app checks if it already * exist. If the backup already exists then delete the old and create the new one. * * @param params * @return */ @Override protected String doInBackground(String... params) { String MoneyDBpath = "/data/" + context.getPackageName() + "/databases/MoneyDatabase"; String MoneyOutputName = "/TransactionsBackup"; BackupRestoreSD backupRestoreSD = new BackupRestoreSD(MoneyDBpath, MoneyOutputName, context); backupRestoreSD.backup(); File MoneyBackup = new File(backupRestoreSD.getSDPath() + "/Pocket-Wallet/TransactionsBackup"); String CategoriesDBpath = "/data/" + context.getPackageName() + "/databases/categories"; String CategoriesOutputName = "/CategoriesBackup"; backupRestoreSD = new BackupRestoreSD(CategoriesDBpath, CategoriesOutputName, context); backupRestoreSD.backup(); File CategoryBackup = new File(backupRestoreSD.getSDPath() + "/Pocket-Wallet/CategoriesBackup"); try { InputStream Mis = new FileInputStream(MoneyBackup); InputStream Cis = new FileInputStream(CategoryBackup); DropboxAPI.Entry metadata = api.metadata("/", 1000, null, true, null); boolean flag = false, cflag = false; List<DropboxAPI.Entry> CFolder = metadata.contents; for (DropboxAPI.Entry entry : CFolder) { if (entry.fileName().equals("TransactionsBackup")) { flag = true; } else if (entry.fileName().equals("CategoriesBackup")) { cflag = true; } if (flag && cflag) { break; } } if (!flag) { api.putFile("TransactionsBackup", Mis, MoneyBackup.length(), null, null); } else { api.delete("/TransactionsBackup"); api.putFile("TransactionsBackup", Mis, MoneyBackup.length(), null, null); } if (!cflag) { api.putFile("CategoriesBackup", Cis, CategoryBackup.length(), null, null); } else { api.delete("/CategoriesBackup"); api.putFile("CategoriesBackup", Cis, CategoryBackup.length(), null, null); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DropboxException e) { e.printStackTrace(); } return null; }