/** * Method call overload * * Allows calling REST actions as object methods; however, you must * follow-up by chaining the request with a request to an HTTP request * method (post, get, delete, put): * <code> * $response = $rest->sayHello('Foo', 'Manchu')->get(); * </code> * * Or use them together, but in sequential calls: * <code> * $rest->sayHello('Foo', 'Manchu'); * $response = $rest->get(); * </code> * * @param string $method Method name * @param array $args Method args * @return \Zend\Rest\Client\RestClient_Result|\Zend\Rest\Client\RestClient \Zend\Rest\Client\RestClient if using * a remote method, Zend_Rest_Client_Result if using an HTTP request method */ public function __call($method, $args) { $methods = array('post', 'get', 'delete', 'put'); if (in_array(strtolower($method), $methods)) { if (!isset($args[0])) { $args[0] = $this->_uri->getPath(); } $this->_data['rest'] = 1; $data = array_slice($args, 1) + $this->_data; $response = $this->{'rest' . $method}($args[0], $data); $this->_data = array(); //Initializes for next Rest method. return new Result($response->getBody()); } else { // More than one arg means it's definitely a Zend_Rest_Server if (sizeof($args) == 1) { // Uses first called function name as method name if (!isset($this->_data['method'])) { $this->_data['method'] = $method; $this->_data['arg1'] = $args[0]; } $this->_data[$method] = $args[0]; } else { $this->_data['method'] = $method; if (sizeof($args) > 0) { foreach ($args as $key => $arg) { $key = 'arg' . $key; $this->_data[$key] = $arg; } } } return $this; } }
/** * Ingest from a URL. * * Accepts the following non-prefixed keys: * * + ingest_url: (required) The URL to ingest. The idea is that some URLs * contain sensitive data that should not be saved to the database, such * as private keys. To preserve the URL, remove sensitive data from the * URL and set it to o:source. * + store_original: (optional, default true) Whether to store an original * file. This is helpful when you want the media to have thumbnails but do * not need the original file. * * {@inheritDoc} */ public function ingest(Media $media, Request $request, ErrorStore $errorStore) { $data = $request->getContent(); if (!isset($data['ingest_url'])) { $errorStore->addError('error', 'No ingest URL specified'); return; } $uri = new HttpUri($data['ingest_url']); if (!($uri->isValid() && $uri->isAbsolute())) { $errorStore->addError('ingest_url', 'Invalid ingest URL'); return; } $file = $this->getServiceLocator()->get('Omeka\\File'); $file->setSourceName($uri->getPath()); $this->downloadFile($uri, $file->getTempPath()); $fileManager = $this->getServiceLocator()->get('Omeka\\File\\Manager'); $hasThumbnails = $fileManager->storeThumbnails($file); $media->setHasThumbnails($hasThumbnails); if (!isset($data['store_original']) || $data['store_original']) { $fileManager->storeOriginal($file); $media->setHasOriginal(true); } $media->setFilename($file->getStorageName()); $media->setMediaType($file->getMediaType()); if (!array_key_exists('o:source', $data)) { $media->setSource($uri); } }
public function ingest(Media $media, Request $request, ErrorStore $errorStore) { $data = $request->getContent(); if (!isset($data['o:source'])) { $errorStore->addError('o:source', 'No YouTube URL specified'); return; } $uri = new HttpUri($data['o:source']); if (!($uri->isValid() && $uri->isAbsolute())) { $errorStore->addError('o:source', 'Invalid YouTube URL specified'); return; } switch ($uri->getHost()) { case 'www.youtube.com': if ('/watch' !== $uri->getPath()) { $errorStore->addError('o:source', 'Invalid YouTube URL specified, missing "/watch" path'); return; } $query = $uri->getQueryAsArray(); if (!isset($query['v'])) { $errorStore->addError('o:source', 'Invalid YouTube URL specified, missing "v" parameter'); return; } $youtubeId = $query['v']; break; case 'youtu.be': $youtubeId = substr($uri->getPath(), 1); break; default: $errorStore->addError('o:source', 'Invalid YouTube URL specified, not a YouTube URL'); return; } $fileManager = $this->getServiceLocator()->get('Omeka\\File\\Manager'); $file = $this->getServiceLocator()->get('Omeka\\File'); $url = sprintf('http://img.youtube.com/vi/%s/0.jpg', $youtubeId); $this->downloadFile($url, $file->getTempPath()); $hasThumbnails = $fileManager->storeThumbnails($file); $media->setData(['id' => $youtubeId, 'start' => $request->getValue('start'), 'end' => $request->getValue('end')]); if ($hasThumbnails) { $media->setFilename($file->getStorageName()); $media->setHasThumbnails(true); } }
/** * Send request to the remote server with streaming support. * * @param string $method * @param \Zend\Uri\Http $uri * @param string $http_ver * @param array $headers * @param string $body * @return string Request as string */ public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '') { // Make sure we're properly connected if (!$this->socket) { throw new Adapter\Exception('Trying to write but we are not connected'); } $host = $uri->getHost(); $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host; if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) { throw new Adapter\Exception('Trying to write but we are connected to the wrong host'); } // Save request method for later $this->method = $method; // Build request headers $path = $uri->getPath(); if ($uri->getQuery()) { $path .= '?' . $uri->getQuery(); } $request = "{$method} {$path} HTTP/{$http_ver}\r\n"; foreach ($headers as $k => $v) { if (is_string($k)) { $v = ucfirst($k) . ": {$v}"; } $request .= "{$v}\r\n"; } // Send the headers over $request .= "\r\n"; if (!@fwrite($this->socket, $request)) { throw new Adapter\Exception('Error writing request to server'); } //read from $body, write to socket $chunk = $body->read(self::CHUNK_SIZE); while ($chunk !== FALSE) { if (!@fwrite($this->socket, $chunk)) { throw new Adapter\Exception('Error writing request to server'); } $chunk = $body->read(self::CHUNK_SIZE); } $body->closeFileHandle(); return 'Large upload, request is not cached.'; }