Spaces:
Running
Running
| namespace App\Domain\Files; | |
| use Illuminate\Support\Carbon; | |
| use Illuminate\Support\Facades\URL; | |
| class SignedFileUrlFactory | |
| { | |
| public function temporaryFileDownloadUrl(string $routeName, string $fileId, ?Carbon $expiresAt = null): array | |
| { | |
| return $this->temporaryFileUrl($routeName, $fileId, $expiresAt); | |
| } | |
| public function temporaryFileStreamUrl(string $routeName, string $fileId, ?Carbon $expiresAt = null): array | |
| { | |
| return $this->temporaryFileUrl($routeName, $fileId, $expiresAt); | |
| } | |
| private function temporaryFileUrl(string $routeName, string $fileId, ?Carbon $expiresAt = null): array | |
| { | |
| $expiresAt ??= Carbon::now()->addMinutes(10); | |
| $signedRelativeUrl = URL::temporarySignedRoute($routeName, $expiresAt, [ | |
| 'fileId' => $fileId, | |
| ], false); | |
| $relativePath = parse_url($signedRelativeUrl, PHP_URL_PATH) ?: $signedRelativeUrl; | |
| $queryString = (string) (parse_url($signedRelativeUrl, PHP_URL_QUERY) ?: ''); | |
| $publicPath = $this->publicPathFor($relativePath); | |
| return [ | |
| 'url' => rtrim($this->publicOrigin(), '/').$publicPath.($queryString === '' ? '' : '?'.$queryString), | |
| 'expires_at' => $expiresAt, | |
| ]; | |
| } | |
| private function publicPathFor(string $relativePath): string | |
| { | |
| $publicBase = $this->publicApiBaseUrl(); | |
| $basePath = rtrim((string) (parse_url($publicBase, PHP_URL_PATH) ?: ''), '/'); | |
| if ($basePath === '') { | |
| return $relativePath; | |
| } | |
| if (str_starts_with($relativePath, $basePath.'/')) { | |
| return $relativePath; | |
| } | |
| if (str_ends_with($basePath, '/api/v1') && str_starts_with($relativePath, '/api/v1/')) { | |
| return $basePath.substr($relativePath, strlen('/api/v1')); | |
| } | |
| return $relativePath; | |
| } | |
| private function publicOrigin(): string | |
| { | |
| $publicBase = $this->publicApiBaseUrl(); | |
| $basePath = rtrim((string) (parse_url($publicBase, PHP_URL_PATH) ?: ''), '/'); | |
| if ($publicBase === '') { | |
| return ''; | |
| } | |
| return preg_replace('#'.preg_quote($basePath, '#').'$#', '', $publicBase) ?: $publicBase; | |
| } | |
| private function publicApiBaseUrl(): string | |
| { | |
| $configured = trim((string) (config('app.public_api_base_url') ?: config('platform.backend_api_base_url'))); | |
| if ($configured !== '') { | |
| return rtrim($configured, '/'); | |
| } | |
| $request = request(); | |
| $origin = $request->getSchemeAndHttpHost(); | |
| $baseUrl = rtrim($request->getBaseUrl(), '/'); | |
| if ($origin !== '' && $baseUrl !== '') { | |
| return $origin.$baseUrl.'/api/v1'; | |
| } | |
| $requestPath = (string) (parse_url($request->getRequestUri(), PHP_URL_PATH) ?: ''); | |
| $apiPrefixPosition = strpos($requestPath, '/api/v1'); | |
| if ($origin !== '' && $apiPrefixPosition !== false) { | |
| $publicPrefix = rtrim(substr($requestPath, 0, $apiPrefixPosition), '/'); | |
| return $origin.$publicPrefix.'/api/v1'; | |
| } | |
| return rtrim((string) config('app.url'), '/'); | |
| } | |
| } | |