diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index e6896096a9e..fe7e31a1356 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.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()); } } diff --git a/lib/private/Files/ObjectStore/S3ObjectTrait.php b/lib/private/Files/ObjectStore/S3ObjectTrait.php index 91eff90babb..b6b55c746b2 100644 --- a/lib/private/Files/ObjectStore/S3ObjectTrait.php +++ b/lib/private/Files/ObjectStore/S3ObjectTrait.php @@ -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, diff --git a/lib/private/Preview/Movie.php b/lib/private/Preview/Movie.php index d9d4135dff1..00648e7526e 100644 --- a/lib/private/Preview/Movie.php +++ b/lib/private/Preview/Movie.php @@ -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: