feat(movie-preview): Use getDirectDownloadById for generating preview

Allow to speed-up considerably the creation of previews for movies
stored on S3.

Signed-off-by: Carl Schwan <carlschwan@kde.org>
pull/53634/head
Carl Schwan 5 days ago
parent ffe91b48dc
commit 2ea1bd4cdc
No known key found for this signature in database
GPG Key ID: 02325448204E452A
  1. 38
      apps/files_external/lib/Lib/Storage/AmazonS3.php
  2. 8
      lib/private/Files/ObjectStore/S3ObjectTrait.php
  3. 15
      lib/private/Preview/Movie.php

@ -23,6 +23,7 @@ use OCP\ICache;
use OCP\ICacheFactory;
use OCP\ITempManager;
use OCP\Server;
use Override;
use Psr\Log\LoggerInterface;
class AmazonS3 extends Common {
@ -761,34 +762,43 @@ class AmazonS3 extends Common {
return $size;
}
/**
* Generates and returns a presigned URL that expires after set duration
*
*/
#[Override]
public function getDirectDownload(string $path): array|false {
if (!$this->isUsePresignedUrl()) {
return false;
}
$command = $this->getConnection()->getCommand('GetObject', [
'Bucket' => $this->bucket,
'Key' => $path,
]);
$duration = '+10 minutes';
$expiration = new \DateTime();
$expiration->modify($duration);
$expiration = new \DateTimeImmutable('+60 minutes');
// generate a presigned URL that expires after $duration time
$request = $this->getConnection()->createPresignedRequest($command, $duration, []);
try {
$presignedUrl = (string)$request->getUri();
// generate a presigned URL that expires after $expiration time
$presignedUrl = (string)$this->getConnection()->createPresignedRequest($command, $expiration, [
'signPayload' => true,
])->getUri();
} catch (S3Exception $exception) {
$this->logger->error($exception->getMessage(), [
'app' => 'files_external',
'exception' => $exception,
]);
return false;
}
$result = [
return [
'url' => $presignedUrl,
'presigned' => true,
'expiration' => $expiration,
'expiration' => $expiration->getTimestamp(),
];
return $result;
}
#[Override]
public function getDirectDownloadById(string $fileId): array|false {
if (!$this->isUsePresignedUrl()) {
return false;
}
$entry = $this->getCache()->get((int)$fileId);
return $this->getDirectDownload($entry->getPath());
}
}

@ -298,15 +298,15 @@ trait S3ObjectTrait {
}
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string {
if (!$this->isUsePresignedUrl()) {
return null;
}
$command = $this->getConnection()->getCommand('GetObject', [
'Bucket' => $this->getBucket(),
'Key' => $urn,
]);
if (!$this->isUsePresignedUrl()) {
return null;
}
try {
return (string)$this->getConnection()->createPresignedRequest($command, $expiration, [
'signPayload' => true,

@ -43,21 +43,18 @@ class Movie extends ProviderV2 {
}
private function connectDirect(File $file): string|false {
if (stream_get_meta_data($file->fopen('r'))['seekable'] !== true) {
if ($file->isEncrypted()) {
return false;
}
// Checks for availability to access the video file directly via HTTP/HTTPS.
// Returns a string containing URL if available. Only implemented and tested
// with Amazon S3 currently. In all other cases, return false. ffmpeg
// with Amazon S3 currently. In all other cases, return false. ffmpeg
// supports other protocols so this function may expand in the future.
$gddValues = $file->getStorage()->getDirectDownload($file->getName());
$gddValues = $file->getStorage()->getDirectDownloadById((string)$file->getId());
if (is_array($gddValues)) {
if (array_key_exists('url', $gddValues) && array_key_exists('presigned', $gddValues)) {
$directUrl = (str_starts_with($gddValues['url'], 'http') && ($gddValues['presigned'] === true)) ? $gddValues['url'] : false;
return $directUrl;
}
if (is_array($gddValues) && array_key_exists('url', $gddValues)) {
return str_starts_with($gddValues['url'], 'http') ? $gddValues['url'] : false;
}
return false;
}
@ -81,7 +78,7 @@ class Movie extends ProviderV2 {
// If HTTP/HTTPS direct connect is not available or if the file is encrypted,
// process normally
if (($connectDirect === false) || $file->isEncrypted()) {
if ($connectDirect === false) {
// By default, download $sizeAttempts from the file along with
// the 'moov' atom.
// Example bitrates in the higher range:

Loading…
Cancel
Save