Skip to content

Commit

Permalink
Merge pull request #1 from ferencsarai/feat/filedownload
Browse files Browse the repository at this point in the history
♻️ Use downloadFile
  • Loading branch information
ferencsarai authored Oct 31, 2023
2 parents 8249a4c + 67a5dea commit b8a3396
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/JsonStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class JsonStorage {
this.addToStorage("empty.json", {}),
]).then(() => {})
await this.initPromise;
console.log('feed URL:', this.feedURLString())
console.log('feed URL:', `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}`)
}
}

Expand All @@ -63,7 +63,10 @@ export class JsonStorage {

this.mantarayNode.addFork(
this.pathToBytes(key),
this.hexStringToReference(beeUploadResult.reference)
this.hexStringToReference(beeUploadResult.reference), {
'Content Type': 'application/json',
'Filename': `${key}.json`
}
)

const savedMantaray = await this.mantarayNode.save(
Expand All @@ -84,24 +87,22 @@ export class JsonStorage {
return new TextEncoder().encode(string);
}

private feedURLString(): string{
return `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}`
}

async put(key: string, value: Object) {
await this.initAsyncResources();
await this.addToStorage(key, value);
console.log('put: ', `${this.feedURLString()}/${key}`)
console.log('put: ', key)
console.log(value)
}

async get(key: string) {
async get(key: string): Promise<Object> {
await this.initAsyncResources();
const urlString = `${this.beeURL.toString()}/bzz/${this.feedManifestResult?.reference}/${key}`
const res = await fetch(urlString)
const value = res.ok? await res.json() : {}
console.log('get: ', urlString)
const res = await this.bee.downloadFile(
this.feedManifestResult!.reference as any, key
)
const value = JSON.parse(res.data.text())
console.log('get: ', key)
console.log(value)
return value
}

}
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main() {
const def = { first: 'def1', second: 'def2' }
await jsonStorage.put('def', def)

await jsonStorage.get("abc")
await jsonStorage.get("def")
const r1 = await jsonStorage.get("abc")
const r2 = await jsonStorage.get("def")
console.log({r1})
}

0 comments on commit b8a3396

Please sign in to comment.