Skip to content

Commit

Permalink
fix: fix drive stats
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentuso committed Jun 23, 2021
1 parent 1a7aac5 commit 42df5a2
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions packages/@uppy/companion/src/server/provider/drive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FI
// using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint
const SHARED_DRIVE_FIELDS = '*'

const VIRTUAL_SHARED_DIR = 'shared-with-me'

/**
* Adapter for API https://developers.google.com/drive/api/v3/
*/
Expand All @@ -35,6 +37,9 @@ class Drive extends Provider {

let sharedDrivesPromise = Promise.resolve(undefined)

const isRoot = directory === 'root'
const isVirtualSharedDirRoot = directory === VIRTUAL_SHARED_DIR

const shouldListSharedDrives = directory === 'root' && !query.cursor
if (shouldListSharedDrives) {
sharedDrivesPromise = new Promise((resolve) => {
Expand All @@ -53,10 +58,14 @@ class Drive extends Provider {
})
}

const q = isVirtualSharedDirRoot
? 'sharedWithMe and trashed=false'
: `('${directory}' in parents) and trashed=false`

const where = {
fields: DRIVE_FILES_FIELDS,
pageToken: query.cursor,
q: `'${directory}' in parents and trashed=false`,
q,
includeItemsFromAllDrives: true,
supportsAllDrives: true
}
Expand All @@ -83,7 +92,8 @@ class Drive extends Provider {
filesResponse.body,
sharedDrives && sharedDrives.body,
directory,
query
query,
isRoot && !query.cursor
)
done(null, returnData)
},
Expand All @@ -102,18 +112,18 @@ class Drive extends Provider {
.get(`files/${id}`)
.qs({ fields: DRIVE_FILE_FIELDS, supportsAllDrives: true })
.auth(token)
.request((err, resp) => {
.request((err, resp, body) => {
if (err) {
reject(err)
} else {
resolve(resp)
resolve({ resp, body })
}
})
}),
retryDelays: [1000, 5000, 10000, 10000]
})
.then(
(resp) => done(null, resp),
({ resp, body }) => done(null, resp, body),
(err) => done(err)
)
}
Expand Down Expand Up @@ -227,7 +237,7 @@ class Drive extends Provider {
})
}

adaptData (res, sharedDrivesResp, directory, query) {
adaptData (res, sharedDrivesResp, directory, query, showSharedWithMe) {
const adaptItem = (item) => ({
isFolder: adapter.isFolder(item),
icon: adapter.getItemIcon(item),
Expand All @@ -249,7 +259,21 @@ class Drive extends Provider {
const items = adapter.getItemSubList(res)
const sharedDrives = sharedDrivesResp ? sharedDrivesResp.drives || [] : []

const adaptedItems = sharedDrives.concat(items).map(adaptItem)
// “Shared with me” is a list of shared documents,
// not the same as sharedDrives
const virtualItem = showSharedWithMe && ({
isFolder: true,
icon: 'folder',
name: 'Shared with me',
mimeType: 'application/vnd.google-apps.folder',
id: VIRTUAL_SHARED_DIR,
requestPath: VIRTUAL_SHARED_DIR
})

const adaptedItems = [
...(virtualItem ? [virtualItem] : []), // shared folder first
...([...sharedDrives, ...items].map(adaptItem))
]

return {
username: adapter.getUsername(res),
Expand Down

0 comments on commit 42df5a2

Please sign in to comment.