Use ajax to download file, OC_Dialogs for errors
This commit is contained in:
parent
73eca66a89
commit
d20eea9761
6 changed files with 102 additions and 29 deletions
63
apps/user_migrate/ajax/export.php
Normal file
63
apps/user_migrate/ajax/export.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* ownCloud - user_migrate
|
||||
*
|
||||
* @author Tom Needham
|
||||
* @copyright 2012 Tom Needham tom@owncloud.com
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
// 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'] = '';
|
||||
}
|
|
@ -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(
|
||||
|
|
27
apps/user_migrate/js/export.js
Normal file
27
apps/user_migrate/js/export.js
Normal file
|
@ -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')+'<img style="display: none;" class="loading" src="'+OC.filePath('core','img','loading.git')+'" />');
|
||||
});
|
||||
}
|
||||
}
|
||||
// End ajax
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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();
|
||||
}
|
||||
// fill template
|
||||
$tmpl = new OC_Template('user_migrate', 'settings');
|
||||
return $tmpl->fetchPage();
|
|
@ -1,8 +1,6 @@
|
|||
<form id="export" action="#" method="post">
|
||||
<fieldset class="personalblock">
|
||||
<legend><strong><?php echo $l->t('Export your user account');?></strong></legend>
|
||||
<p><?php echo $l->t('This will create a compressed file that contains your ownCloud account.');?>
|
||||
</p>
|
||||
<input type="submit" name="user_export" value="Export" />
|
||||
</fieldset>
|
||||
</form>
|
||||
<fieldset class="personalblock">
|
||||
<legend><strong><?php echo $l->t('Export your user account');?></strong></legend>
|
||||
<p><?php echo $l->t('This will create a compressed file that contains your ownCloud account.');?>
|
||||
</p>
|
||||
<button id="exportbtn">Export<img style="display: none;" class="loading" src="<?php echo OC_Helper::linkTo('core', 'img/loading.gif'); ?>" /></button>
|
||||
</fieldset>
|
||||
|
|
|
@ -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' );
|
||||
|
|
Loading…
Reference in a new issue