From: Tom Needham Date: Sat, 31 Mar 2012 22:41:43 +0000 (+0000) Subject: Use ajax to download file, OC_Dialogs for errors X-Git-Tag: v4.0.0beta~393 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d20eea9761238e0569f538c6f8b1bb553068bf7b;p=nextcloud-server.git Use ajax to download file, OC_Dialogs for errors --- diff --git a/apps/user_migrate/ajax/export.php b/apps/user_migrate/ajax/export.php new file mode 100644 index 00000000000..ef947c610f2 --- /dev/null +++ b/apps/user_migrate/ajax/export.php @@ -0,0 +1,63 @@ +. + * + */ +// Init owncloud +require_once('../../../lib/base.php'); + +// Check if we are a user +OC_JSON::checkLoggedIn(); +OC_Util::checkAppEnabled('user_migrate'); + OC_JSON::error(); + die(); +// Which operation +if( $_GET['operation']=='create' ){ +$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); +if( $uid != OC_User::getUser() ){ + // Needs to be admin to export someone elses account + OC_JSON::error(); + die(); +} +// Create the export zip +if( !$path = OC_Migrate::export( $uid ) ){ + // Error + OC_JSON::error(); + die(); +} else { + // Save path in session + $_SESSION['ocuserexportpath'] = $path; +} +OC_JSON::success(); +die(); +} else if( $_GET['operation']=='download' ){ + // Download the export + $path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false; + if( !$path ){ + die(); + } + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($path)); + header("Content-Length: " . filesize($path)); + @ob_end_clean(); + readfile($path); + unlink( $path ); + $_SESSION['ocuserexportpath'] = ''; +} diff --git a/apps/user_migrate/appinfo/app.php b/apps/user_migrate/appinfo/app.php index 18ea8f52b2a..a59b6dd705c 100644 --- a/apps/user_migrate/appinfo/app.php +++ b/apps/user_migrate/appinfo/app.php @@ -23,6 +23,7 @@ OC_APP::registerPersonal( 'user_migrate', 'settings' ); OC_APP::registerAdmin( 'user_migrate', 'admin' ); +OC_Util::addScript( 'user_migrate', 'export'); // add settings page to navigation $entry = array( diff --git a/apps/user_migrate/js/export.js b/apps/user_migrate/js/export.js new file mode 100644 index 00000000000..0e1e396f65d --- /dev/null +++ b/apps/user_migrate/js/export.js @@ -0,0 +1,27 @@ +$(document).ready(function(){ + // Do the export + $('#exportbtn').click(function(){ + // Show loader + $('.loading').show(); + $.getJSON( + OC.filePath('user_migrate','ajax','export.php'), + {operation:'create'}, + function(result){ + if(result.status == 'success'){ + // Download the file + window.location = OC.filePath('user_migrate','ajax','export.php?operation=download') ; + $('.loading').hide(); + $('#exportbtn').val(t('user_migrate', 'Export')); + } else { + // Cancel loading + $('#exportbtn').html('Failed'); + // Show Dialog + OC.dialogs.alert(t('user_migrate', 'Something went wrong while the export file was being generated'), t('user_migrate', 'An error has occurred'), function(){ + $('#exportbtn').html(t('user_migrate', 'Export')+''); + }); + } + } + // End ajax + ); + }); +}); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 38eee990b46..62f347b3557 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -24,22 +24,6 @@ */ OC_Util::checkAppEnabled('user_migrate'); -if (isset($_POST['user_export'])) { - // Create the export zip - if( !$path = OC_Migrate::export() ){ - // Error - die('error'); - } else { - // Download it - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($path)); - header("Content-Length: " . filesize($path)); - @ob_end_clean(); - readfile($path); - unlink( $path ); - } -} else { - // fill template - $tmpl = new OC_Template('user_migrate', 'settings'); - return $tmpl->fetchPage(); -} \ No newline at end of file +// fill template +$tmpl = new OC_Template('user_migrate', 'settings'); +return $tmpl->fetchPage(); \ No newline at end of file diff --git a/apps/user_migrate/templates/settings.php b/apps/user_migrate/templates/settings.php index 389de563a6f..5f4857de5fa 100644 --- a/apps/user_migrate/templates/settings.php +++ b/apps/user_migrate/templates/settings.php @@ -1,8 +1,6 @@ -
-
- t('Export your user account');?> -

t('This will create a compressed file that contains your ownCloud account.');?> -

- -
-
\ No newline at end of file +
+ t('Export your user account');?> +

t('This will create a compressed file that contains your ownCloud account.');?> +

+ +
diff --git a/lib/migrate.php b/lib/migrate.php index fe5ac456005..db3852fb832 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -73,12 +73,12 @@ class OC_Migrate{ /** * @breif exports a user, or owncloud instance + * @param optional $uid string user id of user to export if export type is user, defaults to current * @param ootional $type string type of export, defualts to user * @param otional $path string path to zip output folder - * @param optional $uid string user id of user to export if export type is user, defaults to current * @return false on error, path to zip on success */ - public static function export( $type='user', $path=null, $uid=null ){ + public static function export( $uid=null, $type='user', $path=null ){ $datadir = OC_Config::getValue( 'datadirectory' ); // Validate export type $types = array( 'user', 'instance', 'system', 'userfiles' );