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.

110 lines
2.8 KiB

3 years ago
  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. private $options;
  31. private $tmpFiles = [];
  32. /**
  33. * Constructor
  34. *
  35. * @param array $options
  36. */
  37. public function __construct(array $options = []) {
  38. $this->options = $options;
  39. }
  40. /**
  41. * @return string Regex with the mimetypes that are supported by this provider
  42. */
  43. abstract public function getMimeType(): string ;
  44. /**
  45. * Check if a preview can be generated for $path
  46. *
  47. * @param FileInfo $file
  48. * @return bool
  49. */
  50. public function isAvailable(FileInfo $file): bool {
  51. return true;
  52. }
  53. /**
  54. * get thumbnail for file at path $path
  55. *
  56. * @param File $file
  57. * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
  58. * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
  59. * @return null|\OCP\IImage false if no preview was generated
  60. * @since 17.0.0
  61. */
  62. abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
  63. /**
  64. * Get a path to either the local file or temporary file
  65. *
  66. * @param File $file
  67. * @param int $maxSize maximum size for temporary files
  68. * @return string
  69. */
  70. protected function getLocalFile(File $file, int $maxSize = null): string {
  71. $useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
  72. if ($useTempFile) {
  73. $absPath = \OC::$server->getTempManager()->getTemporaryFile();
  74. $content = $file->fopen('r');
  75. if ($maxSize) {
  76. $content = stream_get_contents($content, $maxSize);
  77. }
  78. file_put_contents($absPath, $content);
  79. $this->tmpFiles[] = $absPath;
  80. return $absPath;
  81. } else {
  82. return $file->getStorage()->getLocalFile($file->getInternalPath());
  83. }
  84. }
  85. /**
  86. * Clean any generated temporary files
  87. */
  88. protected function cleanTmpFiles() {
  89. foreach ($this->tmpFiles as $tmpFile) {
  90. unlink($tmpFile);
  91. }
  92. $this->tmpFiles = [];
  93. }
  94. }