1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Merge pull request #78 from oceanprotocol/fix/file_download

Fix/file download
This commit is contained in:
Sebastian Gerske 2018-12-17 11:07:34 +01:00 committed by GitHub
commit 72974c49e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 45 deletions

View File

@ -1,5 +1,5 @@
[bumpversion] [bumpversion]
current_version = 0.2.3 current_version = 0.2.4
[bumpversion:file:package.json] [bumpversion:file:package.json]

13
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@oceanprotocol/squid", "name": "@oceanprotocol/squid",
"version": "0.2.3", "version": "0.2.4",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -141,9 +141,9 @@
"integrity": "sha512-lQDFUhFgQkAnbKtH5nuYXr+IyUy4HC6Z90Ipc0T8OTS4VcvLqbt7F+24scb8W8K9p3suh+eTuEnfPIbcKJqE7A==" "integrity": "sha512-lQDFUhFgQkAnbKtH5nuYXr+IyUy4HC6Z90Ipc0T8OTS4VcvLqbt7F+24scb8W8K9p3suh+eTuEnfPIbcKJqE7A=="
}, },
"@oceanprotocol/secret-store-client": { "@oceanprotocol/secret-store-client": {
"version": "0.0.13", "version": "0.0.14",
"resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.13.tgz", "resolved": "https://registry.npmjs.org/@oceanprotocol/secret-store-client/-/secret-store-client-0.0.14.tgz",
"integrity": "sha512-uEofITszJh72IAHcjXBzPx1I2xAxNUJ5kEDFako9jhQqz0seR1xGP40gj/fp+cmGU8lIq30dKWeGcYZisf735Q==", "integrity": "sha512-Yo2/9MJC1vlWXGIaU+35naYHWWYGbtzTu2t5dW4Ro1JuIRkzxfM3TSNzPUAgsCrUBUxsCsu9pd/RjvPuE45SGw==",
"requires": { "requires": {
"node-fetch": "^2.3.0" "node-fetch": "^2.3.0"
} }
@ -2350,11 +2350,6 @@
"integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==", "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==",
"dev": true "dev": true
}, },
"file-saver": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.0.tgz",
"integrity": "sha512-cYM1ic5DAkg25pHKgi5f10ziAM7RJU37gaH1XQlyNDrtUnzhC/dfoV9zf2OmF0RMKi42jG5B0JWBnPQqyj/G6g=="
},
"file-type": { "file-type": {
"version": "5.2.0", "version": "5.2.0",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-5.2.0.tgz", "resolved": "https://registry.npmjs.org/file-type/-/file-type-5.2.0.tgz",

View File

@ -1,6 +1,6 @@
{ {
"name": "@oceanprotocol/squid", "name": "@oceanprotocol/squid",
"version": "0.2.3", "version": "0.2.4",
"description": "JavaScript client library for Ocean Protocol", "description": "JavaScript client library for Ocean Protocol",
"main": "./dist/node/squid.js", "main": "./dist/node/squid.js",
"browser": "./dist/browser/squid.cjs2.min.js", "browser": "./dist/browser/squid.cjs2.min.js",
@ -52,12 +52,11 @@
"homepage": "https://github.com/oceanprotocol/squid-js#readme", "homepage": "https://github.com/oceanprotocol/squid-js#readme",
"dependencies": { "dependencies": {
"@oceanprotocol/keeper-contracts": "~0.5.0", "@oceanprotocol/keeper-contracts": "~0.5.0",
"@oceanprotocol/secret-store-client": "~0.0.13", "@oceanprotocol/secret-store-client": "~0.0.14",
"bignumber.js": "^8.0.1", "bignumber.js": "^8.0.1",
"eth-crypto": "^1.2.7", "eth-crypto": "^1.2.7",
"eth-ecies": "^1.0.3", "eth-ecies": "^1.0.3",
"ethereumjs-util": "^6.0.0", "ethereumjs-util": "^6.0.0",
"file-saver": "^2.0.0",
"jsonwebtoken": "^8.4.0", "jsonwebtoken": "^8.4.0",
"node-fetch": "^2.3.0", "node-fetch": "^2.3.0",
"uuid": "^3.3.2", "uuid": "^3.3.2",

View File

@ -63,6 +63,8 @@ import config from "./config"
service.serviceDefinitionId, service.serviceDefinitionId,
serviceAgreementSignatureResult.serviceAgreementId, serviceAgreementSignatureResult.serviceAgreementId,
serviceAgreementSignatureResult.serviceAgreementSignature, serviceAgreementSignatureResult.serviceAgreementSignature,
consumer) (files) => Logger.log(`Got files, first files length in bytes: ${files[0].length}`),
consumer,
)
})() })()

View File

@ -1,4 +1,3 @@
import {saveAs} from "file-saver"
import AquariusProvider from "../aquarius/AquariusProvider" import AquariusProvider from "../aquarius/AquariusProvider"
import SearchQuery from "../aquarius/query/SearchQuery" import SearchQuery from "../aquarius/query/SearchQuery"
import BrizoProvider from "../brizo/BrizoProvider" import BrizoProvider from "../brizo/BrizoProvider"
@ -190,39 +189,13 @@ export default class Ocean {
) )
Logger.log("Completed asset payment, now access should be granted.") Logger.log("Completed asset payment, now access should be granted.")
}) })
const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName,
accessService.conditions[1].events[1].name, {})
accessEvent.listenOnce(async (data) => {
Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.")
const webConnector = WebServiceConnectorProvider.getConnector()
const contentUrls = await SecretStoreProvider.getSecretStore()
.decryptDocument(id, metadataService.metadata.base.contentUrls[0])
const serviceUrl: string = accessService.serviceEndpoint
Logger.log("Consuming asset files using service url: ", serviceUrl)
for (const cUrl of contentUrls) {
let url: string = serviceUrl + `?url=${cUrl}`
url = url + `&serviceAgreementId=${serviceAgreementId}`
url = url + `&consumerAddress=${consumer.getId()}`
Logger.log("Fetching asset from: ", url)
const response: any = await webConnector.get(url)
const buffer: Buffer = await response.buffer()
const parts: string[] = cUrl.split("/")
const filename: string = parts[parts.length - 1]
Logger.debug(`Got response: filename is ${filename}, url is ${response.url}`)
const target = `${__dirname}/downloads/${filename}`
await saveAs([buffer.toString("utf8")], filename)
Logger.log("saved file to:", target)
}
Logger.log("Done downloading asset files.")
})
return { return {
serviceAgreementId, serviceAgreementId,
serviceAgreementSignature, serviceAgreementSignature,
} }
} catch (err) {
} catch (err) {
Logger.error("Signing ServiceAgreement failed!", err) Logger.error("Signing ServiceAgreement failed!", err)
} }
} }
@ -231,8 +204,47 @@ export default class Ocean {
serviceDefinitionId: string, serviceDefinitionId: string,
serviceAgreementId: string, serviceAgreementId: string,
serviceAgreementSignature: string, serviceAgreementSignature: string,
cb,
consumer: Account) { consumer: Account) {
const result = await BrizoProvider
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
const id = did.replace("did:op:", "")
const accessService: Service = ddo.findServiceByType("Access")
const metadataService: Service = ddo.findServiceByType("Metadata")
const accessEvent: ContractEvent = EventListener.subscribe(
accessService.conditions[1].contractName,
accessService.conditions[1].events[1].name, {})
accessEvent.listenOnce(async () => {
Logger.log("Awesome; got a AccessGranted Event. Let's download the asset files.")
const webConnector = WebServiceConnectorProvider.getConnector()
const contentUrls = await SecretStoreProvider
.getSecretStore()
.decryptDocument(id, metadataService.metadata.base.contentUrls[0])
const serviceUrl: string = accessService.serviceEndpoint
Logger.log("Consuming asset files using service url: ", serviceUrl)
const files = []
for (const cUrl of contentUrls) {
let url: string = serviceUrl + `?url=${cUrl}`
url = url + `&serviceAgreementId=${serviceAgreementId}`
url = url + `&consumerAddress=${consumer.getId()}`
Logger.log("Fetching asset from: ", url)
const response: any = await webConnector.get(url)
const responseBuffer: Buffer = await response.buffer()
const urlParts: string[] = cUrl.split("/")
const filename: string = urlParts[urlParts.length - 1]
Logger.debug(`Got response: filename is ${filename}, url is ${response.url}`)
files.push(responseBuffer.toString("utf8"))
}
Logger.log("Done downloading asset files.")
cb(files)
})
await BrizoProvider
.getBrizo() .getBrizo()
.initializeServiceAgreement( .initializeServiceAgreement(
did, did,
@ -240,8 +252,6 @@ export default class Ocean {
serviceDefinitionId, serviceDefinitionId,
serviceAgreementSignature, serviceAgreementSignature,
consumer.getId()) consumer.getId())
Logger.log(result.status)
} }
public async executeServiceAgreement(did: string, public async executeServiceAgreement(did: string,

View File

@ -109,6 +109,12 @@ export default class ServiceAgreement extends OceanBase {
throw new Error(`TemplateId not found in service "${service.type}" ddo.`) throw new Error(`TemplateId not found in service "${service.type}" ddo.`)
} }
const templateActive = await serviceAgreement.getTemplateStatus(service.templateId)
if (!templateActive) {
throw new Error(`Template with id ${service.templateId} is not active.`)
}
const executeAgreementReceipt = await serviceAgreement const executeAgreementReceipt = await serviceAgreement
.executeAgreement( .executeAgreement(
service.templateId, service.templateId,