ProviderV2.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Preview;
  25. use OCP\Files\File;
  26. use OCP\Files\FileInfo;
  27. use OCP\IImage;
  28. use OCP\Preview\IProviderV2;
  29. abstract class ProviderV2 implements IProviderV2 {
  30. /** @var array */
  31. protected $options;
  32. /** @var array */
  33. protected $tmpFiles = [];
  34. /**
  35. * Constructor
  36. *
  37. * @param array $options
  38. */
  39. public function __construct(array $options = []) {
  40. $this->options = $options;
  41. }
  42. /**
  43. * @return string Regex with the mimetypes that are supported by this provider
  44. */
  45. abstract public function getMimeType(): string ;
  46. /**
  47. * Check if a preview can be generated for $path
  48. *
  49. * @param FileInfo $file
  50. * @return bool
  51. */
  52. public function isAvailable(FileInfo $file): bool {
  53. return true;
  54. }
  55. /**
  56. * get thumbnail for file at path $path
  57. *
  58. * @param File $file
  59. * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
  60. * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
  61. * @return null|\OCP\IImage false if no preview was generated
  62. * @since 17.0.0
  63. */
  64. abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
  65. protected function useTempFile(File $file): bool {
  66. return $file->isEncrypted() || !$file->getStorage()->isLocal();
  67. }
  68. /**
  69. * Get a path to either the local file or temporary file
  70. *
  71. * @param File $file
  72. * @param int $maxSize maximum size for temporary files
  73. * @return string|false
  74. */
  75. protected function getLocalFile(File $file, int $maxSize = null) {
  76. if ($this->useTempFile($file)) {
  77. $absPath = \OC::$server->getTempManager()->getTemporaryFile();
  78. $content = $file->fopen('r');
  79. if ($maxSize) {
  80. $content = stream_get_contents($content, $maxSize);
  81. }
  82. file_put_contents($absPath, $content);
  83. $this->tmpFiles[] = $absPath;
  84. return $absPath;
  85. } else {
  86. $path = $file->getStorage()->getLocalFile($file->getInternalPath());
  87. if (is_string($path)) {
  88. return $path;
  89. } else {
  90. return false;
  91. }
  92. }
  93. }
  94. /**
  95. * Clean any generated temporary files
  96. */
  97. protected function cleanTmpFiles(): void {
  98. foreach ($this->tmpFiles as $tmpFile) {
  99. unlink($tmpFile);
  100. }
  101. $this->tmpFiles = [];
  102. }
  103. }