解决File Browser运行时因锁定数据库而无法插入用户的问题
File Browser(文件浏览器)是一种基于GO语言开发的开源轻量级文件管理系统,它能够在浏览器上管理你的文件,让您可以在浏览器上使用查看、下载、修改等各种功能。 你可以把它当作私有云盘来使用。
File Browser最吸引我的地方就是可以通过shell命令进行管理,通过 filebrowser config 等等命令进行配置,参考 Command Line Interface 文档。例如想插入用户信息,则可以执行:
filebrowser users add root password --perm.admin
但File Browser运行过程中由于事务原因将数据库锁定了,执行命令提示timeout。
Activating privacy features... done.
2022/05/13 10:36:24 timeout
github翻找了一遍,发现很多人在问这个问题:
https://github.com/filebrowser/filebrowser/issues/210
https://github.com/filebrowser/filebrowser/issues/627
大家提供2种思路:
1、使用pm2关闭服务,插入用户信息再启动服务
2、将数据库复制一份,将用户信息写入未锁定的数据库,然后关闭服务后替换数据库文件。
思路虽好,但不满足我的需求,最后使出php+curl大法,模拟管理员登陆,模拟管理员新增用户,直接上代码:
<?php header('Content-Type:application/json; charset=utf-8'); function doCurlPostRequest($url = '',Array $data = array(),$auth) { $data_string = json_encode($data,JSON_UNESCAPED_UNICODE); $curl_con = curl_init(); curl_setopt($curl_con, CURLOPT_URL,$url); curl_setopt($curl_con, CURLOPT_HEADER, false); curl_setopt($curl_con, CURLOPT_POST, true); curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($curl_con, CURLOPT_HTTPHEADER,array( "Content-Type: application/json", "Content-Length: " . strlen($data_string) . "", "X-Auth: ".$auth) ); curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data_string); $res = curl_exec($curl_con); $status = curl_getinfo($curl_con); curl_close($curl_con); return $res; } //filebrowser服务地址 $weburl = 'http://192.168.2.100:3331'; //管理员账号 $admin_username = "admin"; $admin_userpass = "admin"; //新增用户信息 $newuser_username = "zhangsan"; $newuser_userpass = "123457"; $param = array( "username"=>$admin_username, "password"=>$admin_userpass, "recaptcha"=>"", ); //模拟登录,此处url改为filebrowser服务地址 $auth = doCurlPostRequest($weburl.'/api/login',$param,''); if(trim($auth)=="403 Forbidden"){ echo json_encode(array("code" => 0,"data" => '新增用户失败,校验未通过')); exit; } //添加用户,参数含义,可自己使用filebrowser新增一个用户看参数 $addUser = array( "what"=>"user", "which"=>[], "data"=>array( "scope"=>".", "locale"=>"zh-cn", "viewMode"=>"mosaic", "singleClick"=>false, "sorting"=>array("by"=>"","asc"=>false), "perm"=>array("admin"=>false,"execute"=>false,"create"=>true,"rename"=>true,"modify"=>true,"delete"=>true,"share"=>true,"download"=>true), "commands"=>[], "hideDotfiles"=>false, "dateFormat"=>false, "username"=>$newuser_username, "passsword"=>"", "rules"=>[], "lockPassword"=>false, "id"=>0, "password"=>$newuser_userpass ), ); $user = doCurlPostRequest($weburl.'/api/users',$addUser,$auth); if(trim($user)=="403 Forbidden"){ echo json_encode(array("code" => 0,"data" => '新增用户失败,cookie校验未通过')); exit; }else if(trim($user)=="500 Internal Server Error"){ echo json_encode(array("code" => 0,"data" => '新增用户失败,可能用户已存在')); exit; }else if(trim($user)=="201 Created"){ echo json_encode(array("code" => 1,"data" => '新增用户成功')); exit; }
其它系统想同步新增网盘用户时,直接调php接口即可。