googleworkspace/android-samples

how to prevent duplicate folder created?

hksfho opened this issue · 1 comments

I am trying to create folders using android google drive API using the scope

Surprisingly, the api allows me to upload multiple files with the exact same title into the same folder. I can even see these duplicate files if I browse over to my google drive in my web browser.

Is there any way I can have the API prevent duplicates in filenames? I could check for a file's existence before uploading another with the same name, but that seems to be a strange way to handle writing to a file system...

private DriveFolder getChildren(final DriveClient driveClient, final DriveResourceClient driveResourceClient, DriveFolder parent, String folderName) throws ExecutionException, InterruptedException {
        DriveFolder folder = null;
        Query query = new Query.Builder()
                .addFilter(Filters.eq(SearchableField.TITLE, folderName))
                .addFilter(Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE))
                .addFilter(Filters.eq(SearchableField.TRASHED, false))
                .build();
        MetadataBuffer children = Tasks.await(driveResourceClient.queryChildren(parent, query));
        if (children.getCount() > 0) {
            for (int i = 0; i < children.getCount(); i++) {
                Metadata metadata = children.get(i);
                if (!metadata.isTrashed() && metadata.isFolder() && metadata.getTitle().equals(folderName)) {
                    DriveFolder oldFolder = metadata.getDriveId().asDriveFolder();
                    Log.d(TAG, "Folder exist:" + folderName + "," + oldFolder.getDriveId().encodeToString());
                    folder = oldFolder;
                    break;
                }
            }
        }
        if (folder == null) {
            Log.d(TAG, "Creating folder:" + folderName);
            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                    .setTitle(folderName)
                    .setMimeType(DriveFolder.MIME_TYPE)
                    .build();
            DriveFolder newFolder = Tasks.await(driveResourceClient.createFolder(parent, metadataChangeSet));
            folder = newFolder;
        }
        return folder;
    }

This issue tracker is only meant to be used to report bugs in the sample code. For general Q&A about our APIs, we recommend Stack Overflow:

https://developers.google.com/drive/android/support#questions_and_advice

Google Drive does not require that file names are unique within a folder, and therefore doesn't provide features to help ensure that.