123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- 'use strict'
- const path = require('path')
- const debug = require('debug')('joystream:colossus:api:asset')
- const utilRanges = require('@joystream/storage-utils/ranges')
- const filter = require('@joystream/storage-node-backend/filter')
- function errorHandler(response, err, code) {
- debug(err)
- response.status(err.code || code || 500).send({ message: err.toString() })
- }
- module.exports = function (storage, runtime) {
- const doc = {
-
- parameters: [
- {
- name: 'id',
- in: 'path',
- required: true,
- description: 'Joystream Content ID',
- schema: {
- type: 'string',
- },
- },
- ],
-
- async head(req, res) {
- const id = req.params.id
-
- try {
- const size = await storage.size(id)
- const stream = await storage.open(id, 'r')
- const type = stream.fileInfo.mimeType
-
-
- stream.destroy()
- res.status(200)
- res.contentType(type)
- res.header('Content-Disposition', 'inline')
- res.header('Content-Transfer-Encoding', 'binary')
- res.header('Accept-Ranges', 'bytes')
- if (size > 0) {
- res.header('Content-Length', size)
- }
- res.send()
- } catch (err) {
- errorHandler(res, err, err.code)
- }
- },
-
- async put(req, res) {
- const id = req.params.id
-
-
- const roleAddress = runtime.identities.key.address
- const providerId = runtime.storageProviderId
- let dataObject
- try {
- debug('calling checkLiaisonForDataObject')
- dataObject = await runtime.assets.checkLiaisonForDataObject(providerId, id)
- debug('called checkLiaisonForDataObject')
- } catch (err) {
- errorHandler(res, err, 403)
- return
- }
-
-
- let stream
- try {
- stream = await storage.open(id, 'w')
-
-
- let finished = false
- let accepted = false
- const possiblyCommit = () => {
- if (finished && accepted) {
- debug('Stream is finished and passed filters; committing.')
- stream.commit()
- }
- }
- stream.on('fileInfo', async info => {
- try {
- debug('Detected file info:', info)
-
- const filterResult = filter({}, req.headers, info.mimeType)
- if (200 !== filterResult.code) {
- debug('Rejecting content', filterResult.message)
- stream.end()
- res.status(filterResult.code).send({ message: filterResult.message })
-
- await runtime.assets.rejectContent(roleAddress, providerId, id)
- return
- }
- debug('Content accepted.')
- accepted = true
-
- possiblyCommit()
- } catch (err) {
- errorHandler(res, err)
- }
- })
- stream.on('finish', () => {
- try {
- finished = true
- possiblyCommit()
- } catch (err) {
- errorHandler(res, err)
- }
- })
- stream.on('committed', async hash => {
- console.log('commited', dataObject)
- try {
- if (hash !== dataObject.ipfs_content_id.toString()) {
- debug('Rejecting content. IPFS hash does not match value in objectId')
- await runtime.assets.rejectContent(roleAddress, providerId, id)
- res.status(400).send({ message: "Uploaded content doesn't match IPFS hash" })
- return
- }
- debug('accepting Content')
- await runtime.assets.acceptContent(roleAddress, providerId, id)
- debug('creating storage relationship for newly uploaded content')
-
- const dosrId = await runtime.assets.createAndReturnStorageRelationship(roleAddress, providerId, id)
- debug('toggling storage relationship for newly uploaded content')
- await runtime.assets.toggleStorageRelationshipReady(roleAddress, providerId, dosrId, true)
- debug('Sending OK response.')
- res.status(200).send({ message: 'Asset uploaded.' })
- } catch (err) {
- debug(`${err.message}`)
- errorHandler(res, err)
- }
- })
- stream.on('error', err => errorHandler(res, err))
- req.pipe(stream)
- } catch (err) {
- errorHandler(res, err)
- return
- }
- },
-
- async get(req, res) {
- const id = req.params.id
- const download = req.query.download
-
- let ranges
- if (!download) {
- try {
- const rangeHeader = req.headers.range
- ranges = utilRanges.parse(rangeHeader)
- } catch (err) {
-
-
- }
- if (ranges && ranges.unit !== 'bytes') {
-
- ranges = undefined
- }
- }
- debug('Requested range(s) is/are', ranges)
-
- try {
- const size = await storage.size(id)
- const stream = await storage.open(id, 'r')
-
-
- let sendName = id
- const type = stream.fileInfo.mimeType
- if (download) {
- let ext = path.extname(sendName)
- if (!ext) {
- ext = stream.fileInfo.ext
- if (ext) {
- sendName = `${sendName}.${ext}`
- }
- }
- }
- const opts = {
- name: sendName,
- type,
- size,
- ranges,
- download,
- }
- utilRanges.send(res, stream, opts)
- } catch (err) {
- errorHandler(res, err, err.code)
- }
- },
- }
-
- doc.get.apiDoc = {
- description: 'Download an asset.',
- operationId: 'assetData',
- tags: ['asset', 'data'],
- parameters: [
- {
- name: 'download',
- in: 'query',
- description: 'Download instead of streaming inline.',
- required: false,
- allowEmptyValue: true,
- schema: {
- type: 'boolean',
- default: false,
- },
- },
- ],
- responses: {
- 200: {
- description: 'Asset download.',
- content: {
- default: {
- schema: {
- type: 'string',
- format: 'binary',
- },
- },
- },
- },
- default: {
- description: 'Unexpected error',
- content: {
- 'application/json': {
- schema: {
- $ref: '#/components/schemas/Error',
- },
- },
- },
- },
- },
- }
- doc.put.apiDoc = {
- description: 'Asset upload.',
- operationId: 'assetUpload',
- tags: ['asset', 'data'],
- requestBody: {
- content: {
- '*/*': {
- schema: {
- type: 'string',
- format: 'binary',
- },
- },
- },
- },
- responses: {
- 200: {
- description: 'Asset upload.',
- content: {
- 'application/json': {
- schema: {
- type: 'object',
- required: ['message'],
- properties: {
- message: {
- type: 'string',
- },
- },
- },
- },
- },
- },
- default: {
- description: 'Unexpected error',
- content: {
- 'application/json': {
- schema: {
- $ref: '#/components/schemas/Error',
- },
- },
- },
- },
- },
- }
- doc.head.apiDoc = {
- description: 'Asset download information.',
- operationId: 'assetInfo',
- tags: ['asset', 'metadata'],
- responses: {
- 200: {
- description: 'Asset info.',
- },
- default: {
- description: 'Unexpected error',
- content: {
- 'application/json': {
- schema: {
- $ref: '#/components/schemas/Error',
- },
- },
- },
- },
- },
- }
- return doc
- }
|