Files
api/services/nginx/app/classes/attachment_store.php
T

105 lines
2.6 KiB
PHP

<?php
namespace classes;
use interfaces\minio_uploads_i;
use traits\minio_t;
class attachment_store implements minio_uploads_i
{
use minio_t;
public function __construct()
{
self::setBucket('attachments'); // 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
{
if (
$filePath === ''
|| str_starts_with($filePath, '/')
|| preg_match('/^[a-zA-Z0-9_\-\/.]+$/', $filePath) !== 1
) {
return false;
}
foreach (explode('/', $filePath) as $segment) {
if ($segment === '' || $segment === '.' || $segment === '..') {
return false;
}
}
return true;
}
/**
* @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 = 'https://api.truckwash.io';
$this->requireValidFilePath($fileName);
$encodedPath = implode('/', array_map('rawurlencode', explode('/', $fileName)));
// Generate a direct download URL for the given file name
return $host . '/files/' . $encodedPath;
}
}