本地文件同步海豚附件方法

use app\admin\model\Attachment;


public function local_upload($dir = ''){
if($dir){
if(!is_dir($dir)){
$this->error('路径不存在');
}
set_time_limit(0);
ini_set('memory_limit', '3072M');
$reload = url();
if($this->request->isPost()){
$ids = input('ids/a', []);
if($ids){
foreach ($ids as $key => $path) {
$md5 = md5_file($path);
$exist = Attachment::where('md5', $md5)->find();
if($exist){
if(is_file($exist['path'])){
;
}else{
copy($path, $exist['path']);
}
}else{
$target_dir = 'uploads/files/'.date('Ymd');
if(!is_dir($target_dir)){
mkdir($target_dir);
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$target = $target_dir . DIRECTORY_SEPARATOR.md5((string) microtime(true)).'.'.pathinfo($path, PATHINFO_EXTENSION);
copy($path, $target);
$file_name = pathinfo($path, PATHINFO_BASENAME);
$file_info = [
'uid' => session('user_auth.uid'),
'name' => $file_name,
'mime' => $mime,
'path' => str_replace('\\', '/', $target),
'ext' => pathinfo($file_name, PATHINFO_EXTENSION),
'size' => filesize($path),
'md5' => md5_file($path),
'sha1' => sha1_file($path),
'thumb' => '',
'module' => 'admin',
'width' => '',
'height' => '',
];
Attachment::create($file_info);
}
}
$this->success('同步成功');
}else{
$this->error('请至少选择一个文件');
}
}
$current_dir = getcwd();
chdir($dir);
$data_list = [];
$files = glob('*.*');
chdir($current_dir);
if($files){
foreach ($files as $key => $file) {
$file = $dir.DIRECTORY_SEPARATOR.$file;
$type = is_dir(realpath($file))?'dir':'file';
if($type == 'file'){
$md5 = md5_file($file);
$exist = Attachment::where('md5', $md5)->find();
}else{
$md5 = '';
$exist = [];
}
$data_list[] = [
'index' => $key,
'md5' => $md5,
'path' => realpath($file),
'type' => is_dir(realpath($file))?'dir':'file',
'size' => filesize($file),
'status' => $exist? is_file($exist['path'])?'已同步'.$exist['id'] :'待恢复'.$exist['id']: '未同步',
];
}
}
$parent_dir = dirname($dir, 1);
$parent_dir = url('local_upload', ['dir'=>$parent_dir]);
$js = <<<JS
<script>
function browser(index){
\$path = \$.trim(\$('#builder-table table tbody tr').eq(index).find('td:eq(1) div').text());
location.href = '{$reload}'.replace('.html', '?dir='+\$path);
}
</script>
JS;
return ZBuilder::make('table')
->setTableName(true)
->addTopButton('upload', ['title'=>'同步', 'href'=>url('local_upload', ['dir'=>$dir]), 'class'=>'btn btn-default ajax-post'])
->addColumns([
// ['index', 'index', 'hidden'],
['path', 'path'],
['md5', 'md5'],
['size', 'size'],
['status', '状态'],
['right_button', '操作'],
])
->setPrimaryKey('path')
->setPageTips("当前路径为:{$dir}, <a href='{$parent_dir}'>返回上一级</a> <a href='{$reload}'>重新浏览</a>")
->addRightButton('sub_dir', [
'icon' => 'fa fa-fw fa-inbox',
'title' => '浏览子目录',
'href' => 'javascript:browser(__index__)',
])
->setExtraJs($js)
->replaceRightButton(['type'=>'file'], '', ['sub_dir'])
->setRowList($data_list)
->fetch(); // 渲染模板
}else{
return ZBuilder::make('form')
->setPageTips('请填写要同步的本地目录')
->addFormItems([
['text','dir', '待匹配的目录', '绝对路径格式'],
])
->method('get')
->isAjax(false)
->fetch();
}
}
上面是attachment 模型里加的获取器
 
然后自己加个节点 使用就好,
 

0 个评论

要回复文章请先登录注册