This is how you can use the Google Drive API with axios for each of your questions:
- Search all files and folders by date
Yes you can! you can use createdTime and modifiedTime filters (see more available query terms here)
- Find a file by size
Yes, you can, but, you will need to return the size of the file/folder and filter the resulting files by specific size, you don't need to download the file, but it could be a little inefficient since you need to fetch the files first (use query filters to limit the results).
- Find all empty folders
Yes, you can! but similar to the previous point, you need to iterate each folder and search files using the folder id, checking if files are empty.
Ensure you're filtering folders only by using mimeType filter application/vnd.google-apps.folder
- Find a file by type such as ppt, image, etc
Use mimeType i.e image/jpeg
// Search all files and folders by date
const dateFilter = new Date('January 01, 2022').toISOString();
// 1. Search all files and folders by date
const filesFilteredByDate = await axios.get('https://www.googleapis.com/drive/v3/files', {
params: {
q: `createdTime >= '${dateFilter}' or modifiedTime >= '${dateFilter}'`,
fields: 'files(id,name,modifiedTime,createdTime,mimeType,size)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
// 2. Find a file by size
const sizeInBytes = 1024;
const filesFilteredBySize = filesFilteredByDate.data.files.filter(file => Number(file.size || 0) >= sizeInBytes);
// 3. Find all empty folders
const emptyFoldersSearch = await axios.get('https://www.googleapis.com/drive/v3/files', {
params: {
q: `mimeType = 'application/vnd.google-apps.folder'`,
fields: 'files(id, name)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
const emptyFolders = [];
for await (const folder of emptyFoldersSearch.data.files) {
const childrenResponse = await axios.get('https://www.googleapis.com/drive/v3/files', {
params: {
folderId: folder.id,
spaces: 'drive',
},
headers: {
authorization: `Bearer ${googleClient.fusebit.credentials.access_token}`
}
});
if (!childrenResponse.data.files.length) {
emptyFolders.push(folder);
}
}
// 4. Find a file by type such as ppt, image, etc
const mimeType = 'image/jpeg';
const filesFilteredByType = await axios.get('https://www.googleapis.com/drive/v3/files', {
params:{
q: `mimeType:'${mimeType}'`,
fields: 'files(id,name,mimeType,size)',
spaces: 'drive',
},
headers: {
authorization: `Bearer ${access_token}`
}
});
console.log(`Found ${filesFilteredByDate.data.files.length} files/folders created or modified at ${dateFilter}`);
console.log(`Files larger than ${sizeInBytes} bytes:`, filesFilteredBySize);
console.log(`Found ${emptyFolders.length} empty folders`);
console.log(`Found ${filesFilteredByType.data.files.length} images of type ${mimeType}'`);