Skip to content

Commit

Permalink
Merge pull request #75 from fleetbase/dev-v1.4.12
Browse files Browse the repository at this point in the history
v1.4.12
  • Loading branch information
roncodes authored Mar 12, 2024
2 parents c614dfd + 0213b7d commit bfad44e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.4.11",
"version": "1.4.12",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down Expand Up @@ -44,6 +44,7 @@
"pragmarx/countries": "^0.8.2",
"sentry/sentry-laravel": "*",
"spatie/laravel-activitylog": "^4.7",
"spatie/laravel-google-cloud-storage": "^2.2",
"spatie/laravel-permission": "^6.3",
"spatie/laravel-responsecache": "^7.4",
"spatie/laravel-sluggable": "^3.5",
Expand Down
124 changes: 107 additions & 17 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public function adminOverview(AdminRequest $request)
}

/**
* Loads and sends the filesystem configuration.
* Retrieves the current filesystem configuration including details about configured storage drivers.
* It specifically fetches configurations for AWS S3 and Google Cloud Storage (GCS).
*
* @return \Illuminate\Http\JsonResponse
* @param AdminRequest $request the incoming request
*
* @return \Illuminate\Http\JsonResponse JSON response containing the filesystem configuration details
*/
public function getFilesystemConfig(AdminRequest $request)
{
Expand All @@ -46,31 +49,112 @@ public function getFilesystemConfig(AdminRequest $request)
$s3Url = config('filesystems.disks.s3.url');
$s3Endpoint = config('filesystems.disks.s3.endpoint');

// Get GCS bucket
$gcsBucket = config('filesystems.disks.gcs.bucket');

// Get GCS configurations
$isGoogleCloudStorageEnvConfigued = (!empty(env('GOOGLE_CLOUD_STORAGE_BUCKET')) && !empty(env('GOOGLE_CLOUD_PROJECT_ID'))) || !empty(env('GOOGLE_APPLICATION_CREDENTIALS')) || !empty(env('GOOGLE_CLOUD_KEY_FILE'));

// Get the GCS configuration file if any
$gcsCredentialsFileId = config('filesystems.disks.gcs.key_file_id');
$gcsCredentialsFile = null;
if (Str::isUuid($gcsCredentialsFileId)) {
$gcsCredentialsFile = File::where('uuid', $gcsCredentialsFileId)->first();
}

return response()->json([
'driver' => $driver,
'disks' => $disks,
's3Bucket' => $s3Bucket,
's3Url' => $s3Url,
's3Endpoint' => $s3Endpoint,
'driver' => $driver,
'disks' => $disks,
's3Bucket' => $s3Bucket,
's3Url' => $s3Url,
's3Endpoint' => $s3Endpoint,
'gcsBucket' => $gcsBucket,
'isGoogleCloudStorageEnvConfigued' => $isGoogleCloudStorageEnvConfigued,
'gcsCredentialsFileId' => $gcsCredentialsFileId,
'gcsCredentialsFile' => $gcsCredentialsFile,
]);
}

/**
* Saves filesystem configuration.
* Saves the updated filesystem configuration based on the incoming request.
* It handles the settings for various filesystem drivers like local, AWS S3, and GCS.
*
* @return \Illuminate\Http\JsonResponse
* @param AdminRequest $request the incoming request containing filesystem configuration data
*
* @return \Illuminate\Http\JsonResponse JSON response indicating the success status of the operation
*/
public function saveFilesystemConfig(AdminRequest $request)
{
$driver = $request->input('driver', 'local');
$s3 = $request->input('s3', config('filesystems.disks.s3'));
$gcs = static::_getGcsConfigWithIcomingRequest($request);

Setting::configure('system.filesystem.driver', $driver);
Setting::configure('system.filesystem.gcs', $gcs);
Setting::configure('system.filesystem.s3', array_merge(config('filesystems.disks.s3', []), $s3));

return response()->json(['status' => 'OK']);
}

