All files / src/fs/v1 PublicFile.ts

0% Statements 0/14
0% Branches 0/3
0% Functions 0/6
0% Lines 0/14

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                                                                                                                                                   
import { FileInfo, FileHeader, PutDetails } from '../protocol/public/types'
import { CID, FileContent } from '../../ipfs'
import BaseFile from '../base/file'
import PublicHistory from './PublicHistory'
import * as check from '../types/check'
import * as history from './PublicHistory'
import * as metadata from '../metadata'
import * as protocol from '../protocol'
import { isObject, Maybe } from '../../common'
 
 
type ConstructorParams = {
  cid: Maybe<CID>
  content: FileContent,
  header: FileHeader
}
 
export class PublicFile extends BaseFile {
 
  cid: Maybe<CID>
  header: FileHeader
  history: PublicHistory
 
  constructor({ content, header, cid }: ConstructorParams) {
    super(content)
 
    this.cid = cid
    this.header = header
    this.history = new PublicHistory(this as unknown as history.Node)
  }
 
  static instanceOf(obj: any): obj is PublicFile {
    return isObject(obj)
      && obj.content !== undefined
      && check.isFileHeader(obj.header)
  }
 
  static async create(content: FileContent): Promise<PublicFile> {
    return new PublicFile({
      content,
      header: { metadata: metadata.empty(true) },
      cid: null
    })
  }
 
  static async fromCID(cid: CID): Promise<PublicFile> {
    const info = await protocol.pub.get(cid)
    return PublicFile.fromInfo(info, cid)
  }
 
  static async fromInfo(info: FileInfo, cid: CID): Promise<PublicFile> {
    const { userland, metadata, previous } = info
    const content = await protocol.basic.getFile(userland)
    return new PublicFile({
      content,
      header: { metadata, previous },
      cid
    })
  }
 
  async putDetailed(): Promise<PutDetails> {
    const details = await protocol.pub.putFile(
      this.content,
      metadata.updateMtime(this.header.metadata),
      this.cid
    )
    this.cid = details.cid
    return details
  }
 
}
 
export default PublicFile