From d2df4758df988b29176a3301575907a6974529e1 Mon Sep 17 00:00:00 2001 From: brunosimon Date: Tue, 9 Aug 2016 19:20:58 +0200 Subject: [PATCH] Update project structure --- site/app.class.js | 54 +++---- site/models/files.class.js | 121 +++++++++++++++ site/models/project.class.js | 293 +---------------------------------- 3 files changed, 146 insertions(+), 322 deletions(-) create mode 100644 site/models/files.class.js diff --git a/site/app.class.js b/site/app.class.js index 1356416..f40c0e2 100644 --- a/site/app.class.js +++ b/site/app.class.js @@ -41,18 +41,14 @@ class App // Tests var project = this.projects.create_project( 'dummy' ) - project.create_folder( './toto' ) - project.get_folder( './toto//tutu/tete', true ) - project.create_file( './coucou/coco.txt', '1234' ) - project.create_file( './test-1.txt', 'content 1' ) - project.update_file( './test-1.txt', 'content 2' ) - project.update_file( './test-1.txt', 'content 31298' ) - project.update_file( './toto/tata/lorem.txt', '123456789' ) - project.update_file( './toto/tata/lorem.txt', '1aze' ) - project.update_file( './toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?' ) - project.delete_folder( './toto/tata', true ) - project.delete_file( './toto/tata/ipsum.txt' ) - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + project.files.create( './coucou/coco.txt', '1234' ) + project.files.create( './test-1.txt', 'content 1' ) + project.files.update( './test-1.txt', 'content 2' ) + project.files.update( './test-1.txt', 'content 31298' ) + project.files.update( './toto/tata/lorem.txt', '123456789' ) + project.files.update( './toto/tata/lorem.txt', '1aze' ) + project.files.update( './toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?' ) + project.files.delete( './toto/tata/ipsum.txt' ) console.log( util.inspect( project.files, { depth: null, colors: true } ) ) } @@ -115,9 +111,9 @@ class App set_socket() { // Set up - this.sockets = {} - this.sockets.main = socket_io.listen( this.server ) - this.sockets.app = this.sockets.main.of( '/app' ) + this.sockets = {} + this.sockets.main = socket_io.listen( this.server ) + this.sockets.app = this.sockets.main.of( '/app' ) // App connection event this.sockets.app.on( 'connection', ( socket ) => @@ -133,42 +129,28 @@ class App // Create project project = this.projects.create_project( data.name ) - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + console.log( util.inspect( project.files, { depth: null, colors: true } ) ) } ) socket.on( 'update_file', ( data ) => { - project.update_file( data.path, data.content ) + project.files.file( data.path, data.content ) - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + console.log( util.inspect( project.files, { depth: null, colors: true } ) ) } ) socket.on( 'create_file', ( data ) => { - project.create_file( data.path, data.content ) + project.files.create( data.path, data.content ) - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + console.log( util.inspect( project.files, { depth: null, colors: true } ) ) } ) socket.on( 'delete_file', ( data ) => { - project.delete_file( data.path ) + project.files.delete( data.path ) - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) - } ) - - socket.on( 'create_folder', ( data ) => - { - project.create_folder( data.path, data.content ) - - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) - } ) - - socket.on( 'delete_folder', ( data ) => - { - project.delete_folder( data.path ) - - console.log( util.inspect( project.folders, { depth: null, colors: true } ) ) + console.log( util.inspect( project.files, { depth: null, colors: true } ) ) } ) } ) } diff --git a/site/models/files.class.js b/site/models/files.class.js new file mode 100644 index 0000000..38674cd --- /dev/null +++ b/site/models/files.class.js @@ -0,0 +1,121 @@ +'use strict' + +// Dependencies +let paths = require( '../utils/paths.class.js' ), + File = require( './file.class.js' ) + +class Files +{ + constructor( options ) + { + this.items = {} + } + + create( _path, _content ) + { + // Set up + let normalized_path = paths.normalize( _path ), + parsed_path = paths.parse( normalized_path ) + + // Retrieve file + let file = this.get( normalized_path ) + + // File already exist + if( file ) + return false + + // Create + file = new File( { + name : parsed_path.base, + path : parsed_path.dir, + content: _content + } ) + + // Save + this.items[ normalized_path ] = file + + return file + } + + get( _path, _force_creation ) + { + // Params + if( typeof _force_creation === 'undefined' ) + _force_creation = false + + // Set up + let normalized_path = paths.normalize( _path ) + + // Retrieve file + let file = this.items[ normalized_path ] + + // File found + if( typeof file !== 'undefined' ) + { + return file + } + + // Force creation + if( _force_creation ) + { + file = this.create( normalized_path ) + return file + } + + // Not found + return false + } + + update( _path, _content ) + { + // Set up + let normalized_path = paths.normalize( _path ) + + // Retrieve file + let file = this.get( normalized_path, true ) + + // Create version + file.create_version( _content ) + + return file + } + + delete( _path ) + { + // Set up + let normalized_path = paths.normalize( _path ) + + // Retrieve file + let file = this.get( normalized_path ) + + // File found + if( file ) + { + // Delete file + file.destructor() + delete this.items[ normalized_path ] + + return true + } + + return false + } + + describe() + { + // Set up + let result = {} + + // Each file + for( let _file_path in this.items ) + { + let _file = this.items[ _file_path ] + + result[ _file_path ] = _file.describe() + } + + return result + } +} + +module.exports = Files diff --git a/site/models/project.class.js b/site/models/project.class.js index 1d6acdc..ce5bfc8 100644 --- a/site/models/project.class.js +++ b/site/models/project.class.js @@ -1,27 +1,16 @@ 'use strict' // Dependencies -let paths = require( '../utils/paths.class.js' ), - ids = require( '../utils/ids.class.js' ), - util = require( 'util' ), - slug = require( 'slug' ), - File = require( './file.class.js' ) +let slug = require( 'slug' ), + Files = require( './files.class.js' ) class Project { constructor( options ) { - this.name = options.name - this.slug = slug( this.name ) - this.files = {} - this.folders = { - '.': - { - name : '.', - files : {}, - folders: {} - } - } + this.name = options.name + this.slug = slug( this.name ) + this.files = new Files() this.set_socket( options.socket ) } @@ -42,283 +31,15 @@ class Project } ) } - create_file( _path, _content ) - { - // Set up - let normalized_path = paths.normalize( _path ), - parsed_path = paths.parse( normalized_path ) - - // Retrieve file - let file = this.get_file( normalized_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( { - name : parsed_path.base, - path : parsed_path.dir, - content: _content - } ) - - // Save - folder.files[ file.name ] = file.id - this.files[ file.id ] = file - - // Emit - this.socket.emit( 'update_file', file.describe() ) - this.socket.emit( 'update_folders', this.describe_folders() ) - - return file - } - - get_file( _path, _force_creation ) - { - // Params - if( typeof _force_creation === 'undefined' ) - _force_creation = false - - // Set up - let normalized_path = paths.normalize( _path ), - parsed_path = paths.parse( normalized_path ) - - // Retrieve folder - let folder = this.get_folder( parsed_path.dir, _force_creation ) - - // Folder not found - if( !folder ) - return false - - // Retrieve file - let file_id = folder.files[ parsed_path.base ] - - // File found - if( file_id ) - { - let file = this.files[ file_id ] - return file - } - - // Force creation - if( _force_creation ) - { - // Create folders - let file = this.create_file( normalized_path ) - - return file - } - - // Not found - return false - } - - update_file( _path, _content ) - { - // Set up - let normalized_path = paths.normalize( _path ) - - // Retrieve file - let file = this.get_file( normalized_path, true ) - - // Create version - file.create_version( _content ) - - // Emit - this.socket.emit( 'update_file', file.describe() ) - - return file - } - - delete_file( _path ) - { - // Set up - let parsed_path = paths.parse( _path ) - - // Retrieve folder - let folder = this.get_folder( parsed_path.dir ) - - // Folder found - if( folder ) - { - let file_id = folder.files[ parsed_path.base ] - - // File found - if( file_id ) - { - let file = this.files[ file_id ] - - // Delete file - file.destructor() - delete folder.files[ file.name ] - delete this.files[ file.id ] - - // Emit - this.socket.emit( 'delete_file', file_id ) - this.socket.emit( 'update_folders', this.describe_folders() ) - } - } - } - - create_folder( _path ) - { - // Set up - let normalized_path = paths.normalize( _path ), - path_folders = normalized_path.split( paths.separator ), - folder = this - - for( let _path_folder of path_folders ) - { - // Temporary folder - let _folder = folder.folders[ _path_folder ] - - // Folder doesn't exist - if( typeof _folder === 'undefined' ) - { - _folder = {} - _folder.name = _path_folder - _folder.files = {} - _folder.folders = {} - - folder.folders[ _path_folder ] = _folder - } - - folder = _folder - } - - // Emit - this.socket.emit( 'update_folders', this.describe_folders() ) - - return folder - } - - get_folder( _path, _force_creation ) - { - // Params - if( typeof _force_creation === 'undefined' ) - _force_creation = false - - let normalized_path = paths.normalize( _path ), - path_folders = normalized_path.split( paths.separator ), - folder = this, - found = true - - for( let _path_folder of path_folders ) - { - folder = folder.folders[ _path_folder ] - - // 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( normalized_path ) - - return folder - } - - // Not found - return false - } - - delete_folder( _path ) - { - // Set up - let normalized_path = paths.normalize( _path ), - folder = this.get_folder( normalized_path ) - - // Folder not found - if( !folder ) - return false - - // Recursive callback - let empty_folder = ( folder ) => - { - // Each folder - for( let _folder_name in folder.folders ) - { - empty_folder( folder.folders[ _folder_name ] ) - - // Delete folder - delete folder.folders[ _folder_name ] - } - - // Each file - for( let _file_name in folder.files ) - { - // Set up - let file_id = folder.files[ _file_name ], - file = this.files[ file_id ] - - // File found - if( file ) - { - // Delete file - this.delete_file( file.path.full ) - } - } - } - - empty_folder( folder ) - - // Delete folder - let parsed_path = paths.parse( normalized_path ), - parent_folder = this.get_folder( parsed_path.dir ) - - if( parent_folder ) - delete parent_folder.folders[ folder.name ] - - // Emit - this.socket.emit( 'update_folders', this.describe_folders() ) - - return true - } - describe() { // Set up let result = {} - result.name = this.name - result.folders = this.describe_folders() - result.files = this.describe_files() + result.name = this.name + result.files = this.files.describe() return result } - - describe_files() - { - // Set up - let result = {} - - // Each file - for( let _file_id in this.files ) - { - let _file = this.files[ _file_id ] - - result[ _file_id ] = _file.describe() - } - - return result - } - - describe_folders() - { - return this.folders - } } module.exports = Project