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 all commits
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
1 change: 1 addition & 0 deletions packages/@uppy/box/src/Box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Box extends UIPlugin {
install () {
this.view = new ProviderViews(this, {
provider: this.provider,
loadAllFiles: true,
})

const { target } = this.opts
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion-client/src/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export default class Provider extends RequestClient {
}
}

list (directory) {
return this.get(`${this.id}/list/${directory || ''}`)
list (directory, options) {
return this.get(`${this.id}/list/${directory || ''}`, options)
}

async logout () {
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/src/server/provider/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ async function getUserInfo ({ token }) {

async function list ({ directory, query, token }) {
const rootFolderID = '0'
return getClient({ token }).get(`folders/${directory || rootFolderID}/items`, { searchParams: { fields: BOX_FILES_FIELDS, offset: query.cursor }, responseType: 'json' }).json()
// https://developer.box.com/reference/resources/items/
return getClient({ token }).get(`folders/${directory || rootFolderID}/items`, { searchParams: { fields: BOX_FILES_FIELDS, offset: query.cursor, limit: 1000 }, responseType: 'json' }).json()
}

/**
Expand Down
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
27 changes: 12 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 @@ -86,18 +86,9 @@ 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
// We can only do a page size of 1000 because we do not request permissions in DRIVE_FILES_FIELDS.
// Otherwise we are limited to 100. Instead we get the user info from `this.user()`
pageSize: 1000,
orderBy: 'folder,name',
includeItemsFromAllDrives: true,
supportsAllDrives: true,
Expand All @@ -106,15 +97,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()])

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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class OneDrive extends Provider {
async list ({ directory, query, token }) {
return this.#withErrorHandling('provider.onedrive.list.error', async () => {
const path = directory ? `items/${directory}` : 'root'
const qs = { $expand: 'thumbnails' }
// https://learn.microsoft.com/en-us/graph/query-parameters?tabs=http#top-parameter
const qs = { $expand: 'thumbnails', $top: 999 }
if (query.cursor) {
qs.$skiptoken = query.cursor
}
Expand Down
8 changes: 5 additions & 3 deletions packages/@uppy/companion/test/__tests__/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('list provider files', () => {
nock('https://api.box.com').get('/2.0/users/me').reply(200, {
login: defaults.USERNAME,
})
nock('https://api.box.com').get('/2.0/folders/0/items?fields=id%2Cmodified_at%2Cname%2Cpermissions%2Csize%2Ctype').reply(200, {
nock('https://api.box.com').get('/2.0/folders/0/items?fields=id%2Cmodified_at%2Cname%2Cpermissions%2Csize%2Ctype&limit=1000').reply(200, {
entries: [
{
type: 'file',
Expand All @@ -157,7 +157,7 @@ describe('list provider files', () => {
kind: 'drive#driveList', drives: [],
})

nock('https://www.googleapis.com').get('/drive/v3/files?fields=kind%2CnextPageToken%2CincompleteSearch%2Cfiles%28kind%2Cid%2CimageMediaMetadata%2Cname%2CmimeType%2CownedByMe%2Cpermissions%28role%2CemailAddress%29%2Csize%2CmodifiedTime%2CiconLink%2CthumbnailLink%2CteamDriveId%2CvideoMediaMetadata%2CshortcutDetails%28targetId%2CtargetMimeType%29%29&q=%28%27root%27+in+parents%29+and+trashed%3Dfalse&orderBy=folder%2Cname&includeItemsFromAllDrives=true&supportsAllDrives=true').reply(200, {
nock('https://www.googleapis.com').get('/drive/v3/files?fields=kind%2CnextPageToken%2CincompleteSearch%2Cfiles%28kind%2Cid%2CimageMediaMetadata%2Cname%2CmimeType%2CownedByMe%2Csize%2CmodifiedTime%2CiconLink%2CthumbnailLink%2CteamDriveId%2CvideoMediaMetadata%2CshortcutDetails%28targetId%2CtargetMimeType%29%29&q=%28%27root%27+in+parents%29+and+trashed%3Dfalse&pageSize=1000&orderBy=folder%2Cname&includeItemsFromAllDrives=true&supportsAllDrives=true').reply(200, {
kind: 'drive#fileList',
nextPageToken: defaults.NEXT_PAGE_TOKEN,
files: [
Expand All @@ -176,6 +176,8 @@ describe('list provider files', () => {
],
})

nock('https://www.googleapis.com').get((uri) => uri.includes('about')).reply(200, { user: { emailAddress: 'john.doe@transloadit.com' } })

await runTest('drive')
})

Expand Down Expand Up @@ -231,7 +233,7 @@ describe('list provider files', () => {
userPrincipalName: defaults.USERNAME,
mail: defaults.USERNAME,
})
nock('https://graph.microsoft.com').get('/v1.0/me/drive/root/children?%24expand=thumbnails').reply(200, {
nock('https://graph.microsoft.com').get('/v1.0/me/drive/root/children?%24expand=thumbnails&%24top=999').reply(200, {
value: [
{
createdDateTime: '2020-01-31T15:40:26.197Z',
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/test/fixtures/drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const defaults = require('./constants')
module.exports.expects = {}

module.exports.nockGoogleDownloadFile = ({ times = 1 } = {}) => {
nock('https://www.googleapis.com').get(`/drive/v3/files/${defaults.ITEM_ID}?fields=kind%2Cid%2CimageMediaMetadata%2Cname%2CmimeType%2CownedByMe%2Cpermissions%28role%2CemailAddress%29%2Csize%2CmodifiedTime%2CiconLink%2CthumbnailLink%2CteamDriveId%2CvideoMediaMetadata%2CshortcutDetails%28targetId%2CtargetMimeType%29&supportsAllDrives=true`).times(times).reply(200, {
nock('https://www.googleapis.com').get(`/drive/v3/files/${defaults.ITEM_ID}?fields=kind%2Cid%2CimageMediaMetadata%2Cname%2CmimeType%2CownedByMe%2Csize%2CmodifiedTime%2CiconLink%2CthumbnailLink%2CteamDriveId%2CvideoMediaMetadata%2CshortcutDetails%28targetId%2CtargetMimeType%29&supportsAllDrives=true`).times(times).reply(200, {
kind: 'drive#file',
id: defaults.ITEM_ID,
name: 'MY DUMMY FILE NAME.mp4',
Expand All @@ -17,4 +17,5 @@ module.exports.nockGoogleDownloadFile = ({ times = 1 } = {}) => {
size: '758051',
})
nock('https://www.googleapis.com').get(`/drive/v3/files/${defaults.ITEM_ID}?alt=media&supportsAllDrives=true`).reply(200, {})
nock('https://www.googleapis.com').get((uri) => uri.includes('about')).reply(200, { user: { emailAddress: 'john.doe@transloadit.com' } })
}
1 change: 1 addition & 0 deletions packages/@uppy/core/src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
filter: 'Filter',
resetFilter: 'Reset filter',
loading: 'Loading...',
loadedXFiles: 'Loaded %{numFiles} files',
authenticateWithTitle:
'Please authenticate with %{pluginName} to select files',
authenticateWith: 'Connect to %{pluginName}',
Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/dashboard/src/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export default class Dashboard extends UIPlugin {
}

this.setPluginState(update)

this.uppy.emit('dashboard:close-panel', state.activePickerPanel.id)
}

showPanel = (id) => {
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/dropbox/src/Dropbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default class Dropbox extends UIPlugin {
install () {
this.view = new ProviderViews(this, {
provider: this.provider,
loadAllFiles: true,
})

const { target } = this.opts
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/google-drive/src/GoogleDrive.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default class GoogleDrive extends UIPlugin {
install () {
this.view = new DriveProviderViews(this, {
provider: this.provider,
loadAllFiles: true,
})

const { target } = this.opts
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/onedrive/src/OneDrive.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class OneDrive extends UIPlugin {
install () {
this.view = new ProviderViews(this, {
provider: this.provider,
loadAllFiles: true,
})

const { target } = this.opts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default (props) => {
return <VideoIcon />
default: {
const { alt } = props
return <img src={itemIconString} alt={alt} />
return <img src={itemIconString} alt={alt} loading="lazy" />
}
}
}
54 changes: 42 additions & 12 deletions packages/@uppy/provider-views/src/ProviderView/ProviderView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class ProviderView extends View {
showTitles: true,
showFilter: true,
showBreadcrumbs: true,
loadAllFiles: false,
}

// merge default options with the ones set by user
Expand Down Expand Up @@ -105,30 +106,59 @@ export default class ProviderView extends View {
* @returns {Promise} Folders/files in folder
*/
async getFolder (id, name) {
const controller = new AbortController()
const cancelRequest = () => {
controller.abort()
this.clearSelection()
}
const getNewBreadcrumbsDirectories = () => {
const state = this.plugin.getPluginState()
const index = state.directories.findIndex((dir) => id === dir.id)

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

this.plugin.uppy.on('dashboard:close-panel', cancelRequest)
this.plugin.uppy.on('cancel-all', cancelRequest)
this.setLoading(true)

try {
const res = await this.provider.list(id)
const folders = []
const files = []
let updatedDirectories
this.nextPagePath = id

const state = this.plugin.getPluginState()
const index = state.directories.findIndex((dir) => id === dir.id)
do {
const res = await this.provider.list(this.nextPagePath, { signal: controller.signal })

if (index !== -1) {
updatedDirectories = state.directories.slice(0, index + 1)
} else {
updatedDirectories = state.directories.concat([{ id, title: name }])
}
for (const f of res.items) {
if (f.isFolder) folders.push(f)
else files.push(f)
}

this.username = res.username || this.username
this.#updateFilesAndFolders(res, files, folders)
this.plugin.setPluginState({ directories: updatedDirectories, filterInput: '' })
this.nextPagePath = res.nextPagePath
if (res.username) this.username = res.username
this.setLoading(this.plugin.uppy.i18n('loadedXFiles', { numFiles: files.length + folders.length }))
} while (
this.nextPagePath && this.opts.loadAllFiles
)

const directories = getNewBreadcrumbsDirectories(this.nextPagePath)

this.plugin.setPluginState({ files, folders, directories, filterInput: '' })
this.lastCheckbox = undefined
} catch (err) {
if (err.cause?.name === 'AbortError') {
// Expected, user clicked “cancel”
return
}
this.handleError(err)
} finally {
this.setLoading(false)
this.plugin.uppy.off('dashboard:close-panel', cancelRequest)
this.plugin.uppy.off('cancel-all', cancelRequest)
}
}

Expand Down