mirror of
https://github.com/xevidos/codiad.git
synced 2025-01-03 11:42:12 +01:00
Added initial compression ability
This commit is contained in:
parent
24830cc7e8
commit
11bfd4ae77
5 changed files with 94 additions and 31 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
require_once('../../lib/diff_match_patch.php');
|
||||
require_once('../../common.php');
|
||||
require_once('./class.archive.php');
|
||||
|
||||
class Filemanager extends Common {
|
||||
|
||||
|
|
|
@ -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') );"
|
||||
},
|
||||
{
|
||||
|
|
|
@ -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':
|
||||
|
|
|
@ -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 );
|
||||
});
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue