use of com.dropbox.core.v2.files.FileMetadata in project dropbox-sdk-java by dropbox.
the class DropboxBrowse method doBrowse.
// -------------------------------------------------------------------------------------------
// GET /browse?path=...
// -------------------------------------------------------------------------------------------
// The page that lets you browse your Dropbox account. Makes Dropbox API calls to figure out
// the contents of your files and folders and display them to you.
public void doBrowse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!common.checkGet(request, response))
return;
User user = common.getLoggedInUser(request);
if (user == null) {
common.pageSoftError(response, "Can't do /browse. Nobody is logged in.");
return;
}
DbxClientV2 dbxClient = requireDbxClient(request, response, user);
if (dbxClient == null)
return;
String path = request.getParameter("path");
if (path == null || path.length() == 0) {
renderFolder(response, user, dbxClient, "");
} else {
String pathError = DbxPathV2.findError(path);
if (pathError != null) {
response.sendError(400, "Invalid path: " + jq(path) + ": " + pathError);
return;
}
Metadata metadata;
try {
metadata = dbxClient.files().getMetadata(path);
} catch (GetMetadataErrorException ex) {
if (ex.errorValue.isPath()) {
LookupError le = ex.errorValue.getPathValue();
if (le.isNotFound()) {
response.sendError(400, "Path doesn't exist on Dropbox: " + jq(path));
return;
}
}
common.handleException(response, ex, "getMetadata(" + jq(path) + ")");
return;
} catch (DbxException ex) {
common.handleDbxException(response, user, ex, "getMetadata(" + jq(path) + ")");
return;
}
path = DbxPathV2.getParent(path) + "/" + metadata.getName();
if (metadata instanceof FolderMetadata) {
renderFolder(response, user, dbxClient, path);
} else {
renderFile(response, path, (FileMetadata) metadata);
}
}
}
use of com.dropbox.core.v2.files.FileMetadata in project dropbox-sdk-java by dropbox.
the class DownloadFileTask method doInBackground.
@Override
protected File doInBackground(FileMetadata... params) {
FileMetadata metadata = params[0];
try {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(path, metadata.getName());
// Make sure the Downloads directory exists.
if (!path.exists()) {
if (!path.mkdirs()) {
mException = new RuntimeException("Unable to create directory: " + path);
}
} else if (!path.isDirectory()) {
mException = new IllegalStateException("Download path is not a directory: " + path);
return null;
}
// Download the file.
try (OutputStream outputStream = new FileOutputStream(file)) {
mDbxClient.files().download(metadata.getPathLower(), metadata.getRev()).download(outputStream);
}
// Tell android about the file
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
mContext.sendBroadcast(intent);
return file;
} catch (DbxException | IOException e) {
mException = e;
}
return null;
}
use of com.dropbox.core.v2.files.FileMetadata in project dropbox-sdk-java by dropbox.
the class FilesActivity method uploadFile.
private void uploadFile(String fileUri) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
dialog.setMessage("Uploading");
dialog.show();
new UploadFileTask(this, DropboxClientFactory.getClient(), new UploadFileTask.Callback() {
@Override
public void onUploadComplete(FileMetadata result) {
dialog.dismiss();
String message = result.getName() + " size " + result.getSize() + " modified " + DateFormat.getDateTimeInstance().format(result.getClientModified());
Toast.makeText(FilesActivity.this, message, Toast.LENGTH_SHORT).show();
// Reload the folder
loadData();
}
@Override
public void onError(Exception e) {
dialog.dismiss();
Log.e(TAG, "Failed to upload file.", e);
Toast.makeText(FilesActivity.this, "An error has occurred", Toast.LENGTH_SHORT).show();
}
}).execute(fileUri, mPath);
}
use of com.dropbox.core.v2.files.FileMetadata in project dropbox-sdk-java by dropbox.
the class Main method printChanges.
/**
* Prints changes made to a folder in Dropbox since the given
* cursor was retrieved.
*
* @param dbxClient Dropbox client to use for fetching folder changes
* @param cursor lastest cursor received since last set of changes
*
* @return latest cursor after changes
*/
private static String printChanges(DbxClientV2 client, String cursor) throws DbxApiException, DbxException {
while (true) {
ListFolderResult result = client.files().listFolderContinue(cursor);
for (Metadata metadata : result.getEntries()) {
String type;
String details;
if (metadata instanceof FileMetadata) {
FileMetadata fileMetadata = (FileMetadata) metadata;
type = "file";
details = "(rev=" + fileMetadata.getRev() + ")";
} else if (metadata instanceof FolderMetadata) {
FolderMetadata folderMetadata = (FolderMetadata) metadata;
type = "folder";
details = folderMetadata.getSharingInfo() != null ? "(shared)" : "";
} else if (metadata instanceof DeletedMetadata) {
type = "deleted";
details = "";
} else {
throw new IllegalStateException("Unrecognized metadata type: " + metadata.getClass());
}
System.out.printf("\t%10s %24s \"%s\"\n", type, details, metadata.getPathLower());
}
// update cursor to fetch remaining results
cursor = result.getCursor();
if (!result.getHasMore()) {
break;
}
}
return cursor;
}
use of com.dropbox.core.v2.files.FileMetadata in project dropbox-sdk-java by dropbox.
the class Main method main.
public static void main(String[] args) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
// Upload "test.txt" to Dropbox
try (InputStream in = new FileInputStream("test.txt")) {
FileMetadata metadata = client.files().uploadBuilder("/test.txt").uploadAndFinish(in);
}
}
Aggregations