Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Files): bootstrap basic files architecture #923

Merged
merged 17 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ sw.*

# Vim swap files
*.swp
.vscode
InfamousVague marked this conversation as resolved.
Show resolved Hide resolved
109 changes: 109 additions & 0 deletions libraries/Files/Directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Item } from './abstracts/Item.abstract'
import { FileSystemErrors } from './errors/Errors'
import { DIRECTORY_TYPE } from './types/directory'

export class Directory extends Item {
private _type: DIRECTORY_TYPE.DEFAULT
private _children = new Map()

/**
* @param {string=''} name Name of the new directory
* @param {DIRECTORY_TYPE=DIRECTORY_TYPE.DEFAULT} type directory type of the new folder
* @returns {Directory}
*/
constructor(name: string = '', type: DIRECTORY_TYPE = DIRECTORY_TYPE.DEFAULT) {
super(name || 'un-named directory')
this._type = DIRECTORY_TYPE[type] ? type : DIRECTORY_TYPE.DEFAULT
}

/**
* @getter content
* @returns {Array} containing directory contents
*/
get content(): Array<any> {
return Array.from(this._children.values())
}
/**
* @getter type
* @returns {DIRECTORY_TYPE} returns the type of directory
*/
get type(): DIRECTORY_TYPE {
return this._type
}

/**
* @getter
* @returns {Directory} returns a cloned copy of this directory
*/
get copy(): Directory {
const dirCopy = new Directory(`${this.name} copy`, this.type)

this.content.forEach(item => {
const itemCopy = item.copy
itemCopy.name = item.name
dirCopy.addChild(itemCopy)
})

return dirCopy
}

/**
* @method hasChild
* @param {string} childName the name of the child to search for
* @returns {boolean} returns true or false depending on if a child exists in the directory
*/
hasChild(childName: string): boolean {
return this._children.has(childName)
}

/**
* @method addChild
* @param {Item} child the child to add to the parent directory
* @returns {boolean} returns true or false depending on if a child exists in the directory
*/
addChild(child: Item): boolean {
if (this.hasChild(child.name)) return false

if( child === this) throw new Error(FileSystemErrors.DIR_PARADOX)

let parent = this.parent

while (parent !== null) {
if (parent === child)
throw new Error(FileSystemErrors.DIR_PARENT_PARADOX)
parent = parent.parent
}

this._children.set(child.name, child)
child.parent = this

return this.hasChild(child.name)
}


/**
* @method getChild
* @param {string} childName the name of the child to fetch
* @returns {Item} returns the child if it exists, otherwise returns null
*/
getChild(childName: string): Item {
return this._children.get(childName) || null
}

/**
* @method removeChild
* @param {string} childName the name of the child to remove
* @returns {Item} returns true if the child has been successfully removed
*/
removeChild(childName: string): boolean {
if (this.getChild(childName) === null) return false
const child = this.getChild(childName)

if (child) {
this._children.delete(childName)
child.parent = null
}

return !this.hasChild(childName)
}
}
63 changes: 63 additions & 0 deletions libraries/Files/Fil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Item } from "./abstracts/Item.abstract"

import { FILE_TYPE } from './types/file'

export class Fil extends Item {
private _type = FILE_TYPE.GENERIC
private _hash: string = ''
private _description: string = ''

/**
* Create a new file instance
* @constructor
* @param {string} name - Name of the file.
* @param {string} description - Short description or associated text for the file
* @param {string} hash - Hash location of the file on chain (Usually IPFS Hash)
*/
constructor(name: string = '', description: string = '', hash: string = '') {
super(name || 'un-named file')
this._description = description
this._hash = hash
}

/**
* Get the file description
* @getter
*/
get description(): string {
return this._description
}

/**
* Get the file type in plain text
* @getter
*/
get type(): FILE_TYPE {
return this._type
}

/**
* Returns the hash of the file (usually IPFS)
* @getter
*/
get hash(): string {
return this._hash
}

/**
* Get a new copy of the file
* @getter
*/
get copy(): Fil {
return new Fil(`${this.name} copy`, this._description, this._hash)
}

/**
* Update the files description text
* @setter
* @param {string} content the content to set the file description to
*/
set description(content: string) {
this._description = `${content || ''}`
}
}
Loading