HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/S3Library.php
<?php
namespace App\Library;

use DB;
use Hash;
use Storage;
use Config;

class S3Library {

    public function modelUrlRetive($pathUrl, $fileName)
    {
        if (Config::get('filesystems.default') === 's3') {
            $path = Storage::temporaryUrl($pathUrl . $fileName, now()->addMinutes(5));
        } else {
            $path = asset('storage/' . $pathUrl . $fileName);
        }

        return $path;
    }

    public function fileExist($pathUrl, $fileName)
    {
        if(Config::get('filesystems.default') == "s3") {
            return Storage::disk('s3')->exists($pathUrl.$fileName);
        } else {
            return Storage::disk('public')->exists($pathUrl.$fileName);
        }
    }

    public function getFilePathAttribute($value) {

        $disk = Storage::disk('s3');
        /** @var object $disk */
        if ($disk->exists($value)) {
            $command = $disk->getDriver()->getAdapter()->getClient()->getCommand('GetObject', [
                'Bucket'                     => Config::get('filesystems.disks.s3.bucket'),
                'Key'                        => $value,
                // 'ResponseContentDisposition' => 'attachment;'
            ]);

            $request = $disk->getDriver()->getAdapter()->getClient()->createPresignedRequest($command, '+5 minutes');

            return (string) $request->getUri();
        }

        return $value;
    }

    public function s3PathRetriveSumsub($pathUrl, $fileName) {
        if(Config::get('filesystems.default') == "s3") {
            $value = $pathUrl.$fileName;
            return $this->getFilePathAttribute($value);
            // return Storage::temporaryUrl($pathUrl.$fileName, now()->addMinutes(5));
        } else {
            return Storage::disk('public')->path($pathUrl.$fileName);
        }
    }

    /**
     * [fileUpload description]
     * @param  boolean  $format
     * @param  string  $formatArr
     * @param  boolean $renameLogic
     * @param  string  $pathUrl
     * @return array
     */
    public function fileUpload($format, $formatArr, $renameLogic, $file, $pathUrl='digimed/images/') {
        $fileRelationship = $file;
        $fileName = $fileRelationship->getClientOriginalName();
        $fileExtension = $fileRelationship->getClientOriginalExtension();

        if($format) {
            if(!preg_match("/\.($formatArr)$/", $fileName)) {
                return ['code' => 500, 'data' => [], 'msg' => __('digimed_validation.error_response.authenticate_upload_faild_withtype')];
            }
        }

        if($renameLogic) {
            $existUrl = $this->fileExist($pathUrl, $fileName);

            if($existUrl) {
                $fCount = 0;
                $condition = true;
                do {
                    $renamedFile = pathinfo($fileName, PATHINFO_FILENAME).'_('.$fCount.').'.$fileExtension;
                    if($this->fileExist($pathUrl, $renamedFile)) {
                        $fCount++;
                        $condition = true;
                    } else {
                        $condition = false;
                        $renamedFile = $renamedFile;
                    }
                } while ($condition);
            } else {
                $renamedFile = $fileName;
            }
        } else {
            $renamedFile = $fileName;
        }

        if(Config::get('filesystems.default') == "s3") {
            Storage::disk('s3')->putFileAs($pathUrl, $fileRelationship, $renamedFile);
            return ['code' => 200, 'data' => $renamedFile, 'msg' => ''];
        } else {
            Storage::disk('public')->putFileAs($pathUrl, $fileRelationship, $renamedFile);
            return ['code' => 200, 'data' => $renamedFile, 'msg' => ''];
        }

    }

    public function base64($file, $dynamic_id, $concept=[], $pathUrl='digimed/images/', $updateLogic=false) {
        if(count($concept) != 0) {
            //Base 64 upload for only image
            if (preg_match('/data:image\/(.+);base64,(.*)/', $file, $matchings)) {
                $imageData = base64_decode($matchings[2]);
                $extension = $matchings[1];
                $file_name = date('YmdHis') . rand(100, 999) . '_' . $dynamic_id . '.' . $extension;
                $path = $pathUrl . $file_name;

                if(Config::get('filesystems.default') == "s3") {
                    Storage::disk('s3')->put($path, $imageData);
                } else {
                    Storage::disk('public')->put($path, $imageData);
                }
                return ['code' => 200, 'data' => $file_name, 'msg' => ''];
            } else if (preg_match('/data:application\/(.+);base64,(.*)/', $file, $matchings)) {
                $imageData = base64_decode($matchings[2]);
                $extension = $matchings[1];
                if($extension == 'vnd.openxmlformats-officedocument.wordprocessingml.document')
                    $extension = 'docx';
                if($extension == 'msword')
                    $extension = 'doc';
                $file_name = date('YmdHis') . rand(100, 999) . '_' . $dynamic_id . '.' . $extension;
                $path = $pathUrl . $file_name;

                if(Config::get('filesystems.default') == "s3") {
                    Storage::disk('s3')->put($path, $file_name);
                } else {
                    Storage::disk('public')->put($path, $imageData);
                }
                return ['code' => 200, 'data' => $file_name, 'msg' => ''];
            } else {
                if($updateLogic) {
                    if(!empty($file)) {
                        if(Config::get('filesystems.default') == "s3") {
                            $explode = basename($file);
                            $file_name_explode = explode("?", $explode)[0];
                            $file_name = rawurldecode($file_name_explode);
                        } else {
                            $file_name = basename($file);
                        }
                        return ['code' => 200, 'data' => $file_name, 'msg' => ''];
                    } else {
                        return ['code' => 500, 'data' => [], 'msg' => __('digimed_validation.error_response.profile_upload_faild_withtype')];
                    }
                }
            }
        }
    }

