]> source.dussan.org Git - nextcloud-server.git/commitdiff
Calendar & Contacts: Store import progress in OC_Cache
authorBart Visscher <bartv@thisnet.nl>
Mon, 4 Jun 2012 15:42:59 +0000 (17:42 +0200)
committerBart Visscher <bartv@thisnet.nl>
Mon, 4 Jun 2012 20:24:17 +0000 (22:24 +0200)
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.

apps/calendar/ajax/import/import.php
apps/calendar/import_tmp/Info [deleted file]
apps/calendar/js/loader.js
apps/calendar/templates/part.import.php
apps/contacts/import.php
apps/contacts/import_tmp/Info [deleted file]
apps/contacts/js/loader.js
apps/contacts/templates/part.import.php
lib/cache.php [new file with mode: 0644]
lib/cache/file.php [new file with mode: 0644]

index 904c07c52e7b8986051a74ae3a7851ebf577ed9d..a3eaed844a14737d3871e44efc289b93d73961bf 100644 (file)
@@ -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();
diff --git a/apps/calendar/import_tmp/Info b/apps/calendar/import_tmp/Info
deleted file mode 100644 (file)
index abafbce..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-This folder contains static files with the percentage of the import.
-Requires write permission
index 60d92f448eee33fb1cb99689876bdd865615301f..838521ec7f55253cb7a818f28973f6cb0c3b164d 100644 (file)
@@ -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');
                        }
index e93ea1af4c934760ec43106ab498023309622bdf..39cda29c20d9e49dbfd70441eace4d833df9f3fe 100644 (file)
@@ -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
index fdea4975d59f5e5d66457a2e15e2aac8cc9d4000..0ee35f9fd81f094e54fa94950f98cba648e7deee 100644 (file)
@@ -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);
diff --git a/apps/contacts/import_tmp/Info b/apps/contacts/import_tmp/Info
deleted file mode 100644 (file)
index abafbce..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-This folder contains static files with the percentage of the import.
-Requires write permission
index 8c79ea8a1d676b9ea155d0a9c2c583b926db8379..961e0f425c9cb53ff31aadf28c2007b926a78528 100644 (file)
@@ -42,8 +42,8 @@ Contacts_Import={
                        }\r
                        $('#newaddressbook').attr('readonly', 'readonly');\r
                        $('#contacts').attr('disabled', 'disabled');\r
-                       var progressfile = $('#progressfile').val();\r
-                       $.post(OC.filePath('contacts', '', 'import.php'), {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){\r
+                       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'){\r
                                        $('#progressbar').progressbar('option', 'value', 100);\r
                                        $('#import_done').find('p').html(t('contacts', 'Result: ') + jsondata.data.imported + t('contacts', ' imported, ') + jsondata.data.failed + t('contacts', ' failed.'));\r
@@ -55,7 +55,7 @@ Contacts_Import={
                        });\r
                        $('#form_container').css('display', 'none');\r
                        $('#progressbar_container').css('display', 'block');\r
-                       window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);\r
+                       window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500);
                });\r
                $('#contacts').change(function(){\r
                        if($('#contacts option:selected').val() == 'newaddressbook'){\r
@@ -65,11 +65,11 @@ Contacts_Import={
                        }\r
                });\r
        },\r
-       getimportstatus: function(progressfile){\r
-               $.get(OC.filePath('contacts', 'import_tmp', progressfile), function(percent){\r
+       getimportstatus: function(progresskey){
+               $.get(OC.filePath('contacts', '', 'import.php') + '?progress=1&progresskey=' + progresskey, function(percent){
                        $('#progressbar').progressbar('option', 'value', parseInt(percent));\r
                        if(percent < 100){\r
-                               window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);\r
+                               window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500);
                        }else{\r
                                $('#import_done').css('display', 'block');\r
                        }\r
index b8793042997ce94c646d34c96cb74397360c4185..32c8dc50dd62004b576c5be6b11446cf523ddb9f 100644 (file)
@@ -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
diff --git a/lib/cache.php b/lib/cache.php
new file mode 100644 (file)
index 0000000..a4fb244
--- /dev/null
@@ -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);
+       }
+
+}
diff --git a/lib/cache/file.php b/lib/cache/file.php
new file mode 100644 (file)
index 0000000..02aad51
--- /dev/null
@@ -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);
+       }
+}