Google Drive API to fetch only images

0

I recently started to study the Google Drive API. I'm able to list all Drive files and have access to all of their attributes. However, I was asked to search only for image files.

What did I do? I played the search code inside a JavaScript:

if(file.extension === 'jpg' || file.extension === 'png')

But there you go. My search is parsing all the drive files and listing only the .jpg and .png files, making my search very poorly optimized. I wonder if anyone could help me. I want my search to scour the Drive and return only the img extension files.

Follow the API link link . Thanks in advance.

    
asked by anonymous 20.03.2018 / 12:36

1 answer

0

As I do not know how your code is, I'll leave a snippet of how you can do it.

As I said in my comment, just add the q property and use one of the operators below followed by the value you want.

Operators:

  • contains The content of one string is present in the other.
  • = The contents of one string or boolean are the same as the other.
  • != The content of one string or boolean is not the same as the other.
  • < The value is less than another.
  • <= The value is less than or equal to another.
  • > The value is greater than the other.
  • >= The value is greater than or equal to another.
  • in An element within a collection.
  • and Returns items that match both clauses.
  • or Returns items that match any clause.
  • not Deny a search clause.
  • has A collection contains an element that matches the parameters.

How to do:

"q": "<campo>" <operador> "<valor"> ["<campo>" <operador> "<valor"> ],

Example:

function listFiles() {
    gapi.client.drive.files.list({
        'pageSize': 10,
        'q': "mimeType contains 'image/jpeg' or mimeType contains 'image/png'",
        'fields': "nextPageToken, files(id, name)"
    }).then(function(response) {
        console.log('Files:');
        let files = response.result.files;

        if (files && files.length > 0) {
            for (let i = 0; i < files.length; i++) {
                let file = files[i];

                console.log(file.name + ' (' + file.id + ')');
            }
        } else {
            console.log('No files found.');
        }
    });
}
    
22.03.2018 / 19:43