Skip to content

Commit d4d021b

Browse files
committed
refactor(TOSAdapter): 优化签名URL生成逻辑并提取公共方法
将签名URL生成的公共逻辑提取到preSignedURL方法中,简化signUrl和temporaryUploadUrl的实现 移除不必要的换行和参数格式化,保持代码简洁
1 parent 56c6f3e commit d4d021b

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/TOSAdapter.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public function providesTemporaryUrls(): bool
7979
*/
8080
public function temporaryUrl($path, $expiration, array $options = []): string
8181
{
82-
$uri = new Uri($this->signUrl($this->prefixer->prefixPath($path), $expiration, $options, 'GET',
83-
$this->config['endpoint']));
82+
$uri = new Uri($this->signUrl($this->prefixer->prefixPath($path), $expiration, $options, 'GET', $this->config['endpoint']));
8483

8584
return (string) $uri;
8685
}
@@ -96,17 +95,11 @@ public function temporaryUrl($path, $expiration, array $options = []): string
9695
*/
9796
public function temporaryUploadUrl($path, $expiration, array $options = []): array
9897
{
99-
$uri = new Uri($this->signUrl(
100-
$this->prefixer->prefixPath($path),
101-
$expiration,
102-
$options,
103-
'PUT',
104-
$this->config['endpoint']
105-
));
98+
$uri = $this->preSignedURL($this->prefixer->prefixPath($path), $expiration, $options, 'PUT', $this->config['endpoint']);
10699

107100
return [
108-
'url' => (string) $uri,
109-
'headers' => [],
101+
'url' => $uri->getSignedUrl(),
102+
'headers' => $uri->getSignedHeader(),
110103
];
111104
}
112105

@@ -120,35 +113,34 @@ public function getClient(): TosClient
120113
return $this->client;
121114
}
122115

116+
/**
117+
* Get a signed URL for the file at the given path.
118+
*
119+
* @param array<string, mixed> $options
120+
*/
121+
public function signUrl(string $path, \DateTimeInterface|int $expiration, array $options = [], string $method = 'GET', string $alternativeEndpoint = ''): string
122+
{
123+
$uri = $this->preSignedURL($path, $expiration, $options, $method, $alternativeEndpoint);
124+
125+
return $uri->getSignedUrl();
126+
}
123127

124128
/**
125129
* Get a signed URL for the file at the given path.
126130
*
127131
* @param array<string, mixed> $options
128132
*/
129-
public function signUrl(
130-
string $path,
131-
\DateTimeInterface|int $expiration,
132-
array $options = [],
133-
string $method = 'GET',
134-
string $alternativeEndpoint = ''
135-
): string {
133+
protected function preSignedURL(string $path, \DateTimeInterface|int $expiration, array $options = [], string $method = 'GET', string $alternativeEndpoint = ''): \Tos\Model\PreSignedURLOutput
134+
{
136135
$expires = $expiration instanceof \DateTimeInterface ? $expiration->getTimestamp() - time() : $expiration;
137136

138-
$preSignedURLInput = new PreSignedURLInput(
139-
$method,
140-
$alternativeEndpoint === '' || $alternativeEndpoint === '0' ? $this->config['bucket'] : '',
141-
$path,
142-
$expires
143-
);
137+
$preSignedURLInput = new PreSignedURLInput($method, $alternativeEndpoint === '' || $alternativeEndpoint === '0' ? $this->config['bucket'] : '', $path, $expires);
144138
if ($alternativeEndpoint !== '' && $alternativeEndpoint !== '0') {
145139
$preSignedURLInput->setAlternativeEndpoint($alternativeEndpoint);
146140
}
147141

148142
$preSignedURLInput->setQuery($options);
149-
150-
return $this->getClient()->preSignedURL($preSignedURLInput)
151-
->getSignedUrl();
143+
return $this->getClient()->preSignedURL($preSignedURLInput);
152144
}
153145

154146
}

0 commit comments

Comments
 (0)