Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions apps/files_external/lib/Lib/Storage/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,42 @@
unset($this->directoryCache[$key]);
}

/**
* @return array{Key:string, LastModified?:string, ContentLength?:int, ETag?:string, Size?:int}|false
* @throws S3Exception For server errors and unexpected client errors.
*/
private function headObject(string $key): array|false {
if (!isset($this->objectCache[$key])) {
try {
$this->objectCache[$key] = $this->getConnection()->headObject([
'Bucket' => $this->bucket,
'Key' => $key
] + $this->getSSECParameters())->toArray();
} catch (S3Exception $e) {
if ($e->getStatusCode() >= 500) {
throw $e;
}
if (isset($this->objectCache[$key])) {
return $this->objectCache[$key];
}

try {
$result = $this->getConnection()->headObject([
'Bucket' => $this->bucket,
'Key' => $key
] + $this->getSSECParameters())->toArray();

// Defensive shape normalization
if (!isset($result['Key'])) {
$result['Key'] = $key;
}

$this->objectCache[$key] = $result;
return $result;

Check failure on line 134 in apps/files_external/lib/Lib/Storage/AmazonS3.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnStatement

apps/files_external/lib/Lib/Storage/AmazonS3.php:134:11: InvalidReturnStatement: The inferred type 'array{Key: mixed|string, ...<array-key, mixed>}' does not match the declared return type 'array{ContentLength?: int, ETag?: string, Key: string, LastModified?: string, Size?: int}|false' for OCA\Files_External\Lib\Storage\AmazonS3::headObject (see https://psalm.dev/128)
} catch (S3Exception $e) {
$status = (int)$e->getStatusCode();
$awsCode = (string)$e->getAwsErrorCode();

$isNotFound = $status === 404 || $awsCode === 'NoSuchKey' || $awsCode === 'NotFound';

if ($isNotFound) {
$this->objectCache[$key] = false;
return false;
}
}

if (is_array($this->objectCache[$key]) && !isset($this->objectCache[$key]['Key'])) {
/** @psalm-suppress InvalidArgument Psalm doesn't understand nested arrays well */
$this->objectCache[$key]['Key'] = $key;
// Fallback: i.e. surface an unexpected 4xx/5xx
throw $e;
}
return $this->objectCache[$key];
}

/**
Expand Down
Loading