UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

32 lines (31 loc) 1.05 kB
import { ERROR, isHttp } from '../common'; import { FileLinks } from './FileLinks'; import { Uri } from '../Uri'; export class FileSchema { constructor(args) { this.type = 'FILE'; this.fileid = args.fileid; this.path = `${args.nsPath}/${this.type}/${this.fileid}`; this.uri = args.uri; } static uri(args) { const path = (args.path || '').trim(); const parts = path.split('/'); if (parts[0] !== 'NS') { throw new Error(`The DB path does not start with 'NS/'. Given '${path}'.`); } if (parts[2] !== 'FILE') { throw new Error(`The DB path does not contain '/FILE/'. Given '${path}'.`); } const ns = parts[1]; const file = parts[3]; return Uri.create.file(ns, file); } static toFileLocation(input = '') { input = input.trim(); return isHttp(input) ? input : `file://${input}`; } } FileSchema.links = FileLinks; FileSchema.ERROR = ERROR; FileSchema.create = (args) => new FileSchema(args);