mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
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>
This commit is contained in:
parent
ffe91b48dc
commit
2ea1bd4cdc
3 changed files with 34 additions and 27 deletions
|
|
@ -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…
Reference in a new issue