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

removed file-saver and added a callback with the content

This commit is contained in:
Sebastian Gerske 2018-12-14 16:12:23 +01:00
parent 85496f7002
commit 77a410c607
2 changed files with 44 additions and 32 deletions

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"
@ -189,39 +188,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)
} }
} }
@ -230,8 +203,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,
@ -239,8 +251,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,