You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC;
  25. use ownCloud\TarStreamer\TarStreamer;
  26. use ZipStreamer\ZipStreamer;
  27. class Streamer {
  28. // array of regexp. Matching user agents will get tar instead of zip
  29. private $preferTarFor = [ '/macintosh|mac os x/i' ];
  30. // streamer instance
  31. private $streamerInstance;
  32. public function __construct(){
  33. /** @var \OCP\IRequest */
  34. $request = \OC::$server->getRequest();
  35. if ($request->isUserAgent($this->preferTarFor)) {
  36. $this->streamerInstance = new TarStreamer();
  37. } else {
  38. $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
  39. }
  40. }
  41. /**
  42. * Send HTTP headers
  43. * @param string $name
  44. */
  45. public function sendHeaders($name){
  46. $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
  47. $fullName = $name . $extension;
  48. $this->streamerInstance->sendHeaders($fullName);
  49. }
  50. /**
  51. * Stream directory recursively
  52. * @param string $dir
  53. * @param string $internalDir
  54. */
  55. public function addDirRecursive($dir, $internalDir='') {
  56. $dirname = basename($dir);
  57. $rootDir = $internalDir . $dirname;
  58. if (!empty($rootDir)) {
  59. $this->streamerInstance->addEmptyDir($rootDir);
  60. }
  61. $internalDir .= $dirname . '/';
  62. // prevent absolute dirs
  63. $internalDir = ltrim($internalDir, '/');
  64. $files= \OC\Files\Filesystem::getDirectoryContent($dir);
  65. foreach($files as $file) {
  66. $filename = $file['name'];
  67. $file = $dir . '/' . $filename;
  68. if(\OC\Files\Filesystem::is_file($file)) {
  69. $filesize = \OC\Files\Filesystem::filesize($file);
  70. $fileTime = \OC\Files\Filesystem::filemtime($file);
  71. $fh = \OC\Files\Filesystem::fopen($file, 'r');
  72. $this->addFileFromStream($fh, $internalDir . $filename, $filesize, $fileTime);
  73. fclose($fh);
  74. }elseif(\OC\Files\Filesystem::is_dir($file)) {
  75. $this->addDirRecursive($file, $internalDir);
  76. }
  77. }
  78. }
  79. /**
  80. * Add a file to the archive at the specified location and file name.
  81. *
  82. * @param string $stream Stream to read data from
  83. * @param string $internalName Filepath and name to be used in the archive.
  84. * @param int $size Filesize
  85. * @param int|bool $time File mtime as int, or false
  86. * @return bool $success
  87. */
  88. public function addFileFromStream($stream, $internalName, $size, $time) {
  89. $options = [];
  90. if ($time) {
  91. $options = [
  92. 'timestamp' => $time
  93. ];
  94. }
  95. if ($this->streamerInstance instanceof ZipStreamer) {
  96. return $this->streamerInstance->addFileFromStream($stream, $internalName, $options);
  97. } else {
  98. return $this->streamerInstance->addFileFromStream($stream, $internalName, $size, $options);
  99. }
  100. }
  101. /**
  102. * Add an empty directory entry to the archive.
  103. *
  104. * @param string $dirName Directory Path and name to be added to the archive.
  105. * @return bool $success
  106. */
  107. public function addEmptyDir($dirName){
  108. return $this->streamerInstance->addEmptyDir($dirName);
  109. }
  110. /**
  111. * Close the archive.
  112. * A closed archive can no longer have new files added to it. After
  113. * closing, the file is completely written to the output stream.
  114. * @return bool $success
  115. */
  116. public function finalize(){
  117. return $this->streamerInstance->finalize();
  118. }
  119. }