file.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // mark file as partial while uploading (ignored by the scanner)
  45. $partpath = $this->path . '.part';
  46. \OC\Files\Filesystem::file_put_contents($partpath, $data);
  47. //detect aborted upload
  48. if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) {
  49. if (isset($_SERVER['CONTENT_LENGTH'])) {
  50. $expected = $_SERVER['CONTENT_LENGTH'];
  51. $actual = \OC\Files\Filesystem::filesize($partpath);
  52. if ($actual != $expected) {
  53. \OC\Files\Filesystem::unlink($partpath);
  54. throw new Sabre_DAV_Exception_BadRequest(
  55. 'expected filesize ' . $expected . ' got ' . $actual);
  56. }
  57. }
  58. }
  59. // rename to correct path
  60. \OC\Files\Filesystem::rename($partpath, $this->path);
  61. //allow sync clients to send the mtime along in a header
  62. $mtime = OC_Request::hasModificationTime();
  63. if ($mtime !== false) {
  64. if(\OC\Files\Filesystem::touch($this->path, $mtime)) {
  65. header('X-OC-MTime: accepted');
  66. }
  67. }
  68. return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path);
  69. }
  70. /**
  71. * Returns the data
  72. *
  73. * @return string
  74. */
  75. public function get() {
  76. return \OC\Files\Filesystem::fopen($this->path, 'rb');
  77. }
  78. /**
  79. * Delete the current file
  80. *
  81. * @return void
  82. */
  83. public function delete() {
  84. \OC\Files\Filesystem::unlink($this->path);
  85. }
  86. /**
  87. * Returns the size of the node, in bytes
  88. *
  89. * @return int
  90. */
  91. public function getSize() {
  92. $this->getFileinfoCache();
  93. return $this->fileinfo_cache['size'];
  94. }
  95. /**
  96. * Returns the ETag for a file
  97. *
  98. * An ETag is a unique identifier representing the current version of the
  99. * file. If the file changes, the ETag MUST change. The ETag is an
  100. * arbritrary string, but MUST be surrounded by double-quotes.
  101. *
  102. * Return null if the ETag can not effectively be determined
  103. *
  104. * @return mixed
  105. */
  106. public function getETag() {
  107. $properties = $this->getProperties(array(self::GETETAG_PROPERTYNAME));
  108. if (isset($properties[self::GETETAG_PROPERTYNAME])) {
  109. return $properties[self::GETETAG_PROPERTYNAME];
  110. }
  111. return null;
  112. }
  113. /**
  114. * Returns the mime-type for a file
  115. *
  116. * If null is returned, we'll assume application/octet-stream
  117. *
  118. * @return mixed
  119. */
  120. public function getContentType() {
  121. if (isset($this->fileinfo_cache['mimetype'])) {
  122. return $this->fileinfo_cache['mimetype'];
  123. }
  124. return \OC\Files\Filesystem::getMimeType($this->path);
  125. }
  126. }