aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Preview/Bundled.php
blob: 836dc4bd3572b590e3df675798c1e5fdcb71aaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Preview;

use OC\Archive\ZIP;
use OCP\Files\File;
use OCP\IImage;

/**
 * Extracts a preview from files that embed them in an ZIP archive
 */
abstract class Bundled extends ProviderV2 {
	protected function extractThumbnail(File $file, string $path): ?IImage {
		if ($file->getSize() === 0) {
			return null;
		}

		$sourceTmp = \OC::$server->getTempManager()->getTemporaryFile();
		$targetTmp = \OC::$server->getTempManager()->getTemporaryFile();
		$this->tmpFiles[] = $sourceTmp;
		$this->tmpFiles[] = $targetTmp;

		try {
			$content = $file->fopen('r');
			file_put_contents($sourceTmp, $content);

			$zip = new ZIP($sourceTmp);
			$zip->extractFile($path, $targetTmp);

			$image = new \OCP\Image();
			$image->loadFromFile($targetTmp);
			$image->fixOrientation();

			return $image;
		} catch (\Throwable $e) {
			return null;
		}
	}
}