Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Google Drive / OneDrive lists 5-10x faster & always load all files #4513

Merged
merged 20 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions packages/@uppy/companion/src/server/provider/drive/adapter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
const querystring = require('node:querystring')

// @todo use the "about" endpoint to get the username instead
// see: https://developers.google.com/drive/api/v2/reference/about/get
const getUsername = (data) => {
for (const item of data.files) {
if (item.ownedByMe && item.permissions) {
for (const permission of item.permissions) {
if (permission.role === 'owner') {
return permission.emailAddress
}
}
}
}
return undefined
return data.user.emailAddress
}

exports.isGsuiteFile = (mimeType) => {
Expand Down Expand Up @@ -151,7 +140,7 @@ const getVideoDurationMillis = (item) => item.videoMediaMetadata && item.videoMe
// Hopefully this name will not be used by Google
exports.VIRTUAL_SHARED_DIR = 'shared-with-me'

exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showSharedWithMe) => {
exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showSharedWithMe, about) => {
const adaptItem = (item) => ({
isFolder: isFolder(item),
icon: getItemIcon(item),
Expand Down Expand Up @@ -194,7 +183,7 @@ exports.adaptData = (listFilesResp, sharedDrivesResp, directory, query, showShar
]

return {
username: getUsername(listFilesResp),
username: getUsername(about),
items: adaptedItems,
nextPagePath: getNextPagePath(listFilesResp, query, directory),
}
Expand Down
25 changes: 10 additions & 15 deletions packages/@uppy/companion/src/server/provider/drive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { VIRTUAL_SHARED_DIR, adaptData, isShortcut, isGsuiteFile, getGsuiteExport
const { withProviderErrorHandling } = require('../providerErrors')
const { prepareStream } = require('../../helpers/utils')

const DRIVE_FILE_FIELDS = 'kind,id,imageMediaMetadata,name,mimeType,ownedByMe,permissions(role,emailAddress),size,modifiedTime,iconLink,thumbnailLink,teamDriveId,videoMediaMetadata,shortcutDetails(targetId,targetMimeType)'
const DRIVE_FILE_FIELDS = 'kind,id,imageMediaMetadata,name,mimeType,ownedByMe,size,modifiedTime,iconLink,thumbnailLink,teamDriveId,videoMediaMetadata,shortcutDetails(targetId,targetMimeType)'
const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FILE_FIELDS})`
// using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint
const SHARED_DRIVE_FIELDS = '*'
Expand Down Expand Up @@ -82,18 +82,7 @@ class Drive extends Provider {
fields: DRIVE_FILES_FIELDS,
pageToken: query.cursor,
q,
// pageSize: The maximum number of files to return per page.
// Partial or empty result pages are possible even before the end of the files list has been reached.
// Acceptable values are 1 to 1000, inclusive. (Default: 100)
//
// @TODO:
// SAD WARNING: doesn’t work if you have multiple `fields`, defaults to 100 anyway.
// Works if we remove `permissions(role,emailAddress)`, which we use to set the email address
// of logged in user in the Provider View header on the frontend.
// See https://stackoverflow.com/questions/42592125/list-request-page-size-being-ignored
//
// pageSize: 1000,
// pageSize: 10, // can be used for testing pagination if you don't have many files
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
pageSize: 1000,
orderBy: 'folder,name',
includeItemsFromAllDrives: true,
supportsAllDrives: true,
Expand All @@ -102,15 +91,21 @@ class Drive extends Provider {
return client.get('files', { searchParams, responseType: 'json' }).json()
}

const [sharedDrives, filesResponse] = await Promise.all([fetchSharedDrives(), fetchFiles()])
// console.log({ directory, sharedDrives, filesResponse })
async function fetchAbout () {
const searchParams = { fields: 'user' }

return client.get('about', { searchParams, responseType: 'json' }).json()
}

const [sharedDrives, filesResponse, about] = await Promise.all([fetchSharedDrives(), fetchFiles(), fetchAbout()])
Murderlon marked this conversation as resolved.
Show resolved Hide resolved

return adaptData(
filesResponse,
sharedDrives,
directory,
query,
isRoot && !query.cursor, // we can only show it on the first page request, or else we will have duplicates of it
about,
)
})
}
Expand Down
23 changes: 18 additions & 5 deletions packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,36 @@ export default class ProviderView extends View {
async getFolder (id, name) {
this.setLoading(true)
try {
const res = await this.provider.list(id)
const folders = []
const files = []
let path = this.nextPagePath || id
Murderlon marked this conversation as resolved.
Show resolved Hide resolved

while (path) {
const res = await this.provider.list(path)

for (const f of res.items) {
if (f.isFolder) folders.push(f)
else files.push(f)
}

path = res.nextPagePath
this.username ??= res.username
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
this.setLoading(this.plugin.uppy.i18n('addedNumFiles', { numFiles: files.length + folders.length }))
}

// TODO: what is `directories` used for in state?
let updatedDirectories

const state = this.plugin.getPluginState()
const index = state.directories.findIndex((dir) => id === dir.id)
const index = state.directories.findIndex((dir) => path === dir.id)

if (index !== -1) {
updatedDirectories = state.directories.slice(0, index + 1)
} else {
updatedDirectories = state.directories.concat([{ id, title: name }])
}

this.username = res.username || this.username
this.#updateFilesAndFolders(res, files, folders)
this.plugin.setPluginState({ directories: updatedDirectories, filterInput: '' })
this.plugin.setPluginState({ files, folders, directories: updatedDirectories, filterInput: '' })
this.lastCheckbox = undefined
} catch (err) {
this.handleError(err)
Expand Down