/**
* Extracts the Google Cloud Storage (GCS) configuration from the incoming request.
* Updates the GCS configuration based on the provided bucket information and credentials file ID.
*
* @param Request $request the incoming request with GCS configuration details
*
* @return array the updated GCS configuration array
*/
private static function _getGcsConfigWithIcomingRequest(Request $request): array
{
$gcsBucket = $request->input('gcsBucket');
$gcsCredentialsFileId = $request->input('gcsCredentialsFileId');
$gcsConfig = config('filesystems.disks.gcs');
$gcsConfig['bucket'] = $gcsBucket;
if ($gcsCredentialsFileId) {
$gcsCredentialsContent = static::_setupGcsConfigUsingFileId($gcsCredentialsFileId);
if (is_array($gcsCredentialsContent)) {
$gcsConfig['key_file'] = $gcsCredentialsContent;
$gcsConfig['key_file_id'] = $gcsCredentialsFileId;
if (isset($gcsCredentialsContent['project_id'])) {
$gcsConfig['project_id'] = $gcsCredentialsContent['project_id'];
}
}
} else {
unset($gcsConfig['key_file_id']);
$gcsConfig['key_file'] = [];
}

return $gcsConfig;
}

/**
* Sets up the Google Cloud Storage (GCS) configuration using a given file ID.
* Retrieves the credentials file for GCS and decodes its contents to use in the GCS configuration.
*
* @param string $fileId the UUID of the file containing GCS credentials
*
* @return array an array containing the parsed content of the GCS credentials file
*/
private static function _setupGcsConfigUsingFileId(string $fileId): array
{
if (!Str::isUuid($fileId)) {
return [];
}

// Get the "GOOGLE_APPLICATION_CREDENTIALS" file
$gcsCredentialsFile = File::where('uuid', $fileId)->first();
if ($gcsCredentialsFile) {
$gcsCredentialsFileContents = Storage::disk($gcsCredentialsFile->disk)->get($gcsCredentialsFile->path);
if (is_string($gcsCredentialsFileContents)) {
$gcsCredentialsFileContents = json_decode($gcsCredentialsFileContents, true);

return $gcsCredentialsFileContents;
}
}

return [];
}

/**
* Creates a file and uploads it to the users default disks.
*
Expand All @@ -80,24 +164,30 @@ public function saveFilesystemConfig(AdminRequest $request)
*/
public function testFilesystemConfig(AdminRequest $request)
{
$disk = $request->input('disk', config('filesystems.default'));
$message = 'Filesystem configuration is successful, test file uploaded.';
$status = 'success';
$disk = $request->input('disk', config('filesystems.default'));
$message = 'Filesystem configuration is successful, test file uploaded.';
$status = 'success';
$uploaded = false;

// set config values from input
config(['filesystem.default' => $disk]);

config(['filesystems.default' => $disk]);
try {
Storage::disk($disk)->put('testfile.txt', 'Hello World');
$uploaded = Storage::disk($disk)->put('testfile.txt', 'Hello World');
} catch (\Aws\S3\Exception\S3Exception $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\Exception $e) {
} catch (\Throwable $e) {
$message = $e->getMessage();
$status = 'error';
}

return response()->json(['status' => $status, 'message' => $message]);
// sometimes there is no error but file is not uploaded
if (!$uploaded) {
$status = 'error';
$message = 'Configuration is working, but test file upload failed for uknown reasons.';
}

return response()->json(['status' => $status, 'message' => $message, 'uploaded' => $uploaded]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Filter/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function parentsOnly()
}
}

public function parent(?string $id)
public function parentCategory(?string $id)
{
if (Str::isUuid($id)) {
$this->builder->where('parent_uuid', $id);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function isCompanyOwner(Company $company): bool
*/
public function assignCompanyFromId(?string $id): User
{
if (!Str::isUuid($id) || !Utils::isPublicId($id)) {
if (!Str::isUuid($id) && !Utils::isPublicId($id)) {
return $this;
}

Expand Down
1 change: 1 addition & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function mergeConfigFromSettings()
$settings = [
['settingsKey' => 'filesystem.driver', 'configKey' => 'filesystems.default'],
['settingsKey' => 'filesystem.s3', 'configKey' => 'filesystems.disks.s3'],
['settingsKey' => 'filesystem.gcs', 'configKey' => 'filesystems.disks.gcs'],
['settingsKey' => 'mail.mailer', 'configKey' => 'mail.default'],
['settingsKey' => 'mail.from', 'configKey' => 'mail.from'],
['settingsKey' => 'mail.smtp', 'configKey' => 'mail.mailers.smtp'],
Expand Down

0 comments on commit bfad44e

Please sign in to comment.