Files
api/services/nginx/app/classes/upload_store.php
T
Jeppe Bundgaard 356e59bd10 Improve dynamic URL generation and add localhost support for passkey challenges
- Refactor direct download URL generation to dynamically use `HTTP_HOST` and protocol.
- Add support for localhost `rpId` during WebAuthn passkey challenges.
- Extend test cases to validate `localhost` and `truckwash.io` scenarios.
- Update OpenAPI specifications to reflect new `rpId` logic and additional server configurations.
2026-02-23 23:37:15 +01:00

89 lines
2.3 KiB
PHP

<?php
namespace classes;
use interfaces\minio_uploads_i;
use traits\minio_t;
class upload_store implements minio_uploads_i
{
use minio_t;
public function __construct()
{
self::setBucket('uploads'); // Change the bucket to 'pdfs'
}
/**
* @inheritDoc
*/
public function generatePresignedUrl(string $objectName, int $expiry = 3600): string
{
// Generate a presigned URL for the given object name with the specified expiry time
return self::getPresignedUrl($objectName, $expiry, true);
}
/**
* @inheritDoc
*/
public function isValidFileName(string $fileName): bool
{
// Check if the file name is valid
return preg_match('/^[a-zA-Z0-9_\-.]+$/', $fileName) === 1;
}
/**
* @inheritDoc
*/
public function isValidFilePath(string $filePath): bool
{
// Check if the file path is valid
return preg_match('/^[a-zA-Z0-9_\-\/.]+$/', $filePath) === 1;
}
/**
* @inheritDoc
*/
public function requireValidFileName(string $fileName): void
{
if (!$this->isValidFileName($fileName)) {
throw new \InvalidArgumentException("Invalid file name: $fileName");
}
}
/**
* @inheritDoc
*/
public function requireValidFilePath(string $filePath): void
{
if (!$this->isValidFilePath($filePath)) {
throw new \InvalidArgumentException("Invalid file path: $filePath");
}
}
public function isFileInStore(string $fileName): bool
{
// Check if the file exists in the store
return self::doesObjectExist($fileName);
}
public function download(string $file): string
{
$path = '/tmp/' . $file;
$result = self::getS3Client()->getObject([
'Bucket' => self::getBucket(),
'Key' => $file,
'SaveAs' => $path
]);
return $path;
}
public function generateDirectDownloadUrl(string $fileName): string
{
$host = $_SERVER['HTTP_HOST'] ?? 'api.truckwash.dk';
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ($_SERVER['SERVER_PORT'] ?? 0) == 443 ? "https://" : "http://";
// Generate a direct download URL for the given file name
return $protocol . $host . '/files/' . $fileName;
}
}