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

261 lines
13 KiB
PHP

<?php
namespace Modules\GoogleCloudPlatform\Services;
class AugmentationService
{
public function __construct() {
$this->addGCPGeneralSettings();
$this->updateUploadFileController();
$this->updateImageStore();
}
public static function addGCPGeneralSettings() {
if (isModuleActive('GeneralSetting')) {
$path = module_path('GeneralSetting', 'Resources/views/cloud_storage_index.blade.php');
if (file_exists($path)) {
$content = file_get_contents($path);
if (!preg_match("/@include\('googlecloudplatform::setting'\)/", $content)) {
$matches = null;
preg_match('/[.\s\S]+file_storage_default[.\s\S]+?<\/div>[.\s\S]+?<\/div>[.\s\S]+?<\/form>/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
@if(isModuleActive('GoogleCloudPlatform'))
@include('googlecloudplatform::setting')
@endif";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+file_storage_default[.\s\S]+?<\/div>[.\s\S]+?<\/div>[.\s\S]+?<\/form>/', $code, $content);
file_put_contents($path, $content);
}
$content = file_get_contents($path);
preg_match('/[.\s\S]+\#AmazonS3[.\s\S]+?}/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
else if (file_storage[0] == 'GCP') {
\$('#file_storage_gcp').val(file_storage[1])
\$('#GoogleCloudPlatform').submit();
}";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+\#AmazonS3[.\s\S]+?}/', $code, $content);
file_put_contents($path, $content);
}
$content = file_get_contents($path);
preg_match('/[.\s\S]+DefaultStorage[.\s\S]+?contabo_file_storage_form\(\)\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
gcp_file_storage_form();
";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+DefaultStorage[.\s\S]+?contabo_file_storage_form\(\)\;/', $code, $content);
file_put_contents($path, $content);
}
$content = file_get_contents($path);
preg_match('/[.\s\S]+contabo_file_storage_form\(\)\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
gcp_file_storage_form();
";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+contabo_file_storage_form\(\)\;/', $code, $content);
file_put_contents($path, $content);
}
$content = file_get_contents($path);
preg_match('/[.\s\S]+contabo_hidden_div[.\s\S]+?}[.\s\S]+?}/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
function gcp_file_storage_form(){
let file_storage_type = $('.file_storage:checked').data('type');
let gcp_Div = $('#gcp_host_div');
if (file_storage_type === 'GCP') {
gcp_Div.show();
let hiddenInput = $('<input>').attr({
type: 'hidden',
id: 'gcp_hidden_div',
name: 'gcp',
value: 1
})
hiddenInput.appendTo('#gcp_hidden_div');
}
else{
gcp_Div.hide();
$('#gcp_hidden_div').html('');
}
}";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+contabo_hidden_div[.\s\S]+?}[.\s\S]+?}/', $code, $content);
file_put_contents($path, $content);
}
}
}
}
}
public function updateUploadFileController() {
$path = app_path('Http/Controllers/UploadFileController.php');
if (file_exists($path)) {
$content = file_get_contents($path);
if (!preg_match("/[.\s\S]+GCP[.\s\S]+?}[.\s\S]+?}[.\s\S]+?}/", $content)) {
$matches = null;
preg_match('/[.\s\S]+AmazonS3[.\s\S]+?}[.\s\S]+?}[.\s\S]+?}/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
else if(\$host == 'GCP'){
\$config = \Modules\GoogleCloudPlatform\Helpers\Helper::getStorageConfig();
\$storage = Storage::build(\$config);
foreach (\$files as \$file) {
foreach(\$file as \$k => \$f){
\$img_name_for_db = 'images/editorImage/'. time() . '.' . \$f->getClientOriginalExtension();
\$path = \$storage->put(\$img_name_for_db, file_get_contents(\$f), 'public');
\$image_url[\$k] = \$storage->url(\$img_name_for_db);
}
}
} ";
$code .= $snippet;
$content = preg_replace("/[.\s\S]+AmazonS3[.\s\S]+?}[.\s\S]+?}[.\s\S]+?}/", $code, $content);
file_put_contents($path, $content);
}
}
}
}
public function updateImageStore() {
$path = app_path('Traits/ImageStore.php');
if (file_exists($path)) {
$content = file_get_contents($path);
if (!preg_match("/[.\s\S]+saveImage[.\s\S]+?GCP[.\s\S]+?return \$url[.\s\S]+?}/", $content)) {
$matches = null;
preg_match('/[.\s\S]+saveImage[.\s\S]+?activeFileStorage[.\s\S]+?return \$url\;/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
} else if(\$host == 'GCP'){
\$img = Image::make(\$image);
if(\$height != null && \$lenght != null ){
\$img_size = getimagesize(\$image);
\$original_width = \$img_size[0];
\$original_height = \$img_size[1];
if(\$original_width > \$original_height){
// resize the image to a width of 300 and constrain aspect ratio (auto height)
\$img->resize(\$lenght, null, function (\$constraint) {
\$constraint->aspectRatio();
});
}elseif(\$original_width < \$original_height){
// resize the image to a height of 200 and constrain aspect ratio (auto width)
\$img->resize(null, \$height, function (\$constraint) {
\$constraint->aspectRatio();
});
}else{
if(\$lenght>\$height){
\$img->resize(null,\$lenght, function(\$constraint){
\$constraint->aspectRatio();
});
}elseif(\$lenght<\$height){
\$img->resize(\$height,null, function(\$constraint){
\$constraint->aspectRatio();
});
}else{
\$img->resize(\$height,null, function(\$constraint){
\$constraint->aspectRatio();
});
}
}
}
\$path = \Modules\GoogleCloudPlatform\Helpers\Helper::getImageDirectoryPath();
\$config = \Modules\GoogleCloudPlatform\Helpers\Helper::getStorageConfig();
\$storage = Storage::build(\$config);
\$img_name_for_db = \$path.'/'.uniqid().'.'.\$image_extention;
\$path = \$storage->put(\$img_name_for_db, \$img->stream(), 'public');
\$url = \$storage->url(\$img_name_for_db);
return \$url;
";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+saveImage[.\s\S]+?activeFileStorage[.\s\S]+?return \$url\;/', $code, $content);
file_put_contents($path, $content);
}
}
if (!preg_match("/[.\s\S]+deleteImage[.\s\S]+?storage\.googleapis\.com[.\s\S]+?\|\| \$host == \'GCP\'/", $content)) {
$matches = null;
$content = file_get_contents($path);
preg_match('/[.\s\S]+deleteImage[.\s\S]+?storage\.googleapis\.com[.\s\S]+?{/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
\$host = activeFileStorage();
if(\$host == 'GCP'){
\$config = \Modules\GoogleCloudPlatform\Helpers\Helper::getStorageConfig();
\$storage = Storage::build(\$config);
\$info = pathinfo(\$url);
\$path = \Modules\GoogleCloudPlatform\Helpers\Helper::getImageDirectoryPath().'/'.\$info['basename'];
return \$storage->delete(\$path);
}
";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+deleteImage[.\s\S]+?storage\.googleapis\.com[.\s\S]+?{/', $code, $content);
file_put_contents($path, $content);
}
}
if (!preg_match("/[.\s\S]+mediaUpload[.\s\S]+?activeFileStorage[.\s\S]+?\|\| \$host == \'GCP\'/", $content)) {
$matches = null;
$content = file_get_contents($path);
preg_match('/[.\s\S]+mediaUpload[.\s\S]+?activeFileStorage[.\s\S]+?\|\| \$host == \'GoogleCloud\'/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = " || \$host == 'GCP' ";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+mediaUpload[.\s\S]+?activeFileStorage[.\s\S]+?\|\| \$host == \'GoogleCloud\'/', $code, $content);
file_put_contents($path, $content);
}
}
if (!preg_match("/[.\s\S]+mediaUpload[.\s\S]+?GCP/", $content)) {
$matches = null;
$content = file_get_contents($path);
preg_match('/[.\s\S]+mediaUpload[.\s\S]+?activeFileStorage[.\s\S]+?Contabo\'\) {[.\s\S]+?}/', $content, $matches);
if (count($matches) > 0) {
$code = $matches[0];
$snippet = "
else if(\$host == 'GCP'){
\$path = \Modules\GoogleCloudPlatform\Helpers\Helper::getImageDirectoryPath();
\$config = \Modules\GoogleCloudPlatform\Helpers\Helper::getStorageConfig();
\$storage = Storage::build(\$config);
\$img_name_for_db = \$path.'/'.uniqid().'.'.\$image_extention;
\$path = \$storage->put(\$img_name_for_db, \$img->stream(), 'public');
\$url = \$storage->url(\$img_name_for_db);
\$data['file_name'] = \$url;
\$data['storage'] = 'gcs';
} ";
$code .= $snippet;
$content = preg_replace('/[.\s\S]+mediaUpload[.\s\S]+?activeFileStorage[.\s\S]+?Contabo\'\) {[.\s\S]+?}/', $code, $content);
file_put_contents($path, $content);
}
}
}
}
}