customprovider.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const request = require('request')
  2. const BASE_URL = 'https://api.unsplash.com'
  3. function adaptData (res) {
  4. const data = {
  5. username: null,
  6. items: [],
  7. nextPagePath: null,
  8. }
  9. const items = res
  10. items.forEach((item) => {
  11. const isFolder = !!item.published_at
  12. data.items.push({
  13. isFolder,
  14. icon: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb,
  15. name: item.title || item.description,
  16. mimeType: isFolder ? null : 'image/jpeg',
  17. id: item.id,
  18. thumbnail: isFolder ? item.cover_photo.urls.thumb : item.urls.thumb,
  19. requestPath: item.id,
  20. modifiedDate: item.updated_at,
  21. size: null,
  22. })
  23. })
  24. return data
  25. }
  26. /**
  27. * an example of a custom provider module. It implements @uppy/companion's Provider interface
  28. */
  29. class MyCustomProvider {
  30. static version = 2
  31. constructor () {
  32. this.authProvider = 'myunsplash'
  33. }
  34. // eslint-disable-next-line class-methods-use-this
  35. async list ({ token, directory }) {
  36. const path = directory ? `/${directory}/photos` : ''
  37. const options = {
  38. url: `${BASE_URL}/collections${path}`,
  39. method: 'GET',
  40. json: true,
  41. headers: {
  42. Authorization: `Bearer ${token}`,
  43. },
  44. }
  45. return new Promise((resolve, reject) => (
  46. request(options, (err, resp, body) => {
  47. if (err) {
  48. console.log(err)
  49. reject(err)
  50. return
  51. }
  52. resolve(adaptData(body))
  53. })))
  54. }
  55. // eslint-disable-next-line class-methods-use-this
  56. async download ({ id, token }) {
  57. const options = {
  58. url: `${BASE_URL}/photos/${id}`,
  59. method: 'GET',
  60. json: true,
  61. headers: {
  62. Authorization: `Bearer ${token}`,
  63. },
  64. }
  65. const resp = await new Promise((resolve, reject) => {
  66. const req = request(options)
  67. .on('response', (response) => {
  68. // Don't allow any more data to flow yet.
  69. // https://github.com/request/request/issues/1990#issuecomment-184712275
  70. response.pause()
  71. if (resp.statusCode !== 200) {
  72. req.abort() // Or we will leak memory
  73. reject(new Error(`HTTP response ${resp.statusCode}`))
  74. return
  75. }
  76. resolve(response)
  77. })
  78. .on('error', reject)
  79. })
  80. // The returned stream will be consumed and uploaded from the current position
  81. return { stream: resp }
  82. }
  83. // eslint-disable-next-line class-methods-use-this
  84. async size ({ id, token }) {
  85. const options = {
  86. url: `${BASE_URL}/photos/${id}`,
  87. method: 'GET',
  88. json: true,
  89. headers: {
  90. Authorization: `Bearer ${token}`,
  91. },
  92. }
  93. return new Promise((resolve, reject) => (
  94. request(options, (err, resp, body) => {
  95. if (err) {
  96. console.log(err)
  97. reject(err)
  98. return
  99. }
  100. resolve(body.size)
  101. })))
  102. }
  103. }
  104. module.exports = MyCustomProvider