-
Notifications
You must be signed in to change notification settings - Fork 2k
/
CustomProvider.cjs
87 lines (73 loc) · 2.11 KB
/
CustomProvider.cjs
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
const { Readable } = require('node:stream')
const BASE_URL = 'https://api.unsplash.com'
function adaptData (res) {
const data = {
username: null,
items: [],
nextPagePath: null,
}
const items = res
items.forEach((item) => {
const isFolder = !!item.published_at
data.items.push({
isFolder,
icon: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb,
name: item.title || item.description,
mimeType: isFolder ? null : 'image/jpeg',
id: item.id,
thumbnail: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb,
requestPath: item.id,
modifiedDate: item.updated_at,
size: null,
})
})
return data
}
/**
* an example of a custom provider module. It implements @uppy/companion's Provider interface
*/
class MyCustomProvider {
static version = 2
static get oauthProvider () {
return 'myunsplash'
}
// eslint-disable-next-line class-methods-use-this
async list ({ token, directory }) {
const path = directory ? `/${directory}/photos` : ''
const resp = await fetch(`${BASE_URL}/collections${path}`, {
headers:{
Authorization: `Bearer ${token}`,
},
})
if (!resp.ok) {
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`)
}
return adaptData(await resp.json())
}
// eslint-disable-next-line class-methods-use-this
async download ({ id, token }) {
const resp = await fetch(`${BASE_URL}/photos/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
if (!resp.ok) {
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`)
}
return { stream: Readable.fromWeb(resp.body) }
}
// eslint-disable-next-line class-methods-use-this
async size ({ id, token }) {
const resp = await fetch(`${BASE_URL}/photos/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
if (!resp.ok) {
throw new Error(`Errornous HTTP response (${resp.status} ${resp.statusText})`)
}
const { size } = await resp.json()
return size
}
}
module.exports = MyCustomProvider