Continued work on new upload system

This commit is contained in:
xevidos 2019-11-23 22:56:19 -05:00
parent 167cd673a7
commit 988dde4a81
4 changed files with 191 additions and 112 deletions

View file

@ -1572,7 +1572,7 @@
codiad.modal.load( codiad.modal.load(
400, 400,
'components/editor/dialog.php?action=sort' 'components/editor/dialog.php?action=sort',
{}, {},
function( c ) { function( c ) {

View file

@ -736,40 +736,47 @@ class Filemanager extends Common {
// UPLOAD (Handles uploads to the specified directory) // UPLOAD (Handles uploads to the specified directory)
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
public function upload( $path ) { public function upload( $path, $blob ) {
// Check that the path is a directory // Check that the path is a directory
if( ! Permissions::has_write( $path ) ) {
$response["status"] = "error";
$response["message"] = "You do not have access to write to this file.";
return $response;
}
if( ! common::isAbsPath( $path ) ) {
$path = WORKSPACE . "/$path";
}
$response = array( $response = array(
"status" => "none", "status" => "none",
"message" => "", "message" => "",
"files" => array(),
); );
if ( is_file( $path ) ) { $dirname = dirname( $path );
$name = basename( $path );
if( ! is_dir( $dirname ) ) {
mkdir( $dirname, 0755, true );
}
$status = file_put_contents( $path, $blob, FILE_APPEND );
if( $status === false ) {
$response["status"] = "error"; $response["status"] = "error";
$response["message"] = "Path Not A Directory"; $response["message"] = "File could not be written to.";
} else { } else {
foreach( $_FILES['upload']['name'] as $key => $value ) { $response["status"] = "success";
$response["path"] = $path;
if ( ! empty( $value ) ) { $response["bytes"] = $status;
$response["message"] = "$status bytes written to file.";
$filename = $value;
$filepath = $path . "/$filename";
if ( @move_uploaded_file( $_FILES['upload']['tmp_name'][$key], $filepath ) ) {
$info[] = array(
"name" => $filename,
"size" => filesize( $filepath ),
"url" => $filepath,
"thumbnail_url" => $filepath,
"delete_url" => $filepath,
"delete_type" => 'DELETE'
);
}
}
}
} }
return $response; return $response;
} }
} }

View file

@ -46,7 +46,7 @@ if( ! isset( $_SESSION['project'] ) ) {
require_once('../project/controller.php'); require_once('../project/controller.php');
} }
if( isset( $_GET["path"] ) || isset( $_POST["path"] ) ) { if( isset( $_GET["path"] ) || isset( $_POST["path"] ) ) {
$path = isset( $_GET["path"] ) ? $_GET["path"] : $_POST["path"]; $path = isset( $_GET["path"] ) ? $_GET["path"] : $_POST["path"];
} else { } else {
@ -286,8 +286,18 @@ switch( $action ) {
break; break;
case 'upload': case 'upload':
if( ! isset( $_POST["blob"] ) ) {
$response["status"] = "error";
$response["data"] = array(
"error" => "No blob given"
);
exit( json_encode( $response ) );
}
$response = $Filemanager->upload( $path ); $blob = $_POST["blob"];
$response = $Filemanager->upload( $path, $blob );
break; break;
default: default:

View file

@ -611,7 +611,6 @@
if( total_saved == 0 ) { if( total_saved == 0 ) {
root = true;
_this.files = result; _this.files = result;
files = result; files = result;
} else { } else {
@ -647,13 +646,16 @@
files: files files: files
}); });
root = ( node.attr( 'data-type' ) == 'root' );
let plus = span.hasClass( 'plus' ); let plus = span.hasClass( 'plus' );
let minus = span.hasClass( 'minus' ); let minus = span.hasClass( 'minus' );
let trigger = ( plus || span.hasClass( 'none' ) || root || ( rescan && minus ) ); let trigger = ( plus || span.hasClass( 'none' ) || root || ( rescan && minus ) );
_this.toggle_open_close( node.parent(), trigger );
console.log( plus, span.hasClass( 'none' ), root, ( rescan && minus ) );
console.log( trigger ); console.log( trigger );
_this.toggle_open_close( node.parent(), trigger );
if( trigger ) { if( trigger ) {
_this.create_indexes( files, container ); _this.create_indexes( files, container );
@ -1454,26 +1456,30 @@
selector_listeners: function( node, limit ) { selector_listeners: function( node, limit ) {
let _this = this; let _this = codiad.filemanager;
$( node ) $( node )
.on( 'click', 'a', async function( e ) { .on( 'click', 'a', async function( e ) {
let i = $( e.target );
// Select or Expand // Select or Expand
if( codiad.editor.settings.fileManagerTrigger ) { if( codiad.editor.settings.fileManagerTrigger ) {
$( this ).addClass( 'loading' ); _this.toggle_open_close( i.parent(), null, async function() {
let result = await _this.get_indexes( $( this ).attr( 'data-path' ) );
result = codiad.jsend.parse( result ); let result = await _this.get_indexes( i.attr( 'data-path' ) );
let indexes = result.index result = codiad.jsend.parse( result );
console.log( indexes ); let indexes = result.index;
let ul = $( '<ul></ul>' );
let ul = $( '<ul></ul>' ); let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] );
let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] ); i.parent().children( 'ul' ).remove();
$( this ).removeClass( 'loading' ); i.parent().append( ul );
_this.toggle_open_close( $( this ).parent(), null ); },
$( this ).parent().children( 'ul' ).remove(); async function() {
$( this ).parent().append( ul );
i.parent().children( 'ul' ).remove();
});
} else { } else {
_this.toggle_select_node( $( e.target ), limit ); _this.toggle_select_node( $( e.target ), limit );
@ -1481,39 +1487,43 @@
}) })
.on( 'click', 'span', async function( e ) { .on( 'click', 'span', async function( e ) {
// Select or Expand let i = $( e.target ).parent().children( 'a' );
let a = $( this ).parent().children( 'a' );
a.addClass( 'loading' );
let result = await _this.get_indexes( a.attr( 'data-path' ) );
result = codiad.jsend.parse( result );
let indexes = result.index
console.log( indexes );
let ul = $( '<ul></ul>' ); _this.toggle_open_close( i.parent(), null, async function() {
let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] );
a.removeClass( 'loading' ); let result = await _this.get_indexes( i.attr( 'data-path' ) );
_this.toggle_open_close( $( this ).parent(), null ); result = codiad.jsend.parse( result );
$( this ).parent().children( 'ul' ).remove(); let indexes = result.index;
$( this ).parent().append( ul ); let ul = $( '<ul></ul>' );
let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] );
i.parent().children( 'ul' ).remove();
i.parent().append( ul );
},
async function() {
i.parent().children( 'ul' ).remove();
});
}) })
.on( 'dblclick', 'a', async function( e ) { .on( 'dblclick', 'a', async function( e ) {
let _this = codiad.filemanager let i = $( e.target );
// Select or Expand
if( ! codiad.editor.settings.fileManagerTrigger ) { if( ! codiad.editor.settings.fileManagerTrigger ) {
$( this ).addClass( 'loading' ); _this.toggle_open_close( i.parent(), null, async function() {
let result = await _this.get_indexes( $( this ).attr( 'data-path' ) );
result = codiad.jsend.parse( result ); let result = await _this.get_indexes( i.attr( 'data-path' ) );
let indexes = result.index result = codiad.jsend.parse( result );
console.log( indexes ); let indexes = result.index;
let ul = $( '<ul></ul>' );
let ul = $( '<ul></ul>' ); let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] );
let children = _this.create_indexes( indexes, ul, {type: 'directories'}, [] ); i.parent().children( 'ul' ).remove();
$( this ).removeClass( 'loading' ); i.parent().append( ul );
_this.toggle_open_close( $( this ).parent(), null ); },
$( this ).parent().children( 'ul' ).remove(); async function() {
$( this ).parent().append( ul );
i.parent().children( 'ul' ).remove();
});
} else { } else {
_this.toggle_select_node( $( e.target ), limit ); _this.toggle_select_node( $( e.target ), limit );
@ -1529,8 +1539,6 @@
let path = node.attr( 'data-path' ); let path = node.attr( 'data-path' );
let i = 1; let i = 1;
console.log( node, limit );
for( i = _this.selected.length;i--; ) { for( i = _this.selected.length;i--; ) {
if( _this.selected[i] == path ) { if( _this.selected[i] == path ) {
@ -1548,22 +1556,21 @@
} }
_this.selected = []; _this.selected = [];
_this.selected.push( path ); _this.selected.push( path );
node.css( "background", "#fff" );
} else { } else {
if( selected ) { if( selected ) {
node.css( "background", "" );
_this.selected.splice( i, 1 ); _this.selected.splice( i, 1 );
} else { } else {
_this.selected.push( path ); _this.selected.push( path );
node.css( "background", "#fff" );
} }
} }
console.log( path, _this.selected );
}, },
toggle_open_close: function( node, open ) { toggle_open_close: function( node, open, open_callback, close_callback ) {
let span = node.children( 'span' ); let span = node.children( 'span' );
let a = node.children( 'a' ); let a = node.children( 'a' );
@ -1584,6 +1591,11 @@
span.addClass( 'minus' ); span.addClass( 'minus' );
} }
a.addClass( 'open' ); a.addClass( 'open' );
if( typeof open_callback == "function" ) {
open_callback();
}
} else { } else {
span.removeClass( 'minus' ); span.removeClass( 'minus' );
@ -1596,6 +1608,11 @@
node.children( 'span' ).removeClass( 'minus' ).addClass( 'plus' ); node.children( 'span' ).removeClass( 'minus' ).addClass( 'plus' );
node.children().find( 'span' ).removeClass( 'minus' ).addClass( 'plus' ); node.children().find( 'span' ).removeClass( 'minus' ).addClass( 'plus' );
node.children().find( 'a' ).removeClass( 'open' ); node.children().find( 'a' ).removeClass( 'open' );
if( typeof close_callback == "function" ) {
close_callback();
}
}); });
} }
}, },
@ -1619,25 +1636,66 @@
destination = destination + "/"; destination = destination + "/";
} }
//form.append( "action", "upload" ); console.log( files );
console.log( destination );
for( let i = files.length;i--; ) { for( let i = files.length;i--; ) {
let entry = files[i].webkitGetAsEntry(); let entry = files[i];
console.log( entry );
if( entry.isFile ) { if( entry.isFile ) {
console.log( entry ); entry.file( function( file ) {
_this.upload_blobs( file, destination );
});
} else if( entry.isDirectory ) { } else if( entry.isDirectory ) {
_this.upload_read_directory( entry, destination ); _this.upload_read_directory( entry, destination, _this.upload_split );
} }
} }
},
upload_blob: async function( blob, path ) {
console.log( files );
console.log( destination ); },
upload_blobs: function( file, path ) {
console.log( file, path );
let _this = codiad.filemanager;
let blob_size = 1024;
let total_size = file.size;
let current = 0 + blob_size;
let reader = new FileReader();
let blob = file.slice( current, current + blob_size );
let upload_status = null;
let file_path = path;
if( path.charAt( path.length - 1 ) !== '/' ) {
file_path = file_path + '/';
}
reader.onload = async function( e ) {
upload_status = await _this.upload_blob( blob, file_path );
console.log( upload_status );
while( current <= total_size && typeof upload_status === "Boolean" ) {
console.log( ( current / total_size ) * 100 );
blob = file.slice( current, current + blob_size );
let upload_status = await _this.upload_blob( blob, file_path );
console.log( upload_status );
reader.readAsBinaryString( blob );
}
};
reader.readAsBinaryString( blob );
}, },
upload_data: { upload_data: {
@ -1648,43 +1706,20 @@
entries: [], entries: [],
}, },
upload_read_directory: function( item, path ) {
let _this = codiad.filemanager;
let files = [];
if( item.isFile ) {
// Get file
item.file( function( file ) {
console.log("File:", path + file.name);
});
} else if( item.isDirectory ) {
// Get folder contents
let dirReader = item.createReader();
dirReader.readEntries( function( entries ) {
let total = entries.length;
for( let i = 0;i < total; i++ ) {
_this.upload_read_directory( entries[i], path + item.name + "/" );
}
});
}
},
upload_drop: function( e ) { upload_drop: function( e ) {
let _this = codiad.filemanager; let _this = codiad.filemanager;
let data = null; let data = [];
let drop = $( '.drop-overlay' ); let drop = $( '.drop-overlay' );
let items = e.originalEvent.dataTransfer.items; let items = e.originalEvent.dataTransfer.items;
console.log( e ); for( let i = items.length;i--; ) {
data.push( items[i].webkitGetAsEntry() );
}
_this.upload_overlay_off(); _this.upload_overlay_off();
_this.upload( items ) _this.upload( data )
}, },
upload_overlay_off: function() { upload_overlay_off: function() {
@ -1709,6 +1744,33 @@
_this.upload_data.timers.off = setTimeout( _this.upload_overlay_off, 1500 ); _this.upload_data.timers.off = setTimeout( _this.upload_overlay_off, 1500 );
}, },
upload_read_directory: function( item, path, file_callback ) {
let _this = codiad.filemanager;
let files = [];
if( item.isFile ) {
// Get file
item.file( function( file ) {
file_callback( file, path );
});
} else if( item.isDirectory ) {
// Get folder contents
let dirReader = item.createReader();
dirReader.readEntries( function( entries ) {
let total = entries.length;
for( let i = 0;i < total; i++ ) {
_this.upload_read_directory( entries[i], path + item.name + "/", file_callback );
}
});
}
},
uploadToNode: function( path ) { uploadToNode: function( path ) {
let _this = codiad.filemanager; let _this = codiad.filemanager;