{id}.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const path = require('path')
  20. const debug = require('debug')('joystream:colossus:api:asset')
  21. const utilRanges = require('@joystream/storage-utils/ranges')
  22. const filter = require('@joystream/storage-node-backend/filter')
  23. function errorHandler(response, err, code) {
  24. debug(err)
  25. response.status(err.code || code || 500).send({ message: err.toString() })
  26. }
  27. module.exports = function(storage, runtime) {
  28. const doc = {
  29. // parameters for all operations in this path
  30. parameters: [
  31. {
  32. name: 'id',
  33. in: 'path',
  34. required: true,
  35. description: 'Joystream Content ID',
  36. schema: {
  37. type: 'string',
  38. },
  39. },
  40. ],
  41. // Head: report that ranges are OK
  42. async head(req, res) {
  43. const id = req.params.id
  44. // Open file
  45. try {
  46. const size = await storage.size(id)
  47. const stream = await storage.open(id, 'r')
  48. const type = stream.fileInfo.mimeType
  49. // Close the stream; we don't need to fetch the file (if we haven't
  50. // already). Then return result.
  51. stream.destroy()
  52. res.status(200)
  53. res.contentType(type)
  54. res.header('Content-Disposition', 'inline')
  55. res.header('Content-Transfer-Encoding', 'binary')
  56. res.header('Accept-Ranges', 'bytes')
  57. if (size > 0) {
  58. res.header('Content-Length', size)
  59. }
  60. res.send()
  61. } catch (err) {
  62. errorHandler(res, err, err.code)
  63. }
  64. },
  65. // Put for uploads
  66. async put(req, res) {
  67. const id = req.params.id // content id
  68. // First check if we're the liaison for the name, otherwise we can bail
  69. // out already.
  70. const roleAddress = runtime.identities.key.address
  71. const providerId = runtime.storageProviderId
  72. let dataObject
  73. try {
  74. debug('calling checkLiaisonForDataObject')
  75. dataObject = await runtime.assets.checkLiaisonForDataObject(providerId, id)
  76. debug('called checkLiaisonForDataObject')
  77. } catch (err) {
  78. errorHandler(res, err, 403)
  79. return
  80. }
  81. // We'll open a write stream to the backend, but reserve the right to
  82. // abort upload if the filters don't smell right.
  83. let stream
  84. try {
  85. stream = await storage.open(id, 'w')
  86. // We don't know whether the filtering occurs before or after the
  87. // stream was finished, and can only commit if both passed.
  88. let finished = false
  89. let accepted = false
  90. const possiblyCommit = () => {
  91. if (finished && accepted) {
  92. debug('Stream is finished and passed filters; committing.')
  93. stream.commit()
  94. }
  95. }
  96. stream.on('fileInfo', async info => {
  97. try {
  98. debug('Detected file info:', info)
  99. // Filter
  100. const filterResult = filter({}, req.headers, info.mimeType)
  101. if (200 !== filterResult.code) {
  102. debug('Rejecting content', filterResult.message)
  103. stream.end()
  104. res.status(filterResult.code).send({ message: filterResult.message })
  105. // Reject the content
  106. await runtime.assets.rejectContent(roleAddress, providerId, id)
  107. return
  108. }
  109. debug('Content accepted.')
  110. accepted = true
  111. // We may have to commit the stream.
  112. possiblyCommit()
  113. } catch (err) {
  114. errorHandler(res, err)
  115. }
  116. })
  117. stream.on('finish', () => {
  118. try {
  119. finished = true
  120. possiblyCommit()
  121. } catch (err) {
  122. errorHandler(res, err)
  123. }
  124. })
  125. stream.on('committed', async hash => {
  126. console.log('commited', dataObject)
  127. try {
  128. if (hash !== dataObject.ipfs_content_id.toString()) {
  129. debug('Rejecting content. IPFS hash does not match value in objectId')
  130. await runtime.assets.rejectContent(roleAddress, providerId, id)
  131. res.status(400).send({ message: "Uploaded content doesn't match IPFS hash" })
  132. return
  133. }
  134. debug('accepting Content')
  135. await runtime.assets.acceptContent(roleAddress, providerId, id)
  136. debug('creating storage relationship for newly uploaded content')
  137. // Create storage relationship and flip it to ready.
  138. const dosrId = await runtime.assets.createAndReturnStorageRelationship(roleAddress, providerId, id)
  139. debug('toggling storage relationship for newly uploaded content')
  140. await runtime.assets.toggleStorageRelationshipReady(roleAddress, providerId, dosrId, true)
  141. debug('Sending OK response.')
  142. res.status(200).send({ message: 'Asset uploaded.' })
  143. } catch (err) {
  144. debug(`${err.message}`)
  145. errorHandler(res, err)
  146. }
  147. })
  148. stream.on('error', err => errorHandler(res, err))
  149. req.pipe(stream)
  150. } catch (err) {
  151. errorHandler(res, err)
  152. return
  153. }
  154. },
  155. // Get content
  156. async get(req, res) {
  157. const id = req.params.id
  158. const download = req.query.download
  159. // Parse range header
  160. let ranges
  161. if (!download) {
  162. try {
  163. const rangeHeader = req.headers.range
  164. ranges = utilRanges.parse(rangeHeader)
  165. } catch (err) {
  166. // Do nothing; it's ok to ignore malformed ranges and respond with the
  167. // full content according to https://www.rfc-editor.org/rfc/rfc7233.txt
  168. }
  169. if (ranges && ranges.unit !== 'bytes') {
  170. // Ignore ranges that are not byte units.
  171. ranges = undefined
  172. }
  173. }
  174. debug('Requested range(s) is/are', ranges)
  175. // Open file
  176. try {
  177. const size = await storage.size(id)
  178. const stream = await storage.open(id, 'r')
  179. // Add a file extension to download requests if necessary. If the file
  180. // already contains an extension, don't add one.
  181. let sendName = id
  182. const type = stream.fileInfo.mimeType
  183. if (download) {
  184. let ext = path.extname(sendName)
  185. if (!ext) {
  186. ext = stream.fileInfo.ext
  187. if (ext) {
  188. sendName = `${sendName}.${ext}`
  189. }
  190. }
  191. }
  192. const opts = {
  193. name: sendName,
  194. type,
  195. size,
  196. ranges,
  197. download,
  198. }
  199. utilRanges.send(res, stream, opts)
  200. } catch (err) {
  201. errorHandler(res, err, err.code)
  202. }
  203. },
  204. }
  205. // OpenAPI specs
  206. doc.get.apiDoc = {
  207. description: 'Download an asset.',
  208. operationId: 'assetData',
  209. tags: ['asset', 'data'],
  210. parameters: [
  211. {
  212. name: 'download',
  213. in: 'query',
  214. description: 'Download instead of streaming inline.',
  215. required: false,
  216. allowEmptyValue: true,
  217. schema: {
  218. type: 'boolean',
  219. default: false,
  220. },
  221. },
  222. ],
  223. responses: {
  224. 200: {
  225. description: 'Asset download.',
  226. content: {
  227. default: {
  228. schema: {
  229. type: 'string',
  230. format: 'binary',
  231. },
  232. },
  233. },
  234. },
  235. default: {
  236. description: 'Unexpected error',
  237. content: {
  238. 'application/json': {
  239. schema: {
  240. $ref: '#/components/schemas/Error',
  241. },
  242. },
  243. },
  244. },
  245. },
  246. }
  247. doc.put.apiDoc = {
  248. description: 'Asset upload.',
  249. operationId: 'assetUpload',
  250. tags: ['asset', 'data'],
  251. requestBody: {
  252. content: {
  253. '*/*': {
  254. schema: {
  255. type: 'string',
  256. format: 'binary',
  257. },
  258. },
  259. },
  260. },
  261. responses: {
  262. 200: {
  263. description: 'Asset upload.',
  264. content: {
  265. 'application/json': {
  266. schema: {
  267. type: 'object',
  268. required: ['message'],
  269. properties: {
  270. message: {
  271. type: 'string',
  272. },
  273. },
  274. },
  275. },
  276. },
  277. },
  278. default: {
  279. description: 'Unexpected error',
  280. content: {
  281. 'application/json': {
  282. schema: {
  283. $ref: '#/components/schemas/Error',
  284. },
  285. },
  286. },
  287. },
  288. },
  289. }
  290. doc.head.apiDoc = {
  291. description: 'Asset download information.',
  292. operationId: 'assetInfo',
  293. tags: ['asset', 'metadata'],
  294. responses: {
  295. 200: {
  296. description: 'Asset info.',
  297. },
  298. default: {
  299. description: 'Unexpected error',
  300. content: {
  301. 'application/json': {
  302. schema: {
  303. $ref: '#/components/schemas/Error',
  304. },
  305. },
  306. },
  307. },
  308. },
  309. }
  310. return doc
  311. }