    public function profileImage($image) {
        if(Config::get('filesystems.default') == "s3") {
            if (!empty($image) && Storage::disk('s3')->exists('digimed/images/profile-images/' . $image)) {
                $path = (config('filesystems.default') == 's3') ? Storage::temporaryUrl('digimed/images/profile-images/' . $image, now()->addMinutes(30)) : Storage::url('app/public/digimed/images/profile-images/' . $image);
            } else {
                $path = Storage::temporaryUrl('digimed/images/assets/img/user.png', now()->addMinutes(30));
            }
        } else {
            if (!empty($image) && Storage::exists('digimed/images/profile-images/' . $image)) {
                $path = (config('filesystems.default') == 's3') ? Storage::temporaryUrl('digimed/images/profile-images/' . $image, now()->addMinutes(30)) : Storage::disk('public')->url('digimed/images/profile-images/' . $image);
            } else {
                $path = Storage::url('digimed/images/assets/img/user.png');
            }
        }
        return $path;
    }

    public function getFileUrl($filePath)
    {
        $user_id = auth()->user()->id;
        if (!empty($filePath)) {


            if (Config::get('filesystems.default') == "s3") {
                echo "s3 test";
                if (Storage::disk('s3')->exists('digimed/images/patient-documents/'.$user_id.'/' . $filePath)) {
                    echo "disk";
                    return Storage::disk('s3')->temporaryUrl('digimed/images/patient-documents/' . $filePath, now()->addMinutes(30));
                }
                else{
                    echo "no path";
                }
            } else {
                echo  "else";
                if (Storage::exists('digimed/images/patient-documents/' . $filePath)) {
                    return Storage::url('digimed/images/patient-documents/' . $filePath);
                }
            }
        }
        else{
            echo "empty";
        }

        return null;
    }

    public function dependentRelationshipProofUrl($image, $id) {
        /*print_r(Storage::disk('s3')->directories('digimed/images/authenticate_relationship'));
        print_r(Storage::disk('s3')->files('digimed/images/authenticate_relationship/173836.jpg'));
        print_r(Storage::disk('s3')->files('digimed/images/profile-images'));exit;*/

        if(Config::get('filesystems.default') == "s3") {
            if (!empty($image) && Storage::disk('s3')->exists('digimed/images/authenticate_relationship/'.$id.'/'.$image)) {
                $path = (config('filesystems.default') == 's3') ? Storage::temporaryUrl('digimed/images/authenticate_relationship/'.$id.'/'.$image, now()->addMinutes(5)) : Storage::url('app/public/digimed/images/authenticate_relationship/'.$id.'/'.$image);
            } else {
                $path = NULL;
            }
        } else {
            if (!empty($image) && Storage::exists('digimed/images/authenticate_relationship/'.$id.'/'.$image)) {
                $path = (config('filesystems.default') == 's3') ? Storage::temporaryUrl('digimed/images/authenticate_relationship/'.$id.'/'.$image, now()->addMinutes(5)) : Storage::url('app/public/digimed/images/authenticate_relationship/'.$id.'/'.$image);
            } else {
                $path = NULL;
            }
        }
        return $path;
    }

    /**
     * [s3Url description]
     * @param string $fileName
     * @param int|null $id
     * @param string $url
     * @return string|null
     */
    public function s3Url($fileName, $id, $url) {
        if($id != null) { $id = '/'.$id; } else { $id = ''; }
        if(Config::get('filesystems.default') == "s3") { $urlPath = $url; } else { $urlPath = $url; }
        if (Storage::disk('s3')->exists($urlPath.$id.'/'.$fileName)) {
            $path = (config('filesystems.default') == 's3') ? Storage::temporaryUrl($urlPath.$id.'/'.$fileName, now()->addMinutes(30)) : Storage::url($urlPath.$id.'/'.$fileName);
        } else {
            if (Storage::disk('public')->exists($urlPath.$id.'/'.$fileName)) {
                $path = Storage::url($urlPath.$id.'/'.$fileName);
            } else {
                $path = NULL;
            }
        }
        return $path;
    }

    public function is_base64($file)
    {
      if (preg_match('/data:image\/(.+);base64,(.*)/', $file, $matchings)) {
        return true;
      } else if(preg_match('/data:application\/(.+);base64,(.*)/', $file, $matchings)) {
        return true;
      } else {
        return false;
      }
    }
}

//Storage::disk('s3')->delete('path/to/image/in/bucket');
// $path = 'images/profile-images/20220127132736870_70.png';
// $exists = Storage::disk('s3')->exists($path);
/*print_r(Storage::temporaryUrl('digimed/images/profile-images/20220128163102620_70.png', now()->addMinutes(5)));
                exit;*/
                // echo Config::get('filesystems.default');exit;
                //Storage::disk('s3')->directories();