streamer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $fh = \OC\Files\Filesystem::fopen($file, 'r');
  71. $this->addFileFromStream($fh, $internalDir . $filename, $filesize);
  72. fclose($fh);
  73. }elseif(\OC\Files\Filesystem::is_dir($file)) {
  74. $this->addDirRecursive($file, $internalDir);
  75. }
  76. }
  77. }
  78. /**
  79. * Add a file to the archive at the specified location and file name.
  80. *
  81. * @param string $stream Stream to read data from
  82. * @param string $internalName Filepath and name to be used in the archive.
  83. * @param int $size Filesize
  84. * @return bool $success
  85. */
  86. public function addFileFromStream($stream, $internalName, $size){
  87. if ($this->streamerInstance instanceof ZipStreamer) {
  88. return $this->streamerInstance->addFileFromStream($stream, $internalName);
  89. } else {
  90. return $this->streamerInstance->addFileFromStream($stream, $internalName, $size);
  91. }
  92. }
  93. /**
  94. * Add an empty directory entry to the archive.
  95. *
  96. * @param string $dirName Directory Path and name to be added to the archive.
  97. * @return bool $success
  98. */
  99. public function addEmptyDir($dirName){
  100. return $this->streamerInstance->addEmptyDir($dirName);
  101. }
  102. /**
  103. * Close the archive.
  104. * A closed archive can no longer have new files added to it. After
  105. * closing, the file is completely written to the output stream.
  106. * @return bool $success
  107. */
  108. public function finalize(){
  109. return $this->streamerInstance->finalize();
  110. }
  111. }