Skip to content

Commit

Permalink
Merge pull request #1846 from transloadit/google-drive-shared-drive
Browse files Browse the repository at this point in the history
companion,google-drive: drop deprecated usage for Google Drive's "shared drive"
  • Loading branch information
ifedapoolarewaju authored Sep 25, 2019
2 parents bb8b66d + f366f4f commit 56eed5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
8 changes: 4 additions & 4 deletions packages/@uppy/companion/src/server/provider/drive/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ exports.getUsername = (data) => {
}

exports.isFolder = (item) => {
return item.mimeType === 'application/vnd.google-apps.folder' || item.kind === 'drive#teamDrive'
return item.mimeType === 'application/vnd.google-apps.folder' || exports.isSharedDrive(item)
}

exports.getItemSize = (item) => {
return parseInt(item.size, 10)
}

exports.getItemIcon = (item) => {
if (item.kind === 'drive#teamDrive') {
if (exports.isSharedDrive(item)) {
const size = '=w16-h16-n'
const sizeParamRegex = /=[-whncsp0-9]*$/
return item.backgroundImageLink.match(sizeParamRegex)
Expand Down Expand Up @@ -67,8 +67,8 @@ exports.getItemThumbnailUrl = (item) => {
return `/drive/thumbnail/${exports.getItemRequestPath(item)}`
}

exports.isTeamDrive = (item) => {
return item.kind === 'drive#teamDrive'
exports.isSharedDrive = (item) => {
return item.kind === 'drive#drive'
}

exports.getNextPagePath = (data, currentQuery, currentPath) => {
Expand Down
38 changes: 21 additions & 17 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,8 @@ const adapter = require('./adapter')
const AuthError = require('../error')
const DRIVE_FILE_FIELDS = 'kind,id,name,mimeType,ownedByMe,permissions(role,emailAddress),size,modifiedTime,iconLink,thumbnailLink,teamDriveId'
const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FILE_FIELDS})`
const TEAM_DRIVE_FIELDS = 'teamDrives(kind,id,name,backgroundImageLink)'
// using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint
const SHARED_DRIVE_FIELDS = '*'

class Drive {
constructor (options) {
Expand All @@ -25,19 +26,19 @@ class Drive {
const directory = options.directory || 'root'
const query = options.query || {}

let teamDrivesPromise = Promise.resolve(undefined)
let sharedDrivesPromise = Promise.resolve(undefined)

const shouldListTeamDrives = directory === 'root' && !query.cursor
if (shouldListTeamDrives) {
teamDrivesPromise = new Promise((resolve) => {
const shouldListSharedDrives = directory === 'root' && !query.cursor
if (shouldListSharedDrives) {
sharedDrivesPromise = new Promise((resolve) => {
this.client
.query()
.get('teamdrives')
.qs({ fields: TEAM_DRIVE_FIELDS })
.get('drives')
.qs({ fields: SHARED_DRIVE_FIELDS })
.auth(options.token)
.request((err, resp) => {
if (err) {
logger.error(err, 'provider.drive.teamDrive.error')
logger.error(err, 'provider.drive.sharedDrive.error')
return
}
resolve(resp)
Expand Down Expand Up @@ -68,12 +69,12 @@ class Drive {
})
})

Promise.all([teamDrivesPromise, filesPromise])
Promise.all([sharedDrivesPromise, filesPromise])
.then(
([teamDrives, filesResponse]) => {
([sharedDrives, filesResponse]) => {
const returnData = this.adaptData(
filesResponse.body,
teamDrives && teamDrives.body,
sharedDrives && sharedDrives.body,
options.uppy,
directory,
query
Expand All @@ -91,7 +92,7 @@ class Drive {
return this.client
.query()
.get(`files/${id}`)
.qs({ fields: DRIVE_FILE_FIELDS, supportsTeamDrives: true })
.qs({ fields: DRIVE_FILE_FIELDS, supportsAllDrives: true })
.auth(token)
.request(done)
}
Expand All @@ -100,7 +101,7 @@ class Drive {
return this.client
.query()
.get(`files/${id}`)
.qs({ alt: 'media', supportsTeamDrives: true })
.qs({ alt: 'media', supportsAllDrives: true })
.auth(token)
.request()
.on('data', onData)
Expand Down Expand Up @@ -133,7 +134,7 @@ class Drive {
})
}

adaptData (res, teamDrivesResp, uppy, directory, query) {
adaptData (res, sharedDrivesResp, uppy, directory, query) {
const adaptItem = (item) => ({
isFolder: adapter.isFolder(item),
icon: adapter.getItemIcon(item),
Expand All @@ -145,14 +146,17 @@ class Drive {
modifiedDate: adapter.getItemModifiedDate(item),
size: adapter.getItemSize(item),
custom: {
isTeamDrive: adapter.isTeamDrive(item)
// @todo isTeamDrive is left for backward compatibility. We should remove it in the next
// major release.
isTeamDrive: adapter.isSharedDrive(item),
isSharedDrive: adapter.isSharedDrive(item)
}
})

const items = adapter.getItemSubList(res)
const teamDrives = teamDrivesResp ? teamDrivesResp.teamDrives || [] : []
const sharedDrives = sharedDrivesResp ? sharedDrivesResp.drives || [] : []

const adaptedItems = teamDrives.concat(items).map(adaptItem)
const adaptedItems = sharedDrives.concat(items).map(adaptItem)

return {
username: adapter.getUsername(res),
Expand Down
6 changes: 4 additions & 2 deletions packages/@uppy/google-drive/src/DriveProviderViews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ module.exports = class DriveProviderViews extends ProviderViews {
e.stopPropagation()
e.preventDefault()

// Team Drives aren't selectable; for all else, defer to the base ProviderView.
if (!file.custom.isTeamDrive) {
// Shared Drives aren't selectable; for all else, defer to the base ProviderView.
// @todo isTeamDrive is left for backward compatibility. We should remove it in the next
// major release.
if (!file.custom.isTeamDrive && !file.custom.isSharedDrive) {
super.toggleCheckbox(e, file)
}
}
Expand Down

0 comments on commit 56eed5a

Please sign in to comment.