diff options
author | Alessandro Cosentino <cosenal@gmail.com> | 2012-11-08 12:44:35 -0500 |
---|---|---|
committer | Alessandro Cosentino <cosenal@gmail.com> | 2012-11-08 12:45:16 -0500 |
commit | 0dddb38e93b0f2c67c1c611343255ffc5a4fd2e3 (patch) | |
tree | 17009908161e7b6110537026bb69be71aab4c423 | |
parent | c2dfe049465a1e97ca643e54db6b0bbd67d22bf1 (diff) |
[News] importopml implemented via eventsource for incremental notifications to client
-rw-r--r-- | ajax/importopml.php | 39 | ||||
-rw-r--r-- | ajax/selectfromcloud.php | 2 | ||||
-rw-r--r-- | ajax/uploadopml.php | 42 | ||||
-rw-r--r-- | js/settings.js | 17 |
4 files changed, 85 insertions, 15 deletions
diff --git a/ajax/importopml.php b/ajax/importopml.php index 478cc1369..e363254c3 100644 --- a/ajax/importopml.php +++ b/ajax/importopml.php @@ -10,31 +10,47 @@ * */ +global $eventSource; + // Check if we are a user OCP\JSON::checkLoggedIn(); OCP\JSON::checkAppEnabled('news'); OCP\JSON::callCheck(); -session_write_close(); $l = OC_L10N::get('news'); function bailOut($msg) { - OCP\JSON::error(array('data' => array('message' => $msg))); + global $eventSource; + $eventSource->send('error', $msg); + $eventSource->close(); OCP\Util::writeLog('news','ajax/importopml.php: '.$msg, OCP\Util::ERROR); exit(); } -if (isset($_POST['path'])) { - $raw = file_get_contents($_POST['path']); +$eventSource=new OC_EventSource(); + +require_once 'news/opmlparser.php'; + +$source = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : ''; +$path = isset( $_REQUEST['path'] ) ? $_REQUEST['path'] : ''; + +if($path == '') { + bailOut($l->t('Empty filename')); + exit(); } -elseif (isset($_FILES['file'])) { - $raw = file_get_contents($_FILES['file']['tmp_name']); + +if($source == 'cloud') { + $raw = file_get_contents($path); +} elseif ($source == 'local') { + $storage = \OCP\Files::getStorage('news'); + $raw = $storage->file_get_contents($path); +} else { + bailOut($l->t('No source argument passed')); } -else { - bailOut($l->t('No file was submitted.')); + +if ($raw == false) { + bailOut($l->t('Error while reading file')); } - -require_once 'news/opmlparser.php'; try { $parsed = OPMLParser::parse($raw); @@ -119,5 +135,6 @@ function importList($data, $parentid) { $countsuccess = importList($data, 0); -OCP\JSON::success(array('data' => array('title'=>$parsed->getTitle(), 'count'=>$parsed->getCount(), +$eventSource->send('success', array('data' => array('title'=>$parsed->getTitle(), 'count'=>$parsed->getCount(), 'countsuccess'=>$countsuccess))); +$eventSource->close(); diff --git a/ajax/selectfromcloud.php b/ajax/selectfromcloud.php index 37d8053cb..eae8f0a81 100644 --- a/ajax/selectfromcloud.php +++ b/ajax/selectfromcloud.php @@ -41,5 +41,5 @@ if(!file_exists($localpath)) { if (file_put_contents($tmpfname, file_get_contents($localpath))) { OCP\JSON::success(array('data' => array('tmp'=>$tmpfname, 'path'=>$localpath))); } else { - bailOut(bailOut('Couldn\'t save temporary image: '.$tmpfname)); + bailOut($l->t('Couldn\'t save temporary image: ').$tmpfname); } diff --git a/ajax/uploadopml.php b/ajax/uploadopml.php new file mode 100644 index 000000000..d982e0324 --- /dev/null +++ b/ajax/uploadopml.php @@ -0,0 +1,42 @@ +<?php +/** +* ownCloud - News app +* +* @author Alessandro Cosentino +* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com> +* +* This file is licensed under the Affero General Public License version 3 or later. +* See the COPYING-README file +* +*/ + +// Check if we are a user +OCP\JSON::checkLoggedIn(); +OCP\JSON::checkAppEnabled('news'); +OCP\JSON::callCheck(); + +$l = OC_L10N::get('news'); + +function bailOut($msg) { + OCP\JSON::error(array('data' => array('message' => $msg))); + OCP\Util::writeLog('news','ajax/uploadopml.php: '.$msg, OCP\Util::ERROR); + exit(); +} + +if (isset($_POST['path'])) { + OCP\JSON::success(array('data' => array('source'=> 'cloud', 'path' => $_POST['path']))); + +} +elseif (isset($_FILES['file'])) { + $storage = \OCP\Files::getStorage('news'); + $filename = 'opmlfile.xml'; + if ($storage->fromTmpFile($_FILES['file']['tmp_name'], $filename)) { + OCP\JSON::success(array('data' => array('source'=> 'local', 'path' => $filename))); + } else { + bailOut('Could not create the temporary file'); + } + +} +else { + bailOut('No file loaded'); +} diff --git a/js/settings.js b/js/settings.js index 0f8b5bc38..ed99d8733 100644 --- a/js/settings.js +++ b/js/settings.js @@ -43,13 +43,24 @@ News.Settings={ } param = { - url: OC.filePath('news', 'ajax', 'importopml.php'), + url: OC.filePath('news', 'ajax', 'uploadopml.php'), data: ajaxData, type: 'POST', success: function(jsondata){ if (jsondata.status == 'success') { - $('#notification').html(t('files', '{n_success} out of {n_total} feeds imported successfully!', - {n_success: jsondata.data.countsuccess, n_total: jsondata.data.count})); + var eventSource=new OC.EventSource(OC.filePath('news','ajax','importopml.php'),{source:jsondata.data.source, path:jsondata.data.path}); + eventSource.listen('progress',function(progress){ + $('#notification').html('bingo'); + //News.Objects.Menu.addNode(folderid, jsonData.data.listfeed); + //News.Objects.Menu.load(News.MenuNodeType.Feed, jsonData.data.feedid); + }); + eventSource.listen('success',function(data){ + $('#notification').html('done'); + }); + eventSource.listen('error',function(error){ + $('#notification').fadeOut('400'); + OC.dialogs.alert(error, t('news', 'Error')); + }); } else { OC.dialogs.alert(jsondata.data.message, t('news', 'Error')); |