Pārlūkot izejas kodu

storage-node: only serve Accepted content through ipfs gateway

Mokhtar Naamani 4 gadi atpakaļ
vecāks
revīzija
3806714592

+ 4 - 12
storage-node/packages/colossus/lib/middleware/ipfs_proxy.js

@@ -26,23 +26,15 @@ const pathFilter = function (path, req) {
   return path.match('^/asset/v0') && (req.method === 'GET' || req.method === 'HEAD')
 }
 
-const createPathRewriter = (resolve) => {
+const createPathRewriter = () => {
   return async (_path, req) => {
-    // we expect the handler was used in openapi/express path with an id in the path:
-    // "/asset/v0/:id"
-    // TODO: catch and deal with hash == undefined if content id not found
-    const contentId = req.params.id
-    const hash = await resolve(contentId)
+    const hash = req.params.ipfs_content_id
     return `/ipfs/${hash}`
   }
 }
 
-const createResolver = (storage) => {
-  return async (id) => await storage.resolveContentIdWithTimeout(5000, id)
-}
-
-const createProxy = (storage, ipfsHttpGatewayUrl) => {
-  const pathRewrite = createPathRewriter(createResolver(storage))
+const createProxy = (ipfsHttpGatewayUrl) => {
+  const pathRewrite = createPathRewriter()
 
   return createProxyMiddleware(pathFilter, {
     // Default path to local IPFS HTTP GATEWAY

+ 15 - 3
storage-node/packages/colossus/paths/asset/v0/{id}.js

@@ -32,7 +32,19 @@ const PROCESS_UPLOAD_BALANCE = 3
 
 module.exports = function (storage, runtime, ipfsHttpGatewayUrl, anonymous) {
   // Creat the IPFS HTTP Gateway proxy middleware
-  const proxy = ipfsProxy.createProxy(storage, ipfsHttpGatewayUrl)
+  const proxy = ipfsProxy.createProxy(ipfsHttpGatewayUrl)
+
+  const proxyAcceptedContentToIpfsGateway = async (req, res, next) => {
+    // make sure id exists and was Accepted only then proxy
+    const dataObject = await runtime.assets.getDataObject(req.params.id)
+
+    if (dataObject && dataObject.liaison_judgement.type === 'Accepted') {
+      req.params.ipfs_content_id = dataObject.ipfs_content_id.toString()
+      proxy(req, res, next)
+    } else {
+      res.status(404).send({ message: 'Content not found' })
+    }
+  }
 
   const doc = {
     // parameters for all operations in this path
@@ -183,11 +195,11 @@ module.exports = function (storage, runtime, ipfsHttpGatewayUrl, anonymous) {
     },
 
     async get(req, res, next) {
-      proxy(req, res, next)
+      proxyAcceptedContentToIpfsGateway(req, res, next)
     },
 
     async head(req, res, next) {
-      proxy(req, res, next)
+      proxyAcceptedContentToIpfsGateway(req, res, next)
     },
   }