How to use the dropbox api correctly on localhost? - java

I am using dropbox core api.
// Get files and folder metadata from Dropbox root directory
public static ListFolderResult listFiles(DbxClientV2 client) throws DbxException {
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());
}
return result;
}
Now the user should be able to list all folders on localhost via /list. How can I do that?

You need first to autheticate the user to dropbox using the dropbox java api
Please look at this, here is everything described clearly Dropbox JAVA API

Related

File not being renamed in Android 10

I wrote a solution for renaming files, but nothing happens in Android 10, although in 11 and above it is renamed normally.
public class PathDiv {
public static String dev(String path){
return path.replaceAll("^(.*)/.*?$","$1");
}
public static void renameFilesInDir(String path, String dirIn, String ext) {
File checkFile = new File(path);
if (checkFile.isFile()) {
try {
checkFile.renameTo(new File(dirIn, ext));
} catch (Exception e) {
e.printStackTrace();
}
}
}
path: /storage/emulated/0/Music/04. Kasger - Highland.mp3 dirIn: /storage/emulated/0/Music ext: Highland.mp3
PathDiv.renameFilesInDir(songItem.realUri,PathDiv.dev(songItem.realUri),newName.toString())
What's the problem?
In Android 10 and Higher Version of android, you can't Rename or Delete directly.
For Rename or Delete a media file you want to send a request.
Here Read Android Official Document how to create a request
For your information, to Delete a media file Dependency Available here you can use it easily it's created by me
checkFile.renameTo(new File(dirIn, ext)); It Also Works for this you want to add ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION permission in your app but before using this permission please refer to Google Play Police Update

Why querying google cloud files returns an empty list?

My android app creates an app data file with this method
public Task<String> createFile() {
return Tasks.call(mExecutor, () -> {
File metadata = new File()
.setParents(Collections.singletonList("appDataFolder"))
.setName("profile.json");
java.io.File filePath = new java.io.File(Gdx.files.getLocalStoragePath() + "/profile.json");
FileContent mediaContent = new FileContent("application/json", filePath);
File file = mDriveService.files().create(metadata, mediaContent)
.setFields("id")
.execute();
if (file == null) {
throw new IOException("Null result when requesting file creation.");
}
return file.getId();
});
}
But when i query my google drive files with this
public Task<FileList> queryFiles() {
return Tasks.call(mExecutor, () -> mDriveService.files().list().setSpaces("drive").execute());
}
the list is empty even though the file has been successfully created...
On console.developers.google.com I have added the following scope '../auth/drive.appdata'
One last thing, I have 3 google accounts and on one of them, I can successfully read the app data file that I uploaded, all my other accounts returns empty lists but there is this one account where I did various tests about cloud file handling and I somehow successfully created a file that I can read.
I found the solution !
the problem was here
public Task<FileList> queryFiles() {
return Tasks.call(mExecutor, () -> mDriveService.files().list().setSpaces("drive").execute());
}
the solution is
public Task<FileList> queryFiles() {
return Tasks.call(mExecutor, () -> mDriveService.files().list().setSpaces("appDataFolder").execute());
}
Querying the "drive" space seemed to not get the app data folder so I changed "drive" to "appDataFolder"

Google Drive API: Downloading a file in Java

So using Google Drive's API, I am trying to download a file from my drive account. I have followed Google's quickstart guide (https://developers.google.com/drive/web/quickstart/java) and used Google's DriveQuickStart.java to initialize the Drive object.
Everything with the object works correctly (i.e acquiring all the files from my google drive account and displaying their IDs and titles); however, when I tried downloading a file through the input stream of the function Google developed, I keep getting a null exception error.
Here is the code that I am using:
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
The problem is that when the method calls file.getDownloadURL(), it returns a null value. According to the documentation, it should return a null value if the file I am trying to download is a native Google Drive file; however, the file that I am downloading is simply a jar file, so it can't be because of the file extension (I also tried it on other formats too).
Why is it returning a null value, and what can I do to resolve this issue? Thank you!
Figured it out.
For anyone else who struggled with this, the answer is really simple:
In the DriveQuickStart.java code, pay attention to this part:
/** Global instance of the scopes required by this quickstart. */
private static final List<String> SCOPES =
Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
And make sure you set it to:
/** Global instance of the scopes required by this quickstart. */
private static final List<String> SCOPES =
Arrays.asList(DriveScopes.DRIVE);
So the only reason why it didn't work was because program did not have the appropriate permission to do so.

How to access the asset in workflow process step

I am trying to write a workflow process step for the DAM update asset such that the uploaded asset will be sent to an external service that will modify the asset and then the modified asset can be sent to the Metadata extraction step. So I've added my process step to the DAM update asset like this:
And my code looks like this so far:
public void execute(WorkItem item, WorkflowSession wfsession,MetaDataMap args) throws WorkflowException {
try
{
log.info("Here2 in execute method"); //ensure that the execute method is invoked
final Map<String, Object> map = new HashMap<String, Object>();
map.put( "user.jcr.session", wfsession.getSession());
ResourceResolver rr = resolverFactory.getResourceResolver(map);
String path = item.getWorkflowData().getPayload().toString();
log.info("Here2 path: " + path);
Resource resource = rr.getResource(path);
log.info("Here2 resource: " + resource);
InputStream is = resource.adaptTo(InputStream.class);
log.info("Here2 assets IS: " + is);
}
catch (Exception e)
{
log.info("Here Error");
e.printStackTrace();
}
}
This is what I see in the logs when I upload an asset:
Here2 in execute method
Here2 path: /content/dam/photo1.JPG/jcr:content/renditions/original
Here2 asset: null
Question
My external service has an API accepting requests over HTTP. How should I send over the asset to the external service?
Once the external service modifies the asset, what should I do so that the Metadata extraction step reads the modified asset instead of the original?
In order to access your external service via HTTP, you have to write a client. CQ provides commons-httpclient bundle and you may use it to access the service. Documentation for the library can be found here. I don't know if the service expects that the file will be send using PUT or POST, but httpclient provides all these methods. All you have to do is to provide appropriate InputStream. Adapt your resource to Rendition and use getStream() method to get the InputStream.
When you'll get the modified asset from the webservice, you need to replace the original one:
// rendition = ...; // original rendition object created as above
// newInputStream = ...; // new asset received from your webservice
Asset asset = rendition.getAsset();
asset.addRendition("original", newInputStream, rendition.getMimeType());

How to store a file as blob values using the file service in google cloud

With the help of using the Google document Writing files to a blob store
I can store the file in Google Cloud in below way,
//My servlet Class
private StorageService storage = new StorageService();
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
storage.init(fileName, mime); // Calling my Java Class Here
while ((readBytes1 = is1.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
storage.storeFile(b1, readBytes1);}
writeChannel.closeFinally();
I have a storage java class to store the data in Google cloud that is below here,
//My Storage Class
public class StorageService {
public static final String BUCKET_NAME = "MyBucket";
public void init(String fileName, String mime) throws Exception {
GSFileOptionsBuilder builder = new GSFileOptionsBuilder().setAcl("public_read").setBucket(BUCKET_NAME).setKey(fileName).setMimeType(mime);
AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
boolean lock = true;
writeChannel = fileService.openWriteChannel(writableFile, lock);
bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
public void storeFile(byte[] b, int readSize) throws Exception {
bos.write(b,0,readSize);
bos.flush();
}}
From the above reference i can store the file directly in Google cloud. But i need to store those files as blob values using the File service.
Can anyone suggest me an idea.
your help will be appreciated.
Have you add appengine service account into Google API Console Project ?
If you have not done, you should prepare Google API Console Project.
Confirm your appengine service account on "Application Settings" tab on GAE Admin Console
Create new Project on Google API Console. or Open API Console Project for your exists bucket.
Make enable "Google Cloud Storage" in "Services" tab.
Add appengine service account as team on API Console "Team" tab.

Categories