Skip to content

Commit 819a38b

Browse files
committed
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 <[email protected]>
1 parent ffe91b4 commit 819a38b

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

apps/files_external/lib/Lib/Storage/AmazonS3.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCP\ICacheFactory;
2424
use OCP\ITempManager;
2525
use OCP\Server;
26+
use Override;
2627
use Psr\Log\LoggerInterface;
2728

2829
class AmazonS3 extends Common {
@@ -761,34 +762,43 @@ public function writeStream(string $path, $stream, ?int $size = null): int {
761762
return $size;
762763
}
763764

764-
/**
765-
* Generates and returns a presigned URL that expires after set duration
766-
*
767-
*/
765+
#[Override]
768766
public function getDirectDownload(string $path): array|false {
767+
if (!$this->isUsePresignedUrl()) {
768+
return false;
769+
}
770+
769771
$command = $this->getConnection()->getCommand('GetObject', [
770772
'Bucket' => $this->bucket,
771773
'Key' => $path,
772774
]);
773-
$duration = '+10 minutes';
774-
$expiration = new \DateTime();
775-
$expiration->modify($duration);
775+
$expiration = new \DateTimeImmutable('+60 minutes');
776776

777-
// generate a presigned URL that expires after $duration time
778-
$request = $this->getConnection()->createPresignedRequest($command, $duration, []);
779777
try {
780-
$presignedUrl = (string)$request->getUri();
778+
// generate a presigned URL that expires after $expiration time
779+
$presignedUrl = (string)$this->getConnection()->createPresignedRequest($command, $expiration, [
780+
'signPayload' => true,
781+
])->getUri();
781782
} catch (S3Exception $exception) {
782783
$this->logger->error($exception->getMessage(), [
783784
'app' => 'files_external',
784785
'exception' => $exception,
785786
]);
787+
return false;
786788
}
787-
$result = [
789+
return [
788790
'url' => $presignedUrl,
789-
'presigned' => true,
790-
'expiration' => $expiration,
791+
'expiration' => $expiration->getTimestamp(),
791792
];
792-
return $result;
793+
}
794+
795+
#[Override]
796+
public function getDirectDownloadById(string $fileId): array|false {
797+
if (!$this->isUsePresignedUrl()) {
798+
return false;
799+
}
800+
801+
$entry = $this->cache->get($fileId);
802+
return $this->getDirectDownload($entry->getPath());
793803
}
794804
}

lib/private/Files/ObjectStore/S3ObjectTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,15 @@ public function copyObject($from, $to, array $options = []) {
298298
}
299299

300300
public function preSignedUrl(string $urn, \DateTimeInterface $expiration): ?string {
301+
if (!$this->isUsePresignedUrl()) {
302+
return null;
303+
}
304+
301305
$command = $this->getConnection()->getCommand('GetObject', [
302306
'Bucket' => $this->getBucket(),
303307
'Key' => $urn,
304308
]);
305309

306-
if (!$this->isUsePresignedUrl()) {
307-
return null;
308-
}
309-
310310
try {
311311
return (string)$this->getConnection()->createPresignedRequest($command, $expiration, [
312312
'signPayload' => true,

lib/private/Preview/Movie.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ private function connectDirect(File $file): string|false {
4747
return false;
4848
}
4949

50+
if ($file->isEncrypted()) {
51+
return false;
52+
}
53+
5054
// Checks for availability to access the video file directly via HTTP/HTTPS.
5155
// Returns a string containing URL if available. Only implemented and tested
52-
// with Amazon S3 currently. In all other cases, return false. ffmpeg
56+
// with Amazon S3 currently. In all other cases, return false. ffmpeg
5357
// supports other protocols so this function may expand in the future.
54-
$gddValues = $file->getStorage()->getDirectDownload($file->getName());
58+
$gddValues = $file->getStorage()->getDirectDownloadById((string)$file->getId());
5559

56-
if (is_array($gddValues)) {
57-
if (array_key_exists('url', $gddValues) && array_key_exists('presigned', $gddValues)) {
58-
$directUrl = (str_starts_with($gddValues['url'], 'http') && ($gddValues['presigned'] === true)) ? $gddValues['url'] : false;
59-
return $directUrl;
60-
}
60+
if (is_array($gddValues) && array_key_exists('url', $gddValues)) {
61+
return str_starts_with($gddValues['url'], 'http') ? $gddValues['url'] : false;
6162
}
6263
return false;
6364
}
@@ -81,7 +82,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8182

8283
// If HTTP/HTTPS direct connect is not available or if the file is encrypted,
8384
// process normally
84-
if (($connectDirect === false) || $file->isEncrypted()) {
85+
if ($connectDirect === false) {
8586
// By default, download $sizeAttempts from the file along with
8687
// the 'moov' atom.
8788
// Example bitrates in the higher range:

0 commit comments

Comments
 (0)