diff options
author | Olivier Paroz <github@oparoz.com> | 2015-08-18 04:16:49 +0300 |
---|---|---|
committer | Olivier Paroz <github@oparoz.com> | 2015-08-18 04:16:49 +0300 |
commit | a5c1eb0a9b46c2463011f27a3edd58fbf4f32876 (patch) | |
tree | 589bbde528de6a725669693847e4a63fc5e37c6d /utility | |
parent | b9035581998136c19cf4bcb31fedd7f244d48bec (diff) |
Add Thumbnails API
Diffstat (limited to 'utility')
-rw-r--r-- | utility/eventsource.php | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/utility/eventsource.php b/utility/eventsource.php new file mode 100644 index 00000000..63bf1d72 --- /dev/null +++ b/utility/eventsource.php @@ -0,0 +1,95 @@ +<?php +/** + * ownCloud - gallery + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Bart Visscher <bartv@thisnet.nl> + * @author Felix Moeller <mail@felixmoeller.de> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin Appelman <icewind@owncloud.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author Vincent Petry <pvince81@owncloud.com> + * @author Olivier Paroz <owncloud@interfasys.ch> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @copyright Olivier Paroz 2015 + */ + +namespace OCA\Gallery\Utility; + +/** + * Class EventSource + * + * Wrapper for server side events (http://en.wikipedia.org/wiki/Server-sent_events) + * + * This version is tailored for the Gallery app, do not use elsewhere! + * + * @todo Replace with a library + * + * @package OCA\Gallery\Controller + */ +class EventSource implements \OCP\IEventSource { + + /** + * @var bool + */ + private $started = false; + + protected function init() { + if ($this->started) { + return; + } + $this->started = true; + + // prevent php output buffering, caching and nginx buffering + while (ob_get_level()) { + ob_end_clean(); + } + header('Cache-Control: no-cache'); + header('X-Accel-Buffering: no'); + header("Content-Type: text/event-stream"); + flush(); + } + + /** + * Sends a message to the client + * + * If only one parameter is given, a typeless message will be sent with that parameter as data + * + * @param string $type + * @param mixed $data + * + * @throws \BadMethodCallException + */ + public function send($type, $data = null) { + if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { + throw new \BadMethodCallException('Type needs to be alphanumeric (' . $type . ')'); + } + $this->init(); + if (is_null($data)) { + $data = $type; + $type = null; + } + + if ($type) { + echo 'event: ' . $type . PHP_EOL; + } + echo 'data: ' . json_encode($data) . PHP_EOL; + + echo PHP_EOL; + flush(); + } + + /** + * Closes the connection of the event source + * + * It's best to let the client close the stream + */ + public function close() { + $this->send( + '__internal__', 'close' + ); + } +} |