123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- const request = require('request')
- 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 {
- constructor (options) {
- this.authProvider = 'myunsplash'
- }
- list ({ token, directory }, done) {
- const path = directory ? `/${directory}/photos` : ''
- const options = {
- url: `${BASE_URL}/collections${path}`,
- method: 'GET',
- json: true,
- headers: {
- Authorization: `Bearer ${token}`,
- },
- }
- request(options, (err, resp, body) => {
- if (err) {
- console.log(err)
- done(err)
- return
- }
- done(null, adaptData(body))
- })
- }
- download ({ id, token }, onData) {
- const options = {
- url: `${BASE_URL}/photos/${id}`,
- method: 'GET',
- json: true,
- headers: {
- Authorization: `Bearer ${token}`,
- },
- }
- request(options, (err, resp, body) => {
- if (err) {
- console.log(err)
- return
- }
- const url = body.links.download
- request.get(url)
- .on('data', (chunk) => onData(null, chunk))
- .on('end', () => onData(null, null))
- .on('error', (err) => console.log(err))
- })
- }
- size ({ id, token }, done) {
- const options = {
- url: `${BASE_URL}/photos/${id}`,
- method: 'GET',
- json: true,
- headers: {
- Authorization: `Bearer ${token}`,
- },
- }
- request(options, (err, resp, body) => {
- if (err) {
- console.log(err)
- done(err)
- return
- }
- done(null, body.width * body.height)
- })
- }
- }
- module.exports = MyCustomProvider
|