mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Be able to download a specific file of an asset.
This commit is contained in:
parent
7adcb1a87e
commit
3dbc1b90d7
@ -1,5 +1,4 @@
|
|||||||
import { assert } from "chai"
|
import { assert } from "chai"
|
||||||
import * as Web3 from "web3"
|
|
||||||
import * as fs from "fs"
|
import * as fs from "fs"
|
||||||
|
|
||||||
import { config } from "../config"
|
import { config } from "../config"
|
||||||
@ -94,7 +93,7 @@ describe("Consume Asset", () => {
|
|||||||
it("should consume and store the assets", async () => {
|
it("should consume and store the assets", async () => {
|
||||||
const accessService = ddo.findServiceByType("Access")
|
const accessService = ddo.findServiceByType("Access")
|
||||||
|
|
||||||
const folder = "/tmp/ocean/squid-js"
|
const folder = "/tmp/ocean/squid-js-1"
|
||||||
const path = await ocean.assets.consume(
|
const path = await ocean.assets.consume(
|
||||||
serviceAgreementSignatureResult.agreementId,
|
serviceAgreementSignatureResult.agreementId,
|
||||||
ddo.id,
|
ddo.id,
|
||||||
@ -114,4 +113,29 @@ describe("Consume Asset", () => {
|
|||||||
assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.")
|
assert.deepEqual(files, ["file-0", "file-1"], "Stored files are not correct.")
|
||||||
// assert.deepEqual(files, ["README.md", "package.json"], "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.")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
count=0
|
count=0
|
||||||
echo "Starting..."
|
echo "Starting..."
|
||||||
while sleep 0.1;
|
while sleep 0.1;
|
||||||
|
@ -72,12 +72,14 @@ export class Brizo extends Instantiable {
|
|||||||
account: Account,
|
account: Account,
|
||||||
files: File[],
|
files: File[],
|
||||||
destination: string,
|
destination: string,
|
||||||
|
index: number = -1,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
|
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
|
||||||
const filesPromises = files
|
const filesPromises = files
|
||||||
.map(async ({}, i) => {
|
.filter(({}, i) => index === -1 || i === index)
|
||||||
|
.map(async ({index}) => {
|
||||||
let consumeUrl = serviceEndpoint
|
let consumeUrl = serviceEndpoint
|
||||||
consumeUrl += `?index=${i}`
|
consumeUrl += `?index=${index}`
|
||||||
consumeUrl += `&serviceAgreementId=${agreementId}`
|
consumeUrl += `&serviceAgreementId=${agreementId}`
|
||||||
consumeUrl += `&consumerAddress=${account.getId()}`
|
consumeUrl += `&consumerAddress=${account.getId()}`
|
||||||
consumeUrl += `&signature=${agreementIdSignature}`
|
consumeUrl += `&signature=${agreementIdSignature}`
|
||||||
@ -85,7 +87,7 @@ export class Brizo extends Instantiable {
|
|||||||
try {
|
try {
|
||||||
await this.downloadFile(
|
await this.downloadFile(
|
||||||
consumeUrl,
|
consumeUrl,
|
||||||
`file-${i}`,
|
`file-${index}`,
|
||||||
destination,
|
destination,
|
||||||
)
|
)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -147,14 +147,16 @@ export class OceanAssets extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// tslint:disable-next-line
|
// 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, resultPath: string, index?: number): Promise<string>
|
||||||
public async consume(agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account): Promise<true>
|
// 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(
|
public async consume(
|
||||||
agreementId: string,
|
agreementId: string,
|
||||||
did: string,
|
did: string,
|
||||||
serviceDefinitionId: string,
|
serviceDefinitionId: string,
|
||||||
consumerAccount: Account,
|
consumerAccount: Account,
|
||||||
resultPath?: string,
|
resultPath?: string,
|
||||||
|
index: number = -1,
|
||||||
): Promise<string | true> {
|
): Promise<string | true> {
|
||||||
|
|
||||||
const ddo = await this.resolve(did)
|
const ddo = await this.resolve(did)
|
||||||
@ -179,6 +181,7 @@ export class OceanAssets extends Instantiable {
|
|||||||
consumerAccount,
|
consumerAccount,
|
||||||
files,
|
files,
|
||||||
resultPath,
|
resultPath,
|
||||||
|
index,
|
||||||
)
|
)
|
||||||
this.logger.log("Files consumed")
|
this.logger.log("Files consumed")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user