Language updates. New upload form. new classes.
This commit is contained in:
parent
4c2857b445
commit
8f3061ab99
62 changed files with 3107 additions and 1883 deletions
75
classes/CDN.php
Normal file
75
classes/CDN.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace DJMixHosting;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
use Aws\Credentials\Credentials;
|
||||
|
||||
class CDN
|
||||
{
|
||||
protected $s3Client;
|
||||
protected $bucket;
|
||||
protected $config;
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->bucket = $config['aws']['s3']['bucket'];
|
||||
|
||||
$credentials = new Credentials(
|
||||
$config['aws']['s3']['access_key'],
|
||||
$config['aws']['s3']['secret_key']
|
||||
);
|
||||
|
||||
$this->s3Client = new S3Client([
|
||||
'version' => 'latest',
|
||||
'region' => $config['aws']['s3']['region'],
|
||||
'endpoint' => $config['aws']['s3']['host'],
|
||||
'credentials' => $credentials,
|
||||
'use_path_style_endpoint' => true,
|
||||
'profile' => null,
|
||||
'use_aws_shared_config_files' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function uploadFile(string $localPath, string $remotePath, string $mimeType, string $acl = 'private')
|
||||
{
|
||||
try {
|
||||
$result = $this->s3Client->putObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $remotePath,
|
||||
'SourceFile' => $localPath,
|
||||
'ACL' => $acl,
|
||||
'ContentType' => $mimeType,
|
||||
]);
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("Error uploading file to CDN: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function renameFile(string $oldRemotePath, string $newRemotePath)
|
||||
{
|
||||
// S3 does not support renaming directly. Copy then delete.
|
||||
try {
|
||||
$this->s3Client->copyObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'CopySource' => "{$this->bucket}/{$oldRemotePath}",
|
||||
'Key' => $newRemotePath,
|
||||
]);
|
||||
|
||||
$this->s3Client->deleteObject([
|
||||
'Bucket' => $this->bucket,
|
||||
'Key' => $oldRemotePath,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("Error renaming file on CDN: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function clearCache(string $remotePath)
|
||||
{
|
||||
// If using CloudFront or another CDN in front of S3, implement invalidation logic here.
|
||||
// This might involve calling the CloudFront API to invalidate the specific path.
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue