You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中使用Elfinder如何仅显示Google云存储的指定目录

如何在laravel-elfinder中隐藏GCS除指定目录外的所有文件夹?

我已成功对接Google Cloud Storage(GCS),所有基础功能正常,但现在需要实现:隐藏GCS中除指定目录外的所有文件夹,且用户对该指定目录拥有完整权限。使用的是barryvdh/laravel-elfinder": "^0.5.2"版本。

我的配置文件除以下部分外均为默认设置:

/*
|--------------------------------------------------------------------------
| Upload dir
|--------------------------------------------------------------------------
|
| The dir where to store the images (relative from public)
|
*/
'dir' => 'none',

/*
|--------------------------------------------------------------------------
| Filesystem disks (Flysytem)
|--------------------------------------------------------------------------
|
| Define an array of Filesystem disks, which use Flysystem.
| You can set extra options, example:
|
| 'my-disk' => [
|        'URL' => url('to/disk'),
|        'alias' => 'Local storage',
|    ]
*/
'disks' => [
    'gcs' => [
        'alias' => 'Cloud File Storage'
    ]
],

我编写了一个中间件,但其中的两个函数未达到预期效果:

public function handle(Request $request, Closure $next){
        $disk = Storage::disk('gcs');
        $thisOrgId = current_org()->ID;
        $thisOrgName = current_org()->OrganizationName;
        $dirName = $thisOrgId;
        if ($dirName != '' && !$disk->directoryExists($dirName)) {
            $disk->makeDirectory($dirName);
        }
        $dirs = $disk->directories();
        $this->app = app();
        // $roots = $this->app->config->get('elfinder.roots', []);
        if (empty($roots)) {
            $roots = [];
            foreach ($dirs as $key => $dir) {
                // if the dir should be shown
                if($dir == $thisOrgId){
                    array_push($roots , [
                    'driver' => 'gcs', // driver for accessing file system (REQUIRED)
                    'path' => $dir, // path to files (REQUIRED)
                    'startPath' => $dir,
                    'URL' => config('app.google.bucket_prefix'), // URL to files (REQUIRED)
                    'accessControl' => $this->access(
                        'hidden',
                        config('app.google.bucket_prefix').'/'.$dir,'gcs',
                        'accessControlData',
                        true,
                        '/'.$dir),
                    'alias' => $thisOrgName,
                    'uploadAllow' => array('image'),
                    'uploadMaxSize' => '5M',
                    'attributes' => array(
                        array(
                            'pattern' => '/\'/', // Dont write or delete to this but subfolders and files
                            'read' => true,
                            'write' => true,
                            'locked' => true
                            )
                        )
                    ]);
                } else {
                    // hide the dir
                    array_push($roots , [
                        'driver' => 'gcs', // driver for accessing file system (REQUIRED)
                        'path' => $dir, // path to files (REQUIRED)
                        'URL' => config('app.google.bucket_prefix'), // URL to files (REQUIRED)
                        'attributes' => array(
                            array(
                                'pattern'=>$dir,
                                'hidden'=>true
                                )
                            )
                        ]);
                }
            }
        }
        $opts = $this->app->config->get('elfinder.options', array());
        
        $bind = '';
        $plugin = '';
        $opts = array_merge(['bind'=>$bind,'plugin'=>$plugin,'roots' => $roots], $opts);
        
        return $next($request);
    }
function access($attr, $path, $data, $volume, $isDir, $relpath) {
    $basename = basename($path);
    return $basename[0] === '.'                  // if file/folder begins with '.' (dot)
             && strlen($relpath) !== 1           // but with out volume root
        ? !($attr == 'read' || $attr == 'write') // set read+write to false, other (locked+hidden) set to true
        :  null;                                 // else elFinder decide it itself
}

我打印输出的配置结构和示例一致,但功能未达预期,请问我哪里出错了?

elFinder界面截图

内容的提问来源于stack exchange,提问作者Lance

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 20:47:02