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.

108 lines
2.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Nmz <nemesiz@nmz.lt>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Preview;
  31. use OCP\Files\File;
  32. use OCP\Files\FileInfo;
  33. use OCP\IImage;
  34. class TXT extends ProviderV2 {
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function getMimeType(): string {
  39. return '/text\/plain/';
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function isAvailable(FileInfo $file): bool {
  45. return $file->getSize() > 0;
  46. }
  47. /**
  48. * {@inheritDoc}
  49. */
  50. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  51. $content = $file->fopen('r');
  52. if ($content === false) {
  53. return null;
  54. }
  55. $content = stream_get_contents($content,3000);
  56. //don't create previews of empty text files
  57. if (trim($content) === '') {
  58. return null;
  59. }
  60. $lines = preg_split("/\r\n|\n|\r/", $content);
  61. // Define text size of text file preview
  62. $fontSize = $maxX ? (int) ((1 / 32) * $maxX) : 5; //5px
  63. $lineSize = ceil($fontSize * 1.5);
  64. $image = imagecreate($maxX, $maxY);
  65. imagecolorallocate($image, 255, 255, 255);
  66. $textColor = imagecolorallocate($image, 0, 0, 0);
  67. $fontFile = __DIR__;
  68. $fontFile .= '/../../../core';
  69. $fontFile .= '/fonts/NotoSans-Regular.ttf';
  70. $canUseTTF = function_exists('imagettftext');
  71. foreach ($lines as $index => $line) {
  72. $index = $index + 1;
  73. $x = (int) 1;
  74. $y = (int) ($index * $lineSize);
  75. if ($canUseTTF === true) {
  76. imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line);
  77. } else {
  78. $y -= $fontSize;
  79. imagestring($image, 1, $x, $y, $line, $textColor);
  80. }
  81. if (($index * $lineSize) >= $maxY) {
  82. break;
  83. }
  84. }
  85. $imageObject = new \OC_Image();
  86. $imageObject->setResource($image);
  87. return $imageObject->valid() ? $imageObject : null;
  88. }
  89. }