PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/184.168.182.1/wp-content/plugins/updraftplus/oc/rs/lib/OpenCloud/ObjectStore/Resource/DataObject.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 416 lines | 218 code | 58 blank | 140 comment | 13 complexity | 33b033764f31af2e92af1de337ae5505 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP OpenCloud library.
  4. *
  5. * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6. * @license https://www.apache.org/licenses/LICENSE-2.0
  7. * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
  8. * @author Glen Campbell <glen.campbell@rackspace.com>
  9. */
  10. namespace OpenCloud\ObjectStore\Resource;
  11. use Guzzle\Http\EntityBody;
  12. use Guzzle\Http\Message\Response;
  13. use Guzzle\Http\Url;
  14. use OpenCloud\Common\Lang;
  15. use OpenCloud\Common\Exceptions;
  16. use OpenCloud\ObjectStore\Constants\UrlType;
  17. /**
  18. * Objects are the basic storage entities in Cloud Files. They represent the
  19. * files and their optional metadata you upload to the system. When you upload
  20. * objects to Cloud Files, the data is stored as-is (without compression or
  21. * encryption) and consists of a location (container), the object's name, and
  22. * any metadata you assign consisting of key/value pairs.
  23. */
  24. class DataObject extends AbstractResource
  25. {
  26. const METADATA_LABEL = 'Object';
  27. /**
  28. * @var Container
  29. */
  30. private $container;
  31. /**
  32. * @var The file name of the object
  33. */
  34. protected $name;
  35. /**
  36. * @var EntityBody
  37. */
  38. protected $content;
  39. /**
  40. * @var bool Whether or not this object is a "pseudo-directory"
  41. * @link http://docs.openstack.org/trunk/openstack-object-storage/developer/content/pseudo-hierarchical-folders-directories.html
  42. */
  43. protected $directory = false;
  44. /**
  45. * @var string The object's content type
  46. */
  47. protected $contentType;
  48. /**
  49. * @var The size of this object.
  50. */
  51. protected $contentLength;
  52. /**
  53. * @var string Date of last modification.
  54. */
  55. protected $lastModified;
  56. /**
  57. * @var string Etag.
  58. */
  59. protected $etag;
  60. /**
  61. * Also need to set Container parent and handle pseudo-directories.
  62. * {@inheritDoc}
  63. *
  64. * @param Container $container
  65. * @param null $data
  66. */
  67. public function __construct(Container $container, $data = null)
  68. {
  69. $this->setContainer($container);
  70. parent::__construct($container->getService());
  71. // For pseudo-directories, we need to ensure the name is set
  72. if (!empty($data->subdir)) {
  73. $this->setName($data->subdir)->setDirectory(true);
  74. return;
  75. }
  76. $this->populate($data);
  77. }
  78. /**
  79. * A collection list of DataObjects contains a different data structure than the one returned for the
  80. * "Retrieve Object" operation. So we need to stock the values differently.
  81. * {@inheritDoc}
  82. */
  83. public function populate($info, $setObjects = true)
  84. {
  85. parent::populate($info, $setObjects);
  86. if (isset($info->bytes)) {
  87. $this->setContentLength($info->bytes);
  88. }
  89. if (isset($info->last_modified)) {
  90. $this->setLastModified($info->last_modified);
  91. }
  92. if (isset($info->content_type)) {
  93. $this->setContentType($info->content_type);
  94. }
  95. if (isset($info->hash)) {
  96. $this->setEtag($info->hash);
  97. }
  98. }
  99. /**
  100. * Takes a response and stocks common values from both the body and the headers.
  101. *
  102. * @param Response $response
  103. * @return $this
  104. */
  105. public function populateFromResponse(Response $response)
  106. {
  107. $this->content = $response->getBody();
  108. $headers = $response->getHeaders();
  109. return $this->setMetadata($headers, true)
  110. ->setContentType((string) $headers['Content-type'])
  111. ->setLastModified((string) $headers['Last-Modified'])
  112. ->setContentLength((string) $headers['Content-Length'])
  113. ->setEtag((string) $headers['ETag']);
  114. }
  115. public function refresh()
  116. {
  117. $response = $this->getService()->getClient()
  118. ->get($this->getUrl())
  119. ->send();
  120. return $this->populateFromResponse($response);
  121. }
  122. /**
  123. * @param Container $container
  124. * @return $this
  125. */
  126. public function setContainer(Container $container)
  127. {
  128. $this->container = $container;
  129. return $this;
  130. }
  131. /**
  132. * @return Container
  133. */
  134. public function getContainer()
  135. {
  136. return $this->container;
  137. }
  138. /**
  139. * @param $name string
  140. * @return $this
  141. */
  142. public function setName($name)
  143. {
  144. $this->name = $name;
  145. return $this;
  146. }
  147. /**
  148. * @return string
  149. */
  150. public function getName()
  151. {
  152. return $this->name;
  153. }
  154. /**
  155. * @param $directory bool
  156. * @return $this
  157. */
  158. public function setDirectory($directory)
  159. {
  160. $this->directory = $directory;
  161. return $this;
  162. }
  163. /**
  164. * @return bool
  165. */
  166. public function getDirectory()
  167. {
  168. return $this->directory;
  169. }
  170. /**
  171. * @return bool Is this data object a pseudo-directory?
  172. */
  173. public function isDirectory()
  174. {
  175. return (bool) $this->directory;
  176. }
  177. /**
  178. * @param mixed $content
  179. * @return $this
  180. */
  181. public function setContent($content)
  182. {
  183. $this->content = EntityBody::factory($content);
  184. return $this;
  185. }
  186. /**
  187. * @return EntityBody
  188. */
  189. public function getContent()
  190. {
  191. return $this->content;
  192. }
  193. /**
  194. * @param string $contentType
  195. * @return $this
  196. */
  197. public function setContentType($contentType)
  198. {
  199. $this->contentType = $contentType;
  200. return $this;
  201. }
  202. /**
  203. * @return null|string
  204. */
  205. public function getContentType()
  206. {
  207. return $this->contentType ?: $this->content->getContentType();
  208. }
  209. /**
  210. * @param $contentType int
  211. * @return $this
  212. */
  213. public function setContentLength($contentLength)
  214. {
  215. $this->contentLength = $contentLength;
  216. return $this;
  217. }
  218. /**
  219. * @return int
  220. */
  221. public function getContentLength()
  222. {
  223. return $this->contentLength ?: $this->content->getContentLength();
  224. }
  225. /**
  226. * @param $etag
  227. * @return $this
  228. */
  229. public function setEtag($etag)
  230. {
  231. $this->etag = $etag;
  232. return $this;
  233. }
  234. /**
  235. * @return null|string
  236. */
  237. public function getEtag()
  238. {
  239. return $this->etag ?: $this->content->getContentMd5();
  240. }
  241. public function setLastModified($lastModified)
  242. {
  243. $this->lastModified = $lastModified;
  244. return $this;
  245. }
  246. public function getLastModified()
  247. {
  248. return $this->lastModified;
  249. }
  250. public function primaryKeyField()
  251. {
  252. return 'name';
  253. }
  254. public function getUrl($path = null, array $params = array())
  255. {
  256. if (!$this->name) {
  257. throw new Exceptions\NoNameError(Lang::translate('Object has no name'));
  258. }
  259. return $this->container->getUrl($this->name);
  260. }
  261. public function update($params = array())
  262. {
  263. return $this->container->uploadObject($this->name, $this->content, $this->metadata->toArray());
  264. }
  265. /**
  266. * @param string $destination Path (`container/object') of new object
  267. * @return \Guzzle\Http\Message\Response
  268. */
  269. public function copy($destination)
  270. {
  271. return $this->getService()
  272. ->getClient()
  273. ->createRequest('COPY', $this->getUrl(), array(
  274. 'Destination' => (string) $destination
  275. ))
  276. ->send();
  277. }
  278. public function delete($params = array())
  279. {
  280. return $this->getService()->getClient()->delete($this->getUrl())->send();
  281. }
  282. /**
  283. * Get a temporary URL for this object.
  284. *
  285. * @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/TempURL-d1a4450.html
  286. *
  287. * @param $expires Expiration time in seconds
  288. * @param $method What method can use this URL? (`GET' or `PUT')
  289. * @return string
  290. * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
  291. * @throws \OpenCloud\Common\Exceptions\ObjectError
  292. *
  293. */
  294. public function getTemporaryUrl($expires, $method)
  295. {
  296. $method = strtoupper($method);
  297. $expiry = time() + (int) $expires;
  298. // check for proper method
  299. if ($method != 'GET' && $method != 'PUT') {
  300. throw new Exceptions\InvalidArgumentError(sprintf(
  301. 'Bad method [%s] for TempUrl; only GET or PUT supported',
  302. $method
  303. ));
  304. }
  305. // @codeCoverageIgnoreStart
  306. if (!($secret = $this->getService()->getAccount()->getTempUrlSecret())) {
  307. throw new Exceptions\ObjectError('Cannot produce temporary URL without an account secret.');
  308. }
  309. // @codeCoverageIgnoreEnd
  310. $url = $this->getUrl();
  311. $urlPath = urldecode($url->getPath());
  312. $body = sprintf("%s\n%d\n%s", $method, $expiry, $urlPath);
  313. $hash = hash_hmac('sha1', $body, $secret);
  314. return sprintf('%s?temp_url_sig=%s&temp_url_expires=%d', $url, $hash, $expiry);
  315. }
  316. /**
  317. * Remove this object from the CDN.
  318. *
  319. * @param null $email
  320. * @return mixed
  321. */
  322. public function purge($email = null)
  323. {
  324. if (!$cdn = $this->getContainer()->getCdn()) {
  325. return false;
  326. }
  327. $url = clone $cdn->getUrl();
  328. $url->addPath($this->name);
  329. $headers = ($email !== null) ? array('X-Purge-Email' => $email) : array();
  330. return $this->getService()
  331. ->getClient()
  332. ->delete($url, $headers)
  333. ->send();
  334. }
  335. /**
  336. * @param string $type
  337. * @return bool|Url
  338. */
  339. public function getPublicUrl($type = UrlType::CDN)
  340. {
  341. $cdn = $this->container->getCdn();
  342. switch ($type) {
  343. case UrlType::CDN:
  344. $uri = $cdn->getCdnUri();
  345. break;
  346. case UrlType::SSL:
  347. $uri = $cdn->getCdnSslUri();
  348. break;
  349. case UrlType::STREAMING:
  350. $uri = $cdn->getCdnStreamingUri();
  351. break;
  352. case UrlType::IOS_STREAMING:
  353. $uri = $cdn->getIosStreamingUri();
  354. break;
  355. }
  356. return (isset($uri)) ? Url::factory($uri)->addPath($this->name) : false;
  357. }
  358. }