Files
api/traits/minio_t.php
T
Jepp9350 30f30c66ba Refactor wash certificate handling and add user access checks.
Simplified wash certificate storage logic by updating methods for file existence checks and download link generation. Added validation to ensure users can only access their own wash certificates. Introduced a route for downloading wash certificates with proper permissions and error handling.
2025-01-15 15:16:43 +01:00

183 lines
4.5 KiB
PHP

<?php
namespace traits;
use Aws\S3\S3Client;
use classes\wash_certificate_store;
trait minio_t
{
/**
* The bucket to store the files in
* @return string
*/
private string $bucket;
/**
* The S3 client to interact with the Minio server
* @return S3Client
*/
private s3Client $s3Client;
/**
* List all the files in the bucket
* @return array
*/
public function listFiles(): array
{
$objects = self::getS3Client()->listObjects([
'Bucket' => self::getBucket()
]);
// If there are no files, return an empty array
if (!isset($objects['Contents'])) {
return [];
}
return $objects['Contents'];
}
/**
* Returns the S3 client to interact with the Minio server
* @return S3Client
*/
public function getS3Client(): S3Client
{
// If the S3 client is not set, create a new one
if (!isset($this->s3Client)) {
$this->connect();
}
return $this->s3Client;
}
/**
* Connect to the Minio server
*/
private function connect(): self
{
$this->s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => $this->getEndpoint(),
'use_path_style_endpoint' => true,
'credentials' => [
'key' => $this->getAccessKey(),
'secret' => $this->getSecretKey(),
],
]);
return $this;
}
/**
* Returns the endpoint of the Minio server
* @return string
*/
public function getEndpoint(): string
{
global $MINIO;
return $MINIO['endpoint'];
}
/**
* Returns the access key of the Minio server
* @return string
*/
public function getAccessKey(): string
{
global $MINIO;
return $MINIO['access_key'];
}
/**
* Returns the secret key of the Minio server
* @return string
*/
public function getSecretKey(): string
{
global $MINIO;
return $MINIO['secret_key'];
}
/**
* Returns the bucket to store the files in
* @return string
*/
public function getBucket(): string
{
return $this->bucket;
}
/**
* Sets the bucket to store the files in
* @param string $bucket
* @return wash_certificate_store|minio_t
*/
public function setBucket(string $bucket): self
{
$this->bucket = $bucket;
return $this;
}
/**
* Create a new object in the bucket
* @param string $key The key of the object (file)
* @param string $body The content of the object (file)
* @return bool
*/
public function createObject(string $key, string $body): bool
{
$result = self::getS3Client()->putObject([
'Bucket' => self::getBucket(),
'Key' => $key,
'Body' => $body
]);
return $result['@metadata']['statusCode'] == 200;
}
/**
* Get the URL of the object in the bucket
* @param string $key The key of the object (file)
* @return string
*/
public function getObjectUrl(string $key): string
{
return self::getS3Client()->getObjectUrl(self::getBucket(), $key);
}
/**
* Generate a presigned URL for the object in the bucket (valid for 20 minutes)
* @param string $key The key of the object (file)
* @return string
*/
public function getPresignedUrl(string $key): string
{
$command = self::getS3Client()->getCommand('GetObject', [
'Bucket' => self::getBucket(),
'Key' => $key
]);
return (string)self::getS3Client()->createPresignedRequest($command, '+20 minutes')->getUri();
}
/**
* Upload a file to the bucket
* @param string $key The key of the object (file)
* @param string $file The path to the file to upload
* @return bool
*/
public function uploadFile(string $key, string $file): bool
{
$result = self::getS3Client()->putObject([
'Bucket' => self::getBucket(),
'Key' => $key,
'SourceFile' => $file
]);
return $result['@metadata']['statusCode'] == 200;
}
/**
* Check if the object exists in the bucket
* @param string $key The key of the object (file or folder)
* @return bool
*/
public function doesObjectExist(string $key): bool
{
return self::getS3Client()->doesObjectExist(self::getBucket(), $key);
}
}