All files / src/filesystem filesystem.js

90.1% Statements 91/101
84.38% Branches 54/64
100% Functions 18/18
91.58% Lines 87/95

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                        25x         68x   68x   68x 18x     50x 50x   50x 1x 49x       49x 49x         46x 46x 46x 46x   46x 14x     32x 32x       18x 18x 18x   18x 1x 17x 16x   1x     16x 5x   11x         2x 2x 2x 1x   1x       23x   23x       23x   23x 1x 22x       22x 22x   22x           13x   13x       13x 13x 13x       13x 13x   13x               1x 1x 10x 10x           1x       7x 7x   7x 1x 6x     6x         4x 4x   4x 4x   4x   4x 8x 3x     4x       202x   202x       2x 2x         8x 8x   8x 8x   8x 1x 2x   2x 2x         15x            
// base FileSystem module, initial version,
// needs refactoring
 
import Path from './path'
import MODE from './mode'
import Node from './node'
import Stats from './stats'
import FileInfo from './fileinfo'
import IndexedDbStorage from '../storages/indexeddb'
 
export default class FileSystem {
  constructor (opts = {}) {
    this.storage = opts.storage ||
      new IndexedDbStorage((opts && opts.name) || 'default')
  }
 
  async mkdir (path) {
    path = new Path(path).normalize()
 
    const data = await this.exists(path)
 
    if (data) {
      return data.path
    }
 
    const parent = await this.exists(path.parent)
    const parentId = parent ? parent.path : 0
 
    if (!path.parent.isRoot && !parent) {
      throw new Error('parent is not created yet')
    } else Iif (parent && parent.node.mode !== MODE.DIR) {
      throw new Error('parent is not dir')
    }
 
    const node = new Node(path.path, MODE.DIR, 0)
    return this.storage.create(path.path, node, parentId)
  }
 
  // Recursively creates directory, eq. to mkdir -p
  async mkdirParents (path, root = '') {
    path = new Path(path).normalize()
    const mparts = path.path.split('/')
    const mroot = root === '' ? '/' : `${root}/`
    const currentPath = mroot + mparts.shift()
 
    if (mparts.length === 0) {
      return this.mkdir(currentPath)
    }
 
    await this.mkdir(currentPath)
    return this.mkdirParents(mparts.join('/'), currentPath)
  }
 
  async rmdir (path) {
    path = new Path(path).normalize()
    const data = await this.exists(path)
    let isEmpty = false
 
    if (!data) {
      throw new Error('dir does not exists')
    } else if (data.node.mode === MODE.DIR) {
      isEmpty = await this.storage.isEmpty(data.path)
    } else {
      throw new Error('it is not a dir')
    }
 
    if (isEmpty) {
      await this.storage.remove(path.path)
    } else {
      throw new Error('dir is not empty')
    }
  }
 
  async readFile (path, options) {
    path = new Path(path).normalize()
    const data = await this.storage.get(path.path)
    if (!data) {
      throw new Error(`File ${path.path} does not exist`)
    }
    return data.node.data
  }
 
  async writeFile (path, data, options) {
    path = new Path(path).normalize()
 
    Iif (!(data instanceof Blob)) {
      throw new Error('data must be instance of Blob')
    }
 
    const parent = await this.exists(path.parent)
 
    if (!parent) {
      throw new Error('file needs parent')
    } else Iif (parent && parent.node.mode !== MODE.DIR) {
      throw new Error('parent should be dir')
    }
 
    const parentId = parent.path
    const node = new Node(path.path, MODE.FILE, data.size, options, data)
 
    return this.storage.put(path.path, node, parentId)
  }
 
  // Same as writeFile but it recursively creates directory
  // if not exists
  async outputFile (path, data, options) {
    path = new Path(path).normalize()
 
    Iif (!(data instanceof Blob)) {
      throw new Error('data must be instance of Blob')
    }
 
    const parentPath = await this.mkdirParents(path.parent)
    const parent = await this.exists(parentPath)
    Iif (parent && parent.node.mode !== MODE.DIR) {
      throw new Error('parent should be dir')
    }
 
    const parentId = parent.path
    const node = new Node(path.path, MODE.FILE, data.size, options, data)
 
    return this.storage.put(path.path, node, parentId)
  }
 
  // Dexie specific to insert multiple files in one go
  // Chrome is quite slow with lots of insertion, hence
  // this makes chrome happy
  // https://dev.to/skhmt/why-are-indexeddb-operations-significantly-slower-in-chrome-vs-firefox-1bnd
  async bulkOutputFiles (objs) {
    return this.storage.transaction('rw', async () => {
      for (let i = 0; i < objs.length; i++) {
        const o = objs[i]
        await this.outputFile(o.path, o.blob, o.options || {})
      }
    })
  }
 
  async rename (oldPath, newPath) {
    throw new Error('not implemented')
  }
 
  async unlink (path) {
    path = new Path(path).normalize()
    const data = await this.exists(path)
 
    if (!data) {
      throw new Error('file does not exists')
    } else Iif (data.node.mode === MODE.DIR) {
      throw new Error('path points to a directory, please use rmdir')
    } else {
      return this.storage.remove(data.path)
    }
  }
 
  async rmdirRecursive (path) {
    path = new Path(path).normalize()
    const data = await this.exists(path)
 
    Iif (!data) return true
    Iif (data.node.mode !== MODE.DIR) return this.unlink(path)
 
    const list = await this.ls(path)
 
    for (const element of list) {
      if (element.node.mode !== MODE.DIR) await this.unlink(element.path)
      else await this.rmdirRecursive(element.path)
    }
 
    return this.rmdir(path)
  }
 
  async exists (path) {
    path = new Path(path).normalize()
 
    return this.storage.get(path.path)
  }
 
  async stats (path) {
    const data = await this.exists(path)
    Eif (data) return new Stats(data.node, this.path)
    else throw new Error('path does not exist')
  }
 
  async ls (path, filters = {}) {
    const filterKeys = Object.keys(filters)
    const data = await this.exists(path)
 
    Eif (data) {
      let nodes = await this.storage.where({ parentId: data.path })
 
      if (filterKeys.length > 0) {
        nodes = nodes.filter((node) => {
          const fileInfo = new FileInfo(node.node, node.path)
 
          return filterKeys.some((key) => {
            return fileInfo[key] === filters[key]
          })
        })
      }
 
      return nodes.map(node => new FileInfo(node.node, node.path))
    }
 
    throw new Error('path does not exist')
  }
}