/
index.js
158 lines (138 loc) · 4.47 KB
/
index.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
150
151
152
153
154
155
156
157
158
const request = require('request')
const { promisify } = require('util')
const SearchProvider = require('../SearchProvider')
const { getURLMeta } = require('../../helpers/request')
const logger = require('../../logger')
const adapter = require('./adapter')
const { ProviderApiError } = require('../error')
const { requestStream } = require('../../helpers/utils')
const BASE_URL = 'https://api.unsplash.com'
function adaptData (body, currentQuery) {
const pagesCount = body.total_pages
const currentPage = Number(currentQuery.cursor || 1)
const hasNextPage = currentPage < pagesCount
const subList = adapter.getItemSubList(body) || []
return {
searchedFor: currentQuery.q,
username: null,
items: subList.map((item) => ({
isFolder: adapter.isFolder(item),
icon: adapter.getItemIcon(item),
name: adapter.getItemName(item),
mimeType: adapter.getMimeType(item),
id: adapter.getItemId(item),
thumbnail: adapter.getItemThumbnailUrl(item),
requestPath: adapter.getItemRequestPath(item),
modifiedDate: adapter.getItemModifiedDate(item),
author: adapter.getAuthor(item),
size: null,
})),
nextPageQuery: hasNextPage
? adapter.getNextPageQuery(currentQuery)
: null,
}
}
/**
* Adapter for API https://api.unsplash.com
*/
class Unsplash extends SearchProvider {
_list ({ token, query = { cursor: null, q: null } }, done) {
const reqOpts = {
url: `${BASE_URL}/search/photos`,
method: 'GET',
json: true,
qs: {
per_page: 40,
query: query.q,
},
headers: {
Authorization: `Client-ID ${token}`,
},
}
if (query.cursor) {
reqOpts.qs.page = query.cursor
}
request(reqOpts, (err, resp, body) => {
if (err || resp.statusCode !== 200) {
const error = this.error(err, resp)
logger.error(error, 'provider.unsplash.list.error')
return done(error)
}
return done(null, adaptData(body, query))
})
}
async download ({ id, token }) {
try {
const reqOpts = {
method: 'GET',
json: true,
headers: {
Authorization: `Client-ID ${token}`,
},
}
const body = await new Promise((resolve, reject) => (
request({ ...reqOpts, url: `${BASE_URL}/photos/${id}` }, (err, resp, body2) => {
if (err || resp.statusCode !== 200) {
const err2 = this.error(err, resp)
logger.error(err, 'provider.unsplash.download.error')
reject(err2)
return
}
resolve(body2)
})
))
const req = request.get(body.links.download)
const stream = await requestStream(req, async (res) => this.error(null, res))
// To attribute the author of the image, we call the `download_location`
// endpoint to increment the download count on Unsplash.
// https://help.unsplash.com/en/articles/2511258-guideline-triggering-a-download
request({ ...reqOpts, url: body.links.download_location }, (err, resp) => {
if (err || resp.statusCode !== 200) {
const err2 = this.error(err, resp)
logger.error(err2, 'provider.unsplash.download.location.error')
}
})
return stream
} catch (err) {
logger.error(err, 'provider.unsplash.download.url.error')
throw err
}
}
_size ({ id, token }, done) {
const reqOpts = {
url: `${BASE_URL}/photos/${id}`,
method: 'GET',
json: true,
headers: {
Authorization: `Client-ID ${token}`,
},
}
request(reqOpts, (err, resp, body) => {
if (err || resp.statusCode !== 200) {
const error = this.error(err, resp)
logger.error(error, 'provider.unsplash.size.error')
done(error)
return
}
getURLMeta(body.links.download)
.then(({ size }) => done(null, size))
.catch((err2) => {
logger.error(err2, 'provider.unsplash.size.error')
done(err2)
})
})
}
// eslint-disable-next-line class-methods-use-this
error (err, resp) {
if (resp) {
const fallbackMessage = `request to Unsplash returned ${resp.statusCode}`
const msg = resp.body && resp.body.errors ? `${resp.body.errors}` : fallbackMessage
return new ProviderApiError(msg, resp.statusCode)
}
return err
}
}
Unsplash.version = 2
Unsplash.prototype.list = promisify(Unsplash.prototype._list)
Unsplash.prototype.size = promisify(Unsplash.prototype._size)
module.exports = Unsplash