Calendar & Contacts: Store import progress in OC_Cache

Convert calendar and contacts import to use a caching system
for storing the import progress percentage. OC_Cache can later
be made smarter about storing values.
remotes/origin/stable45
Bart Visscher 14 years ago
parent dfc90021ca
commit 091b343d5c
  1. 20
      apps/calendar/ajax/import/import.php
  2. 2
      apps/calendar/import_tmp/Info
  3. 12
      apps/calendar/js/loader.js
  4. 2
      apps/calendar/templates/part.import.php
  5. 21
      apps/contacts/import.php
  6. 2
      apps/contacts/import_tmp/Info
  7. 12
      apps/contacts/js/loader.js
  8. 2
      apps/contacts/templates/part.import.php
  9. 40
      lib/cache.php
  10. 50
      lib/cache/file.php

@ -10,18 +10,22 @@ ob_start();
OCP\JSON::checkLoggedIn();
OCP\App::checkAppEnabled('calendar');
session_write_close();
$nl="\r\n";
$comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true);
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
global $progresskey;
$progresskey = 'calendar.import-' . $_GET['progresskey'];
if (isset($_GET['progress']) && $_GET['progress']) {
echo OC_Cache::get($progresskey);
die;
}
function writeProgress($pct) {
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, $pct);
fclose($progressfopen);
}
global $progresskey;
OC_Cache::set($progresskey, $pct, 300);
}
writeProgress('10');
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
@ -114,7 +118,5 @@ foreach($uids as $uid) {
// finished import
writeProgress('100');
sleep(3);
if(is_writable('import_tmp/')){
unlink($progressfile);
}
OC_Cache::remove($progresskey);
OCP\JSON::success();

@ -1,2 +0,0 @@
This folder contains static files with the percentage of the import.
Requires write permission

@ -43,8 +43,8 @@ Calendar_Import={
}
$('#newcalendar').attr('readonly', 'readonly');
$('#calendar').attr('disabled', 'disabled');
var progressfile = $('#progressfile').val();
$.post(OC.filePath('calendar', 'ajax/import', 'import.php'), {method: String (method), calname: String (calname), path: String (path), file: String (filename), id: String (calid)}, function(data){
var progresskey = $('#progresskey').val();
$.post(OC.filePath('calendar', 'ajax/import', 'import.php') + '?progresskey='+progresskey, {method: String (method), calname: String (calname), path: String (path), file: String (filename), id: String (calid)}, function(data){
if(data.status == 'success'){
$('#progressbar').progressbar('option', 'value', 100);
$('#import_done').css('display', 'block');
@ -52,7 +52,7 @@ Calendar_Import={
});
$('#form_container').css('display', 'none');
$('#progressbar_container').css('display', 'block');
window.setTimeout('Calendar_Import.getimportstatus(\'' + progressfile + '\')', 500);
window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500);
});
$('#calendar').change(function(){
if($('#calendar option:selected').val() == 'newcal'){
@ -62,11 +62,11 @@ Calendar_Import={
}
});
},
getimportstatus: function(progressfile){
$.get(OC.filePath('calendar', 'import_tmp', progressfile), function(percent){
getimportstatus: function(progresskey){
$.get(OC.filePath('calendar', 'ajax/import', 'import.php') + '?progress=1&progresskey=' + progresskey, function(percent){
$('#progressbar').progressbar('option', 'value', parseInt(percent));
if(percent < 100){
window.setTimeout('Calendar_Import.getimportstatus(\'' + progressfile + '\')', 500);
window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500);
}else{
$('#import_done').css('display', 'block');
}

@ -2,7 +2,7 @@
<div id="form_container">
<input type="hidden" id="filename" value="<?php echo $_['filename'];?>">
<input type="hidden" id="path" value="<?php echo $_['path'];?>">
<input type="hidden" id="progressfile" value="<?php echo md5(session_id()) . '.txt';?>">
<input type="hidden" id="progresskey" value="<?php echo rand() ?>">
<p style="text-align:center;"><b><?php echo $l->t('Please choose the calendar'); ?></b></p>
<select style="width:100%;" id="calendar" name="calendar">
<?php

@ -10,16 +10,21 @@ ob_start();
OCP\JSON::checkLoggedIn();
OCP\App::checkAppEnabled('contacts');
session_write_close();
$nl = "\n";
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
global $progresskey;
$progresskey = 'contacts.import-' . $_GET['progresskey'];
if (isset($_GET['progress']) && $_GET['progress']) {
echo OC_Cache::get($progresskey);
die;
}
function writeProgress($pct) {
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, $pct);
fclose($progressfopen);
}
global $progresskey;
OC_Cache::set($progresskey, $pct, 300);
}
writeProgress('10');
$view = $file = null;
@ -93,9 +98,7 @@ foreach($parts as $part){
//done the import
writeProgress('100');
sleep(3);
if(is_writable('import_tmp/')){
unlink($progressfile);
}
OC_Cache::remove($progresskey);
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
if(!$view->unlink('/' . $_POST['file'])) {
OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);

@ -1,2 +0,0 @@
This folder contains static files with the percentage of the import.
Requires write permission

@ -42,8 +42,8 @@ Contacts_Import={
}
$('#newaddressbook').attr('readonly', 'readonly');
$('#contacts').attr('disabled', 'disabled');
var progressfile = $('#progressfile').val();
$.post(OC.filePath('contacts', '', 'import.php'), {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){
var progresskey = $('#progresskey').val();
$.post(OC.filePath('contacts', '', 'import.php') + '?progresskey='+progresskey, {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){
if(jsondata.status == 'success'){
$('#progressbar').progressbar('option', 'value', 100);
$('#import_done').find('p').html(t('contacts', 'Result: ') + jsondata.data.imported + t('contacts', ' imported, ') + jsondata.data.failed + t('contacts', ' failed.'));
@ -55,7 +55,7 @@ Contacts_Import={
});
$('#form_container').css('display', 'none');
$('#progressbar_container').css('display', 'block');
window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);
window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500);
});
$('#contacts').change(function(){
if($('#contacts option:selected').val() == 'newaddressbook'){
@ -65,11 +65,11 @@ Contacts_Import={
}
});
},
getimportstatus: function(progressfile){
$.get(OC.filePath('contacts', 'import_tmp', progressfile), function(percent){
getimportstatus: function(progresskey){
$.get(OC.filePath('contacts', '', 'import.php') + '?progress=1&progresskey=' + progresskey, function(percent){
$('#progressbar').progressbar('option', 'value', parseInt(percent));
if(percent < 100){
window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);
window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500);
}else{
$('#import_done').css('display', 'block');
}

@ -2,7 +2,7 @@
<div id="form_container">
<input type="hidden" id="filename" value="<?php echo $_['filename'];?>">
<input type="hidden" id="path" value="<?php echo $_['path'];?>">
<input type="hidden" id="progressfile" value="<?php echo md5(session_id()) . '.txt';?>">
<input type="hidden" id="progresskey" value="<?php echo rand() ?>">
<p class="bold" style="text-align:center;"><?php echo $l->t('Please choose the addressbook'); ?></p>
<select style="width:100%;" id="contacts" name="contacts">
<?php

@ -0,0 +1,40 @@
<?php
/**
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_Cache {
static protected $cache;
static protected function init() {
self::$cache = new OC_Cache_File();
}
static public function get($key) {
if (!self::$cache) {
self::init();
}
return self::$cache->get($key);
}
static public function set($key, $value, $ttl=0) {
if (empty($key)) {
return false;
}
if (!self::$cache) {
self::init();
}
return self::$cache->set($key, $value, $ttl);
}
static public function remove($key) {
if (!self::$cache) {
self::init();
}
return self::$cache->remove($key);
}
}

50
lib/cache/file.php vendored

@ -0,0 +1,50 @@
<?php
/**
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_Cache_File {
protected function getStorage() {
if(OC_User::isLoggedIn()){
$subdir = 'cache';
$view = new OC_FilesystemView('/'.OC_User::getUser());
if(!$view->file_exists($subdir)) {
$view->mkdir($subdir);
}
return new OC_FilesystemView('/'.OC_User::getUser().'/'.$subdir);
}else{
OC_Log::write('core','Can\'t get cache storage, user not logged in', OC_Log::ERROR);
return false;
}
}
public function get($key) {
$storage = $this->getStorage();
if ($storage->is_file($key)) {
$mtime = $storage->filemtime($key);
if ($mtime < time()) {
$storage->unlink($key);
return false;
}
return $storage->file_get_contents($key);
}
return false;
}
public function set($key, $value, $ttl) {
$storage = $this->getStorage();
if ($storage->file_put_contents($key, $value)) {
return $storage->touch($key, time() + $ttl);
}
return false;
}
public function remove($key) {
$storage = $this->getStorage();
return $storage->unlink($key);
}
}
Loading…
Cancel
Save