generated from fission-codes/project-template
-
Notifications
You must be signed in to change notification settings - Fork 24
/
file.ts
47 lines (34 loc) · 1.16 KB
/
file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { CID } from "multiformats/cid"
import * as Depot from "../../components/depot/implementation.js"
import * as Protocol from "../protocol/index.js"
import { PutResult } from "../../components/depot/implementation.js"
import { isObject, hasProp } from "../../common/index.js"
import BaseFile from "../base/file.js"
export class BareFile extends BaseFile {
depot: Depot.Implementation
constructor(depot: Depot.Implementation, content: Uint8Array) {
super(content)
this.depot = depot
}
static create(depot: Depot.Implementation, content: Uint8Array): BareFile {
return new BareFile(depot, content)
}
static async fromCID(depot: Depot.Implementation, cid: CID): Promise<BareFile> {
const content = await Protocol.basic.getFile(depot, cid)
return new BareFile(depot, content)
}
static instanceOf(obj: unknown): obj is BareFile {
return isObject(obj) && hasProp(obj, "content")
}
async put(): Promise<CID> {
const { cid } = await this.putDetailed()
return cid
}
async putDetailed(): Promise<PutResult> {
return Protocol.basic.putFile(
this.depot,
this.content
)
}
}
export default BareFile