Be able to download a specific file of an asset.

This commit is contained in:
Pedro Gutiérrez 2019-04-03 17:57:47 +02:00 committed by Pedro Gutiérrez
parent 7adcb1a87e
commit 3dbc1b90d7
4 changed files with 38 additions and 7 deletions

View File

@ -1,5 +1,4 @@
import { assert } from "chai"
import * as Web3 from "web3"
import * as fs from "fs"
import { config } from "../config"
@ -94,7 +93,7 @@ describe("Consume Asset", () => {
it("should consume and store the assets", async () => {
const accessService = ddo.findServiceByType("Access")
const folder = "/tmp/ocean/squid-js"
const folder = "/tmp/ocean/squid-js-1"
const path = await ocean.assets.consume(
serviceAgreementSignatureResult.agreementId,
ddo.id,
@ -114,4 +113,29 @@ describe("Consume Asset", () => {
assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.")
// assert.deepEqual(files, ["README.md", "package.json"], "Stored files are not correct.")
})
it("should consume and store one assets", async () => {
const accessService = ddo.findServiceByType("Access")
const folder = "/tmp/ocean/squid-js-2"
const path = await ocean.assets.consume(
serviceAgreementSignatureResult.agreementId,
ddo.id,
accessService.serviceDefinitionId,
consumer,
folder,
1,
)
assert.include(path, folder, "The storage path is not correct.")
const files = await new Promise<string[]>((resolve) => {
fs.readdir(path, (err, fileList) => {
resolve(fileList)
})
})
assert.deepEqual(files, ["file-1"], "Stored files are not correct.")
// assert.deepEqual(files, ["package.json"], "Stored files are not correct.")
})
})

View File

@ -1,3 +1,5 @@
#!/bin/sh
count=0
echo "Starting..."
while sleep 0.1;

View File

@ -72,12 +72,14 @@ export class Brizo extends Instantiable {
account: Account,
files: File[],
destination: string,
index: number = -1,
): Promise<string> {
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
const filesPromises = files
.map(async ({}, i) => {
.filter(({}, i) => index === -1 || i === index)
.map(async ({index}) => {
let consumeUrl = serviceEndpoint
consumeUrl += `?index=${i}`
consumeUrl += `?index=${index}`
consumeUrl += `&serviceAgreementId=${agreementId}`
consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${agreementIdSignature}`
@ -85,7 +87,7 @@ export class Brizo extends Instantiable {
try {
await this.downloadFile(
consumeUrl,
`file-${i}`,
`file-${index}`,
destination,
)
} catch (e) {

View File

@ -147,14 +147,16 @@ export class OceanAssets extends Instantiable {
}
// tslint:disable-next-line
public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath: string): Promise<string>
public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account): Promise<true>
public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath: string, index?: number): Promise<string>
// tslint:disable-next-line
public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath?: undefined | null, index?: number): Promise<true>
public async consume(
agreementId: string,
did: string,
serviceDefinitionId: string,
consumerAccount: Account,
resultPath?: string,
index: number = -1,
): Promise<string | true> {
const ddo = await this.resolve(did)
@ -179,6 +181,7 @@ export class OceanAssets extends Instantiable {
consumerAccount,
files,
resultPath,
index,
)
this.logger.log("Files consumed")