|
@@ -18,12 +18,9 @@
|
|
|
|
|
|
'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')
|
|
|
+const ipfsProxy = require('../../../lib/middleware/ipfs_proxy')
|
|
|
|
|
|
function errorHandler(response, err, code) {
|
|
|
debug(err)
|
|
@@ -31,6 +28,9 @@ function errorHandler(response, err, code) {
|
|
|
}
|
|
|
|
|
|
module.exports = function (storage, runtime) {
|
|
|
+
|
|
|
+ const proxy = ipfsProxy.createProxy(storage)
|
|
|
+
|
|
|
const doc = {
|
|
|
|
|
|
parameters: [
|
|
@@ -45,34 +45,6 @@ module.exports = function (storage, runtime) {
|
|
|
},
|
|
|
],
|
|
|
|
|
|
-
|
|
|
- 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
|
|
@@ -184,61 +156,21 @@ module.exports = function (storage, runtime) {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
-
|
|
|
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}`
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ proxy(req, res)
|
|
|
+ },
|
|
|
|
|
|
- const opts = {
|
|
|
- name: sendName,
|
|
|
- type,
|
|
|
- size,
|
|
|
- ranges,
|
|
|
- download,
|
|
|
- }
|
|
|
- utilRanges.send(res, stream, opts)
|
|
|
- } catch (err) {
|
|
|
- errorHandler(res, err, err.code)
|
|
|
- }
|
|
|
+ async head(req, res) {
|
|
|
+ proxy(req, res)
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
doc.get.apiDoc = {
|
|
|
description: 'Download an asset.',
|