Files
wticreatorstudio/Modules/StudioPlus/Services/GCPService.php
T
2024-02-12 22:54:20 -05:00

85 lines
2.5 KiB
PHP

<?php
namespace Modules\StudioPlus\Services;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Config;
use Google\Cloud\Storage\StorageClient;
use Modules\StudioPlus\Entities\StudioPlusVideo;
use Google\Cloud\Core\Exception\NotFoundException;
class GCPService
{
private $storageClient;
private $bucketName;
private $googleApiEndpoint;
private $bucket;
public function __construct()
{
$this->storageClient = new StorageClient([
'keyFile' => json_decode(file_get_contents(module_path('StudioPlus', 'google-cloud-platform.json')), true)
]);
$this->bucketName = Config::get('studioplus.bucket_name');
$this->googleApiEndpoint = Config::get('studioplus.google_api_endpoint');
$this->bucket = $this->storageClient->bucket($this->bucketName);
}
public function download(StudioPlusVideo $studioPlusVideo, string $destinationPath)
{
try {
if (!File::exists($destinationPath)) {
File::makeDirectory($destinationPath);
}
$video = "$destinationPath/{$studioPlusVideo->filename}";
if (File::exists($video)) {
File::delete($video);
}
$bucket = $this->storageClient->bucket($this->bucketName);
$object = $bucket->object($studioPlusVideo->getRawOriginal('original_video_url'));
$object->downloadToFile("$destinationPath/{$studioPlusVideo->filename}");
return $video;
} catch (\Exception $ex) {
logger($ex->getMessage());
return $ex->getMessage();
}
}
public function upload(string $pathToFile, string $objectName)
{
try {
$bucket = $this->storageClient->bucket($this->bucketName);
$bucket->upload(
fopen($pathToFile, 'r'),
[
'name' => $objectName
]
);
} catch (\Exception $ex) {
logger($ex->getMessage());
return $ex->getMessage();
}
}
public function delete(string $objectName)
{
try {
$bucket = $this->storageClient->bucket($this->bucketName);
$object = $bucket->object($objectName);
$object->delete();
} catch (\Exception $ex) {
logger($ex->getMessage());
throw new \Exception($ex->getMessage());
}
}
}