Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | import BaseFile from '../base/file'
import MMPT from '../protocol/private/mmpt'
import PrivateHistory from './PrivateHistory'
import { FileContent } from '../../ipfs'
import { Maybe } from '../../common'
import { PrivateName, BareNameFilter } from '../protocol/private/namefilter'
import { PrivateAddResult, PrivateFileInfo, Revision } from '../protocol/private/types'
import { genKeyStr } from '../../keystore'
import { isObject } from '../../common/type-checks'
import * as check from '../protocol/private/types/check'
import * as history from './PrivateHistory'
import * as metadata from '../metadata'
import * as protocol from '../protocol'
import * as namefilter from '../protocol/private/namefilter'
type ConstructorParams = {
content: FileContent
key: string
header: PrivateFileInfo
mmpt: MMPT
}
export class PrivateFile extends BaseFile {
header: PrivateFileInfo
history: PrivateHistory
key: string
mmpt: MMPT
constructor({ content, mmpt, key, header }: ConstructorParams) {
super(content)
this.header = header
this.history = new PrivateHistory(this as unknown as history.Node)
this.key = key
this.mmpt = mmpt
}
static instanceOf(obj: any): obj is PrivateFile {
return isObject(obj)
&& obj.content !== undefined
&& obj.mmpt !== undefined
&& check.isPrivateFileInfo(obj.header)
}
static async create(mmpt: MMPT, content: FileContent, parentNameFilter: BareNameFilter, key: string): Promise<PrivateFile> {
const bareNameFilter = await namefilter.addToBare(parentNameFilter, key)
const contentKey = await genKeyStr()
const contentInfo = await protocol.basic.putEncryptedFile(content, contentKey)
return new PrivateFile({
content,
mmpt,
key,
header: {
bareNameFilter,
key: contentKey,
revision: 1,
metadata: metadata.empty(true),
content: contentInfo.cid
}
})
}
static async fromLatestName(mmpt: MMPT, name: PrivateName, key: string): Promise<PrivateFile> {
const info = await protocol.priv.getByLatestName(mmpt, name, key)
if (!check.isPrivateFileInfo(info)) {
throw new Error(`Could not parse a valid private file using the given key`)
}
return PrivateFile.fromInfo(mmpt, key, info)
}
static async fromName(mmpt: MMPT, name: PrivateName, key: string): Promise<PrivateFile> {
const info = await protocol.priv.getByName(mmpt, name, key)
if (!check.isPrivateFileInfo(info)) {
throw new Error(`Could not parse a valid private file using the given key`)
}
return PrivateFile.fromInfo(mmpt, key, info)
}
static async fromInfo(mmpt: MMPT, key: string, info: PrivateFileInfo): Promise<PrivateFile> {
const content = await protocol.basic.getEncryptedFile(info.content, info.key)
return new PrivateFile({
content,
key,
mmpt,
header: info
})
}
async getName(): Promise<PrivateName> {
const { bareNameFilter, revision } = this.header
const revisionFilter = await namefilter.addRevision(bareNameFilter, this.key, revision)
return namefilter.toPrivateName(revisionFilter)
}
async updateParentNameFilter(parentNameFilter: BareNameFilter): Promise<this> {
this.header.bareNameFilter = await namefilter.addToBare(parentNameFilter, this.header.key)
return this
}
async updateContent(content: FileContent): Promise<this> {
const contentInfo = await protocol.basic.putEncryptedFile(content, this.header.key)
this.content = content
this.header = {
...this.header,
revision: this.header.revision + 1,
content: contentInfo.cid
}
return this
}
async putDetailed(): Promise<PrivateAddResult> {
return protocol.priv.addNode(this.mmpt, {
...this.header,
metadata: metadata.updateMtime(this.header.metadata)
}, this.key)
}
}
export default PrivateFile
|