forked from transloadit/uppy
/
adapter.js
149 lines (119 loc) · 4.54 KB
/
adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
exports.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
}
exports.isFolder = (item) => {
return item.mimeType === 'application/vnd.google-apps.folder' || exports.isSharedDrive(item)
}
exports.isShortcut = (mimeType) => {
return mimeType === 'application/vnd.google-apps.shortcut'
}
exports.getItemSize = (item) => {
return parseInt(item.size, 10)
}
exports.getItemIcon = (item) => {
if (exports.isSharedDrive(item)) {
const size = '=w16-h16-n'
const sizeParamRegex = /=[-whncsp0-9]*$/
return item.backgroundImageLink.match(sizeParamRegex)
? item.backgroundImageLink.replace(sizeParamRegex, size)
: `${item.backgroundImageLink}${size}`
}
if (item.thumbnailLink && !item.mimeType.startsWith('application/vnd.google')) {
const smallerThumbnailLink = item.thumbnailLink.replace('s220', 's40')
return smallerThumbnailLink
}
return item.iconLink
}
exports.getItemSubList = (item) => {
const allowedGSuiteTypes = [
'application/vnd.google-apps.document',
'application/vnd.google-apps.drawing',
'application/vnd.google-apps.script',
'application/vnd.google-apps.spreadsheet',
'application/vnd.google-apps.presentation',
'application/vnd.google-apps.shortcut',
]
return item.files.filter((i) => {
return exports.isFolder(i) || !exports.isGsuiteFile(i.mimeType) || allowedGSuiteTypes.includes(i.mimeType)
})
}
exports.getItemName = (item) => {
const extensionMaps = {
'application/vnd.google-apps.document': '.docx',
'application/vnd.google-apps.drawing': '.png',
'application/vnd.google-apps.script': '.json',
'application/vnd.google-apps.spreadsheet': '.xlsx',
'application/vnd.google-apps.presentation': '.ppt',
}
const extension = extensionMaps[item.mimeType]
if (extension && item.name && !item.name.endsWith(extension)) {
return item.name + extension
}
return item.name ? item.name : '/'
}
function getMimeType (mimeType) {
if (exports.isGsuiteFile(mimeType)) {
return exports.getGsuiteExportType(mimeType)
}
return mimeType
}
exports.getMimeType = (item) => {
if (exports.isShortcut(item.mimeType)) {
return getMimeType(item.shortcutDetails.targetMimeType)
}
return getMimeType(item.mimeType)
}
exports.getItemId = (item) => {
return item.id
}
exports.getItemRequestPath = (item) => {
return item.id
}
exports.getItemModifiedDate = (item) => {
return item.modifiedTime
}
exports.getItemThumbnailUrl = (item) => {
return item.thumbnailLink
}
exports.isSharedDrive = (item) => {
return item.kind === 'drive#drive'
}
exports.getNextPagePath = (data, currentQuery, currentPath) => {
if (!data.nextPageToken) {
return null
}
const query = { ...currentQuery, cursor: data.nextPageToken }
return `${currentPath}?${querystring.stringify(query)}`
}
exports.isGsuiteFile = (mimeType) => {
return mimeType && mimeType.startsWith('application/vnd.google')
}
exports.getGsuiteExportType = (mimeType) => {
const typeMaps = {
'application/vnd.google-apps.document': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.google-apps.drawing': 'image/png',
'application/vnd.google-apps.script': 'application/vnd.google-apps.script+json',
'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.google-apps.presentation': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
}
return typeMaps[mimeType] || 'application/pdf'
}
exports.getImageHeight = (item) => item.imageMediaMetadata && item.imageMediaMetadata.height
exports.getImageWidth = (item) => item.imageMediaMetadata && item.imageMediaMetadata.width
exports.getImageRotation = (item) => item.imageMediaMetadata && item.imageMediaMetadata.rotation
exports.getImageDate = (item) => item.imageMediaMetadata && item.imageMediaMetadata.date
exports.getVideoHeight = (item) => item.videoMediaMetadata && item.videoMediaMetadata.height
exports.getVideoWidth = (item) => item.videoMediaMetadata && item.videoMediaMetadata.width
exports.getVideoDurationMillis = (item) => item.videoMediaMetadata && item.videoMediaMetadata.durationMillis