filechunking.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Roeland Jago Douma <rullzer@owncloud.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Thomas Tanghus <thomas@tanghus.net>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2016, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. class OC_FileChunking {
  30. protected $info;
  31. protected $cache;
  32. /**
  33. * TTL of chunks
  34. *
  35. * @var int
  36. */
  37. protected $ttl;
  38. static public function decodeName($name) {
  39. preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches);
  40. return $matches;
  41. }
  42. /**
  43. * @param string[] $info
  44. */
  45. public function __construct($info) {
  46. $this->info = $info;
  47. $this->ttl = \OC::$server->getConfig()->getSystemValue('cache_chunk_gc_ttl', 86400);
  48. }
  49. public function getPrefix() {
  50. $name = $this->info['name'];
  51. $transferid = $this->info['transferid'];
  52. return $name.'-chunking-'.$transferid.'-';
  53. }
  54. protected function getCache() {
  55. if (!isset($this->cache)) {
  56. $this->cache = new \OC\Cache\File();
  57. }
  58. return $this->cache;
  59. }
  60. /**
  61. * Stores the given $data under the given $key - the number of stored bytes is returned
  62. *
  63. * @param string $index
  64. * @param resource $data
  65. * @return int
  66. */
  67. public function store($index, $data) {
  68. $cache = $this->getCache();
  69. $name = $this->getPrefix().$index;
  70. $cache->set($name, $data, $this->ttl);
  71. return $cache->size($name);
  72. }
  73. public function isComplete() {
  74. $prefix = $this->getPrefix();
  75. $cache = $this->getCache();
  76. $chunkcount = (int)$this->info['chunkcount'];
  77. for($i=($chunkcount-1); $i >= 0; $i--) {
  78. if (!$cache->hasKey($prefix.$i)) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * Assembles the chunks into the file specified by the path.
  86. * Chunks are deleted afterwards.
  87. *
  88. * @param resource $f target path
  89. *
  90. * @return integer assembled file size
  91. *
  92. * @throws \OC\InsufficientStorageException when file could not be fully
  93. * assembled due to lack of free space
  94. */
  95. public function assemble($f) {
  96. $cache = $this->getCache();
  97. $prefix = $this->getPrefix();
  98. $count = 0;
  99. for ($i = 0; $i < $this->info['chunkcount']; $i++) {
  100. $chunk = $cache->get($prefix.$i);
  101. // remove after reading to directly save space
  102. $cache->remove($prefix.$i);
  103. $count += fwrite($f, $chunk);
  104. // let php release the memory to work around memory exhausted error with php 5.6
  105. $chunk = null;
  106. }
  107. return $count;
  108. }
  109. /**
  110. * Returns the size of the chunks already present
  111. * @return integer size in bytes
  112. */
  113. public function getCurrentSize() {
  114. $cache = $this->getCache();
  115. $prefix = $this->getPrefix();
  116. $total = 0;
  117. for ($i = 0; $i < $this->info['chunkcount']; $i++) {
  118. $total += $cache->size($prefix.$i);
  119. }
  120. return $total;
  121. }
  122. /**
  123. * Removes all chunks which belong to this transmission
  124. */
  125. public function cleanup() {
  126. $cache = $this->getCache();
  127. $prefix = $this->getPrefix();
  128. for($i=0; $i < $this->info['chunkcount']; $i++) {
  129. $cache->remove($prefix.$i);
  130. }
  131. }
  132. /**
  133. * Removes one specific chunk
  134. * @param string $index
  135. */
  136. public function remove($index) {
  137. $cache = $this->getCache();
  138. $prefix = $this->getPrefix();
  139. $cache->remove($prefix.$index);
  140. }
  141. /**
  142. * Assembles the chunks into the file specified by the path.
  143. * Also triggers the relevant hooks and proxies.
  144. *
  145. * @param \OC\Files\Storage\Storage $storage storage
  146. * @param string $path target path relative to the storage
  147. * @return bool true on success or false if file could not be created
  148. *
  149. * @throws \OC\ServerNotAvailableException
  150. */
  151. public function file_assemble($storage, $path) {
  152. // use file_put_contents as method because that best matches what this function does
  153. if (\OC\Files\Filesystem::isValidPath($path)) {
  154. $target = $storage->fopen($path, 'w');
  155. if ($target) {
  156. $count = $this->assemble($target);
  157. fclose($target);
  158. return $count > 0;
  159. } else {
  160. return false;
  161. }
  162. }
  163. return false;
  164. }
  165. }