diff --git a/app/app.class.js b/app/app.class.js index 83b7040..68c989e 100644 --- a/app/app.class.js +++ b/app/app.class.js @@ -15,9 +15,9 @@ class App /** * Constructor */ - constructor( options ) + constructor( _options ) { - this.set_options( options ) + this.set_options( _options ) this.set_args() this.set_watcher() this.set_socket() @@ -26,21 +26,21 @@ class App /** * Set options */ - set_options( options ) + set_options( _options ) { // No option - if( typeof options !== 'object' ) - options = {} + if( typeof _options !== 'object' ) + _options = {} // Defaults - if( typeof options.domain === 'undefined' ) - options.domain = ip.address() + if( typeof _options.domain === 'undefined' ) + _options.domain = ip.address() - if( typeof options.port === 'undefined' ) - options.port = 3000 + if( typeof _options.port === 'undefined' ) + _options.port = 3000 // Save - this.options = options + this.options = _options } /** @@ -102,35 +102,35 @@ class App ) // Add event - this.watcher.on( 'add', ( path ) => + this.watcher.on( 'add', ( _path ) => { - console.log( 'add:'.green.bold, path ) + console.log( 'add:'.green.bold, _path ) } ) // Change event - this.watcher.on( 'change', ( path ) => + this.watcher.on( 'change', ( _path ) => { - console.log( 'change:'.green.bold, path ) + console.log( 'change:'.green.bold, _path ) } ) // Unlink event - this.watcher.on( 'unlink', ( path ) => + this.watcher.on( 'unlink', ( _path ) => { - console.log( 'unlink:'.green.bold, path ) + console.log( 'unlink:'.green.bold, _path ) } ) // AddDir event - this.watcher.on( 'addDir', ( path ) => + this.watcher.on( 'addDir', ( _path ) => { - console.log( 'addDir:'.green.bold, path ) + console.log( 'addDir:'.green.bold, _path ) } ) // UnlinkDir event - this.watcher.on( 'unlinkDir', ( path ) => + this.watcher.on( 'unlinkDir', ( _path ) => { - console.log( 'unlinkDir:'.green.bold, path ) + console.log( 'unlinkDir:'.green.bold, _path ) } ) } } -module.exports = App \ No newline at end of file +module.exports = App diff --git a/site/app.class.js b/site/app.class.js index acca289..651e385 100644 --- a/site/app.class.js +++ b/site/app.class.js @@ -6,9 +6,9 @@ let express = require( 'express' ), http = require( 'http' ), socket_io = require( 'socket.io' ), colors = require( 'colors' ), - path = require( 'path' ), ip = require( 'ip' ), - util = require( 'util' ) + util = require( 'util' ), + Projects = require( './models/projects.class.js' ) /** * App class @@ -32,20 +32,22 @@ class App */ set_models() { - let Projects = require( './models/projects.class.js' ) + // Set up this.projects = new Projects() + // Tests var project = this.projects.create_project( 'test' ) - project.update_file( 'toto/tata/tete/tutu.txt', 'content 1' ) - project.update_file( 'toto/tata/lorem.txt', 'content 2' ) - project.update_file( 'toto/tata/ipsum.txt', 'content 3' ) + // project.create_folder( 'toto' ) + // project.get_folder( 'toto//tutu/tete', true ) + // project.create_file( 'coucou/coco.txt', 'content 0' ) + // project.update_file( 'toto/tutu/tete/tutu.txt', 'content 1' ) + // project.update_file( 'toto/tata/lorem.txt', 'content 2' ) + // project.update_file( 'toto/tata/ipsum.txt', 'content 3' ) + // project.delete_folder( 'toto/tutu', true ) + // project.delete_file( 'toto/tata/ipsum.txt' ) - project.delete_file( 'toto/tata/tete/tutu.txt' ) - project.delete_file( 'toto/tata/lorem.txt' ) - project.delete_file( 'toto/tata/ipsum.txt' ) - - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + console.log( util.inspect( project, { depth: null, colors: true } ) ) } /** @@ -72,6 +74,7 @@ class App { // Set up this.express = express() + this.express.use( helmet() ) // Controllers this.express.use( '/codes', require( './controllers/index.js' ) ) @@ -118,4 +121,4 @@ class App } } -module.exports = App \ No newline at end of file +module.exports = App diff --git a/site/models/project.class.js b/site/models/project.class.js index 4918c97..7438554 100644 --- a/site/models/project.class.js +++ b/site/models/project.class.js @@ -7,8 +7,9 @@ let path = require( 'path' ), class Project { - constructor() + constructor( name ) { + this.name = name this.folders = { root: { @@ -19,7 +20,83 @@ class Project } } + create_file( _path, _content ) + { + // Set up + let normalize_path = path.normalize( _path ), + parsed_path = path.parse( normalize_path ) + + // Retrieve file + let file = this.get_file( normalize_path ) + + // File already exist + if( file ) + return false + + // Retrieve folder (force creation) + let folder = this.get_folder( parsed_path.dir, true ) + + // Create + file = new File( parsed_path.base ) + folder.files[ file.name ] = file + + // Has content + if( _content ) + file.create_version( _content ) + + return file + } + + get_file( _path, _force_creation ) + { + // Params + if( typeof _force_creation === 'undefined' ) + _force_creation = false + + // Set up + let normalize_path = path.normalize( _path ), + parsed_path = path.parse( normalize_path ) + + // Retrieve folder + let folder = this.get_folder( parsed_path.dir, _force_creation ) + + // Folder not found + if( !folder ) + return false + + // Retrieve file + let file = folder.files[ parsed_path.base ] + + // File found + if( file ) + return file + + // Force creation + if( _force_creation ) + { + // Create folders + file = this.create_file( normalize_path ) + + return file + } + + // Not found + return false + } + update_file( _path, _content ) + { + // Set up + let normalize_path = path.normalize( _path ) + + // Retrieve file + let file = this.get_file( normalize_path, true ) + + // Create version + file.create_version( _content ) + } + + delete_file( _path ) { // Set up let normalize_path = path.normalize( _path ), @@ -28,48 +105,26 @@ class Project // Retrieve folder let folder = this.get_folder( parsed_path.dir ) - // Folder doesn't exist - if( !folder ) + // Folder found + if( folder ) { - // Create folders - folder = this.create_folder( parsed_path.dir ) + let file = folder.files[ parsed_path.base ] + + // File found + if( file ) + { + file.destructor() + delete folder.files[ parsed_path.base ] + } } - - // Retrieve file - let file = folder.files[ parsed_path.base ] - - // File doesn't exist - if( !file ) - { - file = new File( parsed_path.base ) - folder.files[ file.name ] = file - } - - file.create_version( _content ) - } - - get_folder( _path ) - { - let path_parts = _path.split( path.sep ), - folder = this.folders.root - - for( let _path_part of path_parts ) - { - folder = folder.folders[ _path_part ] - - // Folder not found - if(typeof folder === 'undefined') - return false - } - - return folder } create_folder( _path ) { // Set up - let path_parts = _path.split( path.sep ), - folder = this.folders.root + let normalize_path = path.normalize( _path ), + path_parts = normalize_path.split( path.sep ), + folder = this.folders.root for( let _path_part of path_parts ) { @@ -90,46 +145,81 @@ class Project folder = __folder } - return folder; + return folder } - // clean_folders() - // { - // // Empty all - // this.folders.all = {} + get_folder( _path, _force_creation ) + { + // Params + if( typeof _force_creation === 'undefined' ) + _force_creation = false - // let clean_folder = function( folder ) - // { - // for( let _folder_name in folder.folders ) - // { - // clean_folder( folder.folders[ _folder_name ] ) - // } - // } + let normalize_path = path.normalize( _path ), + path_parts = normalize_path.split( path.sep ), + folder = this.folders.root, + found = true - // clean_folder( this.folders.root ) - // } + for( let _path_part of path_parts ) + { + folder = folder.folders[ _path_part ] - delete_file( _path ) + // Folder not found + if(typeof folder === 'undefined') + { + found = false + break + } + } + + // Folder found + if( found ) + return folder + + // Force creation + if( _force_creation ) + { + // Create folders + folder = this.create_folder( normalize_path ) + + return folder + } + + // Not found + return false + } + + delete_folder( _path ) { // Set up let normalize_path = path.normalize( _path ), - parsed_path = path.parse( normalize_path ) + path_parts = _path.split( path.sep ), + folder = this.get_folder( normalize_path ) - // Retrieve folder - let folder = this.get_folder( parsed_path.dir ) + // Folder not found + if( !folder ) + return false - // Folder found - if( folder ) - { - let file = folder.files[ parsed_path.base ] - - // File found - if( file ) + // Recursive callback + let delete_folder = function( folder ) { - delete folder.files[ parsed_path.base ] + for( let _folder_name in folder.folders ) + { + delete_folder( folder.folders[ _folder_name ] ) + } + + for( let _file_name in folder.files ) + { + let file = folder.files[ _file_name ] + + file.destructor() + delete folder.files[ _file_name ] + } } - } + + delete_folder( folder ) + + return true } } -module.exports = Project \ No newline at end of file +module.exports = Project diff --git a/test-folder/iusdhfiuhsdf b/test-folder/iusdhfiuhsdf index e69de29..36b2124 100644 --- a/test-folder/iusdhfiuhsdf +++ b/test-folder/iusdhfiuhsdf @@ -0,0 +1 @@ +dsfsdf diff --git a/test-folder/udshfiuhsdf/iuisduhfsdf b/test-folder/udshfiuhsdf/iuisduhfsdf deleted file mode 100644 index c1f5866..0000000 --- a/test-folder/udshfiuhsdf/iuisduhfsdf +++ /dev/null @@ -1 +0,0 @@ -sdfsdfdsf \ No newline at end of file