file.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack kde@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_DAV_IFile {
  23. /**
  24. * Updates the data
  25. *
  26. * The data argument is a readable stream resource.
  27. *
  28. * After a succesful put operation, you may choose to return an ETag. The
  29. * etag must always be surrounded by double-quotes. These quotes must
  30. * appear in the actual string you're returning.
  31. *
  32. * Clients may use the ETag from a PUT request to later on make sure that
  33. * when they update the file, the contents haven't changed in the mean
  34. * time.
  35. *
  36. * If you don't plan to store the file byte-by-byte, and you return a
  37. * different object on a subsequent GET you are strongly recommended to not
  38. * return an ETag, and just return null.
  39. *
  40. * @param resource $data
  41. * @return string|null
  42. */
  43. public function put($data) {
  44. OC_Filesystem::file_put_contents($this->path, $data);
  45. return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path);
  46. }
  47. /**
  48. * Returns the data
  49. *
  50. * @return string
  51. */
  52. public function get() {
  53. return OC_Filesystem::fopen($this->path, 'rb');
  54. }
  55. /**
  56. * Delete the current file
  57. *
  58. * @return void
  59. */
  60. public function delete() {
  61. OC_Filesystem::unlink($this->path);
  62. }
  63. /**
  64. * Returns the size of the node, in bytes
  65. *
  66. * @return int
  67. */
  68. public function getSize() {
  69. $this->getFileinfoCache();
  70. return $this->fileinfo_cache['size'];
  71. }
  72. /**
  73. * Returns the ETag for a file
  74. *
  75. * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
  76. * The ETag is an arbritrary string, but MUST be surrounded by double-quotes.
  77. *
  78. * Return null if the ETag can not effectively be determined
  79. *
  80. * @return mixed
  81. */
  82. public function getETag() {
  83. $properties = $this->getProperties(array(self::GETETAG_PROPERTYNAME));
  84. if (isset($properties[self::GETETAG_PROPERTYNAME])) {
  85. return $properties[self::GETETAG_PROPERTYNAME];
  86. }
  87. return $this->getETagPropertyForPath($this->path);
  88. }
  89. /**
  90. * Creates a ETag for this path.
  91. * @param string $path Path of the file
  92. * @return string|null Returns null if the ETag can not effectively be determined
  93. */
  94. static protected function createETag($path) {
  95. return OC_Filesystem::hash('md5', $path);
  96. }
  97. /**
  98. * Returns the mime-type for a file
  99. *
  100. * If null is returned, we'll assume application/octet-stream
  101. *
  102. * @return mixed
  103. */
  104. public function getContentType() {
  105. if (isset($this->fileinfo_cache['mimetype'])) {
  106. return $this->fileinfo_cache['mimetype'];
  107. }
  108. return OC_Filesystem::getMimeType($this->path);
  109. }
  110. }