Skip to content

Commit

Permalink
Merge pull request #28897 from nextcloud/backport/28889/stable20
Browse files Browse the repository at this point in the history
[stable20] Fall back to full file for video previews
  • Loading branch information
PVince81 authored Sep 20, 2021
2 parents 247c53a + d98a296 commit 23a2344
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
31 changes: 24 additions & 7 deletions lib/private/Preview/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,34 @@ public function getMimeType(): string {
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
// TODO: use proc_open() and stream the source file ?
$absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB
$result = null;
if ($this->useTempFile($file)) {
// try downloading 5 MB first as it's likely that the first frames are present there
// in some cases this doesn't work for example when the moov atom is at the
// end of the file, so if it fails we fall back to getting the full file
$sizeAttempts = [5242880, null];
} else {
// size is irrelevant, only attempt once
$sizeAttempts = [null];
}

$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
foreach ($sizeAttempts as $size) {
$absPath = $this->getLocalFile($file, $size);

$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
}
}
}

$this->cleanTmpFiles();
$this->cleanTmpFiles();

if ($result !== null) {
break;
}
}

return $result;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/private/Preview/ProviderV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function isAvailable(FileInfo $file): bool {
*/
abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;

protected function useTempFile(File $file) {
return $file->isEncrypted() || !$file->getStorage()->isLocal();
}

/**
* Get a path to either the local file or temporary file
*
Expand All @@ -79,8 +83,7 @@ abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage
* @return string
*/
protected function getLocalFile(File $file, int $maxSize = null): string {
$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
if ($useTempFile) {
if ($this->useTempFile($file)) {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();

$content = $file->fopen('r');
Expand Down

0 comments on commit 23a2344

Please sign in to comment.