From 11bfd4ae77824c2f1a93ed6fb6e902b1d027dc25 Mon Sep 17 00:00:00 2001 From: xevidos Date: Sun, 22 Sep 2019 22:40:02 -0400 Subject: [PATCH] Added initial compression ability --- components/filemanager/class.archive.php | 69 ++++++++++++++++++-- components/filemanager/class.filemanager.php | 1 + components/filemanager/context_menu.json | 4 +- components/filemanager/controller.php | 20 +++++- components/filemanager/init.js | 31 +++------ 5 files changed, 94 insertions(+), 31 deletions(-) diff --git a/components/filemanager/class.archive.php b/components/filemanager/class.archive.php index 5518769..35f3a29 100644 --- a/components/filemanager/class.archive.php +++ b/components/filemanager/class.archive.php @@ -69,9 +69,43 @@ class Archive { return $system; } - public static function compress( $path, $type = "zip" ) { + public static function compress( $path, $output = "default", $type = "default" ) { $response = array(); + + if( $type == "default" ) { + + $type = self:: get_supported_type(); + } + + if( $output == "default" ) { + + $output = dirname( $path ) . "/" . basename( $path ) . ".$type"; + $path_parts = pathinfo( $output ); + $existing = $output; + $i = 1; + + do { + + if( is_dir( $existing ) ) { + + $existing = rtrim( $output, "/" ) . " $i/"; + } elseif( is_file( $existing ) ) { + + if( isset( $path_parts["extension"] ) ) { + + $existing = str_replace( ".{$path_parts["extension"]}", " {$i}.{$path_parts["extension"]}", $output ); + } else { + + $existing = $output . " $i"; + } + } + $i++; + } while( is_file( $existing ) || is_dir( $existing ) ); + + $output = $existing; + } + $supported = self::supports( $type ); $archive = self::get_instance(); @@ -79,10 +113,10 @@ class Archive { if( extension_loaded( self::EXTENSIONS["{$type}"] ) ) { - $response = call_user_func( array( $archive, "{$type}_c" ), $path ); + $response = call_user_func( array( $archive, "{$type}_c" ), $path, $output ); } else { - $response = $archive->execute( $type, "compress" ); + //$response = $archive->execute( $type, "compress", $path, dirname( $path ) ); } } else { @@ -91,7 +125,7 @@ class Archive { return $response; } - public static function decompress( $file, $path = null ) { + public static function decompress( $file, $output = "default" ) { $type = filetype( $file ); $response = array(); @@ -114,6 +148,28 @@ class Archive { return $response; } + public static function get_supported_type() { + + //zip is usually the most used format supported by the most OS's, + //we check that first then check the rest of the types. + + $supported_type = null; + $types = self::SUPPORTED_TYPES; + $zip_id = array_search( "zip", $types ); + unset( $types[$zip_id] ); + array_unshift( $types, "zip" ); + + foreach( $types as $id => $type ) { + + if( self::supports( $type ) ) { + + $supported_type = $type; + break; + } + } + return $supported_type; + } + public static function supports( $type ) { $response = array(); @@ -124,10 +180,12 @@ class Archive { $system = self::get_system(); $supported = false; $extension = self::EXTENSIONS["{$type}"]; - $command = self::COMMANDS["{$system}"]["{$type}"]; if( extension_loaded( $extension ) ) { + $type_supported = true; + } elseif( isset( self::COMMANDS["{$type}"] ) && isset( self::COMMANDS["{$type}"]["compress"][$system] ) ) { + $type_supported = true; } @@ -157,6 +215,7 @@ class Archive { $archive = new ZipArchive(); if( $archive->open( $output, ZIPARCHIVE::CREATE ) !== true ) { + echo var_dump( $path, $output ); return false; } } diff --git a/components/filemanager/class.filemanager.php b/components/filemanager/class.filemanager.php index aa8dc0c..b5f668d 100755 --- a/components/filemanager/class.filemanager.php +++ b/components/filemanager/class.filemanager.php @@ -8,6 +8,7 @@ require_once('../../lib/diff_match_patch.php'); require_once('../../common.php'); +require_once('./class.archive.php'); class Filemanager extends Common { diff --git a/components/filemanager/context_menu.json b/components/filemanager/context_menu.json index 4da5b6e..0a81862 100755 --- a/components/filemanager/context_menu.json +++ b/components/filemanager/context_menu.json @@ -13,8 +13,8 @@ }, { "title": "Archive", - "icon": "icon-folder", - "applies-to" : "directory-only", + "icon": "icon-archive", + "applies-to" : "directory-only non-root", "onclick": "codiad.filemanager.archive( $('#context-menu').attr('data-path') );" }, { diff --git a/components/filemanager/controller.php b/components/filemanager/controller.php index f80c95b..e82422e 100755 --- a/components/filemanager/controller.php +++ b/components/filemanager/controller.php @@ -86,7 +86,6 @@ if( isset( $_GET["destination"] ) ) { ////////////////////////////////////////////////////////////////// $Filemanager = new Filemanager(); -$Archive = new Archive(); switch( $action ) { @@ -97,7 +96,24 @@ switch( $action ) { exit( formatJSEND( "error", "No path specified." ) ); } - //$Archive->compress( ); + if( ! Permissions::check_access( "create", $access ) ) { + + exit( formatJSEND( "error", "Invalid access to create archive." ) ); + } + + $Archive = new Archive(); + $path = $Filemanager->formatPath( $_GET["path"] ); + $result = $Archive->compress( $path ); + + if( $result ) { + + $response = formatJSEND( "success", null ); + } else { + + $response = formatJSEND( "error", "Could not create archive." ); + } + + exit( $response ); break; case 'create': diff --git a/components/filemanager/init.js b/components/filemanager/init.js index 647227d..d93914c 100755 --- a/components/filemanager/init.js +++ b/components/filemanager/init.js @@ -106,11 +106,13 @@ break; case 'root': $( '#context-menu .directory-only, #context-menu .root-only' ).show(); + $( '#context-menu .non-root' ).hide(); break; case 'editor': $( '#context-menu .editor-only' ).show(); break; } + if( codiad.project.isAbsPath( $( '#file-manager a[data-type="root"]' ).attr( 'data-path' ) ) ) { $( '#context-menu .no-external' ).hide(); } else if( type == "editor" ) { @@ -169,31 +171,16 @@ archive: function( path ) { + let _this = this; + $.get( _this.controller + '?action=archive&path=' + encodeURIComponent( path ), function( data ) { console.log( data ); - - var deleteResponse = codiad.jsend.parse( data ); - if( deleteResponse != 'error' ) { - var node = $( '#file-manager a[data-path="' + path + '"]' ); - let parent_path = node.parent().parent().prev().attr( 'data-path' ); - node.parent( 'li' ).remove(); - // Close any active files - $( '#active-files a' ) - .each( function() { - var curPath = $( this ) - .attr( 'data-path' ); - if( curPath.indexOf( path ) == 0 ) { - codiad.active.remove( curPath ); - } - }); - /* Notify listeners. */ - amplify.publish( 'filemanager.onDelete', { - deletePath: path, - path: parent_path - }); - } - codiad.modal.unload(); + let response = codiad.jsend.parse( data ); + parent = path.split( '/' ); + parent.pop(); + _this.rescan( parent.join( '/' ) ); + console.log( response ); }); },