isTemp() ? [$user->userid] : [0, $user->userid]; $builder = File::wherePid($pid); if ($pid > 0) { File::permissionFind($pid, $userids, 0, $permission); } else { $builder->whereUserid($user->userid); } // $array = $builder->take(500)->get()->toArray(); foreach ($array as &$item) { $item['permission'] = $permission; } // if ($pid > 0) { // 遍历获取父级 while ($pid > 0) { $file = File::whereId($pid)->first(); if (empty($file)) { break; } $pid = $file->pid; $temp = $file->toArray(); $temp['permission'] = $file->getPermission($userids); $array[] = $temp; } // 去除没有权限的文件 $isUnset = false; foreach ($array as $index1 => $item1) { if ($item1['permission'] === -1) { foreach ($array as $index2 => $item2) { if ($item2['pid'] === $item1['id']) { $array[$index2]['pid'] = 0; } } $isUnset = true; unset($array[$index1]); } } if ($isUnset) { $array = array_values($array); } } else { // 获取共享相关 DB::statement("SET SQL_MODE=''"); $pre = DB::connection()->getTablePrefix(); $list = File::select(["files.*", DB::raw("MAX({$pre}file_users.permission) as permission")]) ->join('file_users', 'files.id', '=', 'file_users.file_id') ->where('files.userid', '!=', $user->userid) ->whereIn('file_users.userid', $userids) ->groupBy('files.id') ->take(100) ->get(); if ($list->isNotEmpty()) { foreach ($list as $file) { $temp = $file->toArray(); $temp['pid'] = 0; $array[] = $temp; } } } // 图片直接返回预览地址 foreach ($array as &$item) { $item = File::handleImageUrl($item); } return Base::retSuccess('success', $array); } /** * @api {get} api/file/one 02. 获取单条数据 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName one * * @apiParam {Number|String} id * - Number 文件ID(需要登录) * - String 链接码(不需要登录,用于预览) * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function one() { $id = Request::input('id'); // $permission = 0; if (Base::isNumber($id)) { $user = User::auth(); $file = File::permissionFind(intval($id), $user, 0, $permission); } elseif ($id) { $fileLink = FileLink::whereCode($id)->first(); $file = $fileLink?->file; if (empty($file)) { $msg = '文件链接不存在'; $data = File::code2IdName($id); if ($data) { $msg = "【{$data->name}】 {$msg}"; } return Base::retError($msg, $data); } } else { return Base::retError('参数错误'); } // $array = $file->toArray(); $array['permission'] = $permission; return Base::retSuccess('success', $array); } /** * @api {get} api/file/search 03. 搜索文件列表 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName search * * @apiParam {String} [link] 通过分享地址搜索(如:https://t.hitosea.com/single/file/ODcwOCwzOSxpa0JBS2lmVQ==) * @apiParam {String} [key] 关键词 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function search() { $user = User::auth(); // $link = trim(Request::input('link')); $key = trim(Request::input('key')); $id = 0; $take = 50; if (preg_match("/\/single\/file\/(.*?)$/i", $link, $match)) { $id = intval(FileLink::whereCode($match[1])->value('file_id')); $take = 1; if (empty($id)) { return Base::retSuccess('success', []); } } // 搜索自己的 $builder = File::whereUserid($user->userid); if ($id) { $builder->where("id", $id); } if ($key) { $builder->where("name", "like", "%{$key}%"); } $array = $builder->take($take)->get()->toArray(); // 搜索共享的 $take = $take - count($array); if ($take > 0 && ($id || $key)) { $builder = File::whereIn('pshare', function ($queryA) use ($user) { $queryA->select('files.id') ->from('files') ->join('file_users', 'files.id', '=', 'file_users.file_id') ->where('files.userid', '!=', $user->userid) ->where(function ($queryB) use ($user) { $queryB->whereIn('file_users.userid', [0, $user->userid]); }); }); if ($id) { $builder->where("id", $id); } if ($key) { $builder->where("name", "like", "%{$key}%"); } $list = $builder->take($take)->get(); if ($list->isNotEmpty()) { foreach ($list as $file) { $temp = $file->toArray(); if ($file->pshare === $file->id) { $temp['pid'] = 0; } $array[] = $temp; } } } // return Base::retSuccess('success', $array); } /** * @api {get} api/file/add 04. 添加、修改文件(夹) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName add * * @apiParam {String} name 项目名称 * @apiParam {String} type 文件类型 * @apiParam {Number} [id] 文件ID(赋值修改文件名称) * @apiParam {Number} [pid] 父级ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function add() { $user = User::auth(); // 文件名称 $name = trim(Request::input('name')); $type = trim(Request::input('type')); $id = intval(Request::input('id')); $pid = intval(Request::input('pid')); if (mb_strlen($name) < 2) { return Base::retError('文件名称不可以少于2个字'); } elseif (mb_strlen($name) > 32) { return Base::retError('文件名称最多只能设置32个字'); } $tmpName = preg_replace("/[\\\\\/:*?\"<>|]/", '', $name); if ($tmpName != $name) { return Base::retError("文件名称不能包含这些字符:\/:*?\"<>|"); } // if ($id > 0) { // 修改 $file = File::permissionFind($id, $user, 1); // $file->name = $name; $file->handleDuplicateName(); $file->save(); $data = [ 'id' => $file->id, 'name' => $file->name, ]; $file->pushMsg('update', $data); return Base::retSuccess('修改成功', $data); } else { // 添加 if (!in_array($type, [ 'folder', 'document', 'mind', 'drawio', 'word', 'excel', 'ppt', ])) { return Base::retError('类型错误'); } $ext = str_replace([ 'folder', 'document', 'mind', 'drawio', 'word', 'excel', 'ppt', ], [ '', 'md', 'mind', 'drawio', 'docx', 'xlsx', 'pptx', ], $type); // $userid = $user->userid; if ($pid > 0) { if (File::wherePid($pid)->count() >= 300) { return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); } $row = File::permissionFind($pid, $user, 1); $userid = $row->userid; } else { if (File::whereUserid($user->userid)->wherePid(0)->count() >= 300) { return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); } } // 开始创建 $file = File::createInstance([ 'pid' => $pid, 'name' => $name, 'type' => $type, 'ext' => $ext, 'userid' => $userid, 'created_id' => $user->userid, ]); $file->handleDuplicateName(); $file->saveBeforePP(); // $data = File::find($file->id); $data->pushMsg('add', $data); return Base::retSuccess('添加成功', $data); } } /** * @api {get} api/file/copy 05. 复制文件(夹) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName copy * * @apiParam {Number} id 文件ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function copy() { $user = User::auth(); // $id = intval(Request::input('id')); // $row = File::permissionFind($id, $user); // $userid = $user->userid; if ($row->pid > 0) { $userid = intval(File::whereId($row->pid)->value('userid')); } // if ($row->type == 'folder') { return Base::retError('不支持复制文件夹'); } $num = File::whereCid($row->id)->count() + 1; $name = $row->name . " ({$num})"; // 开始复制 $file = File::createInstance([ 'cid' => $row->id, 'pid' => $row->pid, 'name' => $name, 'type' => $row->type, 'ext' => $row->ext, 'userid' => $userid, 'created_id' => $user->userid, ]); $file->handleDuplicateName(); $data = AbstractModel::transaction(function() use ($file) { $content = FileContent::select(['content', 'text', 'size'])->whereFid($file->cid)->orderByDesc('id')->first(); $file->size = $content?->size ?: 0; $file->saveBeforePP(); if ($content) { $content = $content->toArray(); $content['fid'] = $file->id; $content['userid'] = $file->userid; FileContent::createInstance($content)->save(); } return File::find($file->id); }); // $data->pushMsg('add', $data); return Base::retSuccess('复制成功', $data); } /** * @api {get} api/file/move 06. 移动文件(夹) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName move * * @apiParam {Numbers} ids 文件ID(格式:[id1, id2]) * @apiParam {Number} pid 移动到的文件夹ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function move() { $user = User::auth(); // $ids = Request::input('ids'); $pid = intval(Request::input('pid')); // if (!is_array($ids) || empty($ids)) { return Base::retError('请选择移动的文件或文件夹'); } if (count($ids) > 100) { return Base::retError('一次最多只能移动100个文件或文件夹'); } $toShareFile = false; if ($pid > 0) { $tmpFile = File::permissionFind($pid, $user, 1); $toShareFile = $tmpFile->getShareInfo(); } // $files = []; AbstractModel::transaction(function() use ($user, $pid, $ids, $toShareFile, &$files) { foreach ($ids as $id) { $file = File::permissionFind($id, $user, 1000); // if ($pid > 0) { if ($toShareFile) { if ($file->share) { throw new ApiException("{$file->name} 当前正在共享,无法移动到另一个共享文件夹内"); } if ($file->isSubShare()) { throw new ApiException("{$file->name} 内含有共享文件,无法移动到另一个共享文件夹内"); } $file->userid = $toShareFile->userid; File::where('pids', 'LIKE', "%,{$file->id},%")->update(['userid' => $toShareFile->userid]); } // $tmpId = $pid; while ($tmpId > 0) { if ($id == $tmpId) { throw new ApiException('移动位置错误'); } $tmpId = intval(File::whereId($tmpId)->value('pid')); } } else { $file->userid = $user->userid; File::where('pids', 'LIKE', "%,{$file->id},%")->update(['userid' => $user->userid]); } // $file->pid = $pid; $file->handleDuplicateName(); $file->saveBeforePP(); $files[] = $file; } }); foreach ($files as $file) { $file->pushMsg('update', $file); } return Base::retSuccess('操作成功', $files); } /** * @api {get} api/file/remove 07. 删除文件(夹) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName remove * * @apiParam {Numbers} ids 文件ID(格式:[id1, id2]) * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function remove() { $user = User::auth(); // $ids = Request::input('ids'); // if (!is_array($ids) || empty($ids)) { return Base::retError('请选择删除的文件或文件夹'); } if (count($ids) > 100) { return Base::retError('一次最多只能删除100个文件或文件夹'); } // $files = []; AbstractModel::transaction(function() use ($user, $ids, &$files) { foreach ($ids as $id) { $file = File::permissionFind($id, $user, 1000); $file->deleteFile(); $files[] = $file; } }); // return Base::retSuccess('删除成功', $files); } /** * @api {get} api/file/content 08. 获取文件内容 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content * * @apiParam {Number|String} id * - Number: 文件ID(需要登录) * - String: 链接码(不需要登录,用于预览) * @apiParam {String} only_update_at 仅获取update_at字段 * - no (默认) * - yes * @apiParam {String} down 直接下载 * - no: 浏览(默认) * - yes: 下载(office文件直接下载,除非是preview) * - preview: 转预览地址 * @apiParam {Number} [history_id] 读取历史记录ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content() { $id = Request::input('id'); $down = Request::input('down', 'no'); $only_update_at = Request::input('only_update_at', 'no'); $history_id = intval(Request::input('history_id')); // if (Base::isNumber($id)) { $user = User::auth(); $file = File::permissionFind(intval($id), $user); } elseif ($id) { $fileLink = FileLink::whereCode($id)->first(); $file = $fileLink?->file; if (empty($file)) { $msg = '文件链接不存在'; $data = File::code2IdName($id); if ($data) { $msg = "【{$data->name}】 {$msg}"; } return Base::retError($msg, $data); } } else { return Base::retError('参数错误'); } // if ($only_update_at == 'yes') { return Base::retSuccess('success', [ 'id' => $file->id, 'update_at' => Carbon::parse($file->updated_at)->toDateTimeString() ]); } // $builder = FileContent::whereFid($file->id); if ($history_id > 0) { $builder->whereId($history_id); } $content = $builder->orderByDesc('id')->first(); if ($down === 'preview') { return Redirect::to(FileContent::formatPreview($file, $content?->content)); } return FileContent::formatContent($file, $content?->content, $down == 'yes'); } /** * @api {get} api/file/content/save 09. 保存文件内容 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content__save * * @apiParam {Number} id 文件ID * @apiParam {Object} [D] Request Payload 提交 * - content: 内容 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content__save() { $user = User::auth(); // $id = intval(Request::input('id')); $content = Request::input('content'); // $file = File::permissionFind($id, $user, 1); // $text = ''; if ($file->type == 'document') { $data = Base::json2array($content); $isRep = false; preg_match_all("/ $text) { $tmpPath = "uploads/file/document/" . date("Ym") . "/" . $id . "/attached/"; Base::makeDir(public_path($tmpPath)); $tmpPath .= md5($text) . "." . $matchs[1][$key]; if (Base::saveContentImage(public_path($tmpPath), base64_decode($text))) { $paramet = getimagesize(public_path($tmpPath)); $data['content'] = str_replace($matchs[0][$key], 'type) { case 'document': $contentArray = Base::json2array($content); $contentString = $contentArray['content']; $file->ext = $contentArray['type'] == 'md' ? 'md' : 'text'; break; case 'drawio': $contentArray = Base::json2array($content); $contentString = $contentArray['xml']; $file->ext = 'drawio'; break; case 'mind': $contentString = $content; $file->ext = 'mind'; break; case 'txt': case 'code': $contentArray = Base::json2array($content); $contentString = $contentArray['content']; break; default: return Base::retError('参数错误'); } $path = "uploads/file/" . $file->type . "/" . date("Ym") . "/" . $id . "/" . md5($contentString); $save = public_path($path); Base::makeDir(dirname($save)); file_put_contents($save, $contentString); // $content = FileContent::createInstance([ 'fid' => $file->id, 'content' => [ 'type' => $file->ext, 'url' => $path ], 'text' => $text, 'size' => filesize($save), 'userid' => $user->userid, ]); $content->save(); // $file->size = $content->size; $file->save(); $file->pushMsg('content'); // return Base::retSuccess('保存成功', $content); } /** * @api {get} api/file/content/office 10. 保存文件内容(office) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content__office * * @apiParam {Number} id 文件ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content__office() { $user = User::auth(); // $id = intval(Request::input('id')); $status = intval(Request::input('status')); $key = Request::input('key'); $url = Request::input('url'); // $file = File::permissionFind($id, $user, 1); // if ($status === 2) { $parse = parse_url($url); $from = 'http://' . env('APP_IPPR') . '.3' . $parse['path'] . '?' . $parse['query']; $path = 'uploads/file/' . $file->type . '/' . date("Ym") . '/' . $file->id . '/' . $key; $save = public_path($path); Base::makeDir(dirname($save)); $res = Ihttp::download($from, $save); if (Base::isSuccess($res)) { $content = FileContent::createInstance([ 'fid' => $file->id, 'content' => [ 'from' => $from, 'url' => $path ], 'text' => '', 'size' => filesize($save), 'userid' => $user->userid, ]); $content->save(); // $file->size = $content->size; $file->updated_at = Carbon::now(); $file->save(); $file->pushMsg('update', $file); } } return ['error' => 0]; } /** * @api {get} api/file/content/upload 11. 保存文件内容(上传文件) * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content__upload * * @apiParam {Number} [pid] 父级ID * @apiParam {String} [files] 文件名 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content__upload() { $user = User::auth(); // $pid = intval(Request::input('pid')); $webkitRelativePath = Request::input('webkitRelativePath'); // $userid = $user->userid; if ($pid > 0) { if (File::wherePid($pid)->count() >= 300) { return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); } $row = File::permissionFind($pid, $user, 1); $userid = $row->userid; } else { if (File::whereUserid($user->userid)->wherePid(0)->count() >= 300) { return Base::retError('每个文件夹里最多只能创建300个文件或文件夹'); } } // $dirs = explode("/", $webkitRelativePath); $addItem = []; while (count($dirs) > 1) { $dirName = array_shift($dirs); if ($dirName) { AbstractModel::transaction(function () use ($dirName, $user, $userid, &$pid, &$addItem) { $dirRow = File::wherePid($pid)->whereType('folder')->whereName($dirName)->lockForUpdate()->first(); if (empty($dirRow)) { $dirRow = File::createInstance([ 'pid' => $pid, 'type' => 'folder', 'name' => $dirName, 'userid' => $userid, 'created_id' => $user->userid, ]); $dirRow->handleDuplicateName(); if ($dirRow->saveBeforePP()) { $addItem[] = File::find($dirRow->id); } } if (empty($dirRow)) { throw new ApiException('创建文件夹失败'); } $pid = $dirRow->id; }); foreach ($addItem as $tmpRow) { $tmpRow->pushMsg('add', $tmpRow); } } } // $path = 'uploads/tmp/' . date("Ym") . '/'; $data = Base::upload([ "file" => Request::file('files'), "type" => 'more', "autoThumb" => false, "path" => $path, ]); if (Base::isError($data)) { return $data; } $data = $data['data']; // $type = match ($data['ext']) { 'text', 'md', 'markdown' => 'document', 'drawio' => 'drawio', 'mind' => 'mind', 'doc', 'docx' => "word", 'xls', 'xlsx' => "excel", 'ppt', 'pptx' => "ppt", 'wps' => "wps", 'jpg', 'jpeg', 'webp', 'png', 'gif', 'bmp', 'ico', 'raw', 'svg' => "picture", 'rar', 'zip', 'jar', '7-zip', 'tar', 'gzip', '7z', 'gz', 'apk', 'dmg' => "archive", 'tif', 'tiff' => "tif", 'dwg', 'dxf' => "cad", 'ofd' => "ofd", 'pdf' => "pdf", 'txt' => "txt", 'htaccess', 'htgroups', 'htpasswd', 'conf', 'bat', 'cmd', 'cpp', 'c', 'cc', 'cxx', 'h', 'hh', 'hpp', 'ino', 'cs', 'css', 'dockerfile', 'go', 'golang', 'html', 'htm', 'xhtml', 'vue', 'we', 'wpy', 'java', 'js', 'jsm', 'jsx', 'json', 'jsp', 'less', 'lua', 'makefile', 'gnumakefile', 'ocamlmakefile', 'make', 'mysql', 'nginx', 'ini', 'cfg', 'prefs', 'm', 'mm', 'pl', 'pm', 'p6', 'pl6', 'pm6', 'pgsql', 'php', 'inc', 'phtml', 'shtml', 'php3', 'php4', 'php5', 'phps', 'phpt', 'aw', 'ctp', 'module', 'ps1', 'py', 'r', 'rb', 'ru', 'gemspec', 'rake', 'guardfile', 'rakefile', 'gemfile', 'rs', 'sass', 'scss', 'sh', 'bash', 'bashrc', 'sql', 'sqlserver', 'swift', 'ts', 'typescript', 'str', 'vbs', 'vb', 'v', 'vh', 'sv', 'svh', 'xml', 'rdf', 'rss', 'wsdl', 'xslt', 'atom', 'mathml', 'mml', 'xul', 'xbl', 'xaml', 'yaml', 'yml', 'asp', 'properties', 'gitignore', 'log', 'bas', 'prg', 'python', 'ftl', 'aspx', 'plist' => "code", 'mp3', 'wav', 'mp4', 'flv', 'avi', 'mov', 'wmv', 'mkv', '3gp', 'rm' => "media", 'xmind' => "xmind", 'rp' => "axure", default => "", }; if ($data['ext'] == 'markdown') { $data['ext'] = 'md'; } $file = File::createInstance([ 'pid' => $pid, 'name' => Base::rightDelete($data['name'], '.' . $data['ext']), 'type' => $type, 'ext' => $data['ext'], 'userid' => $userid, 'created_id' => $user->userid, ]); $file->handleDuplicateName(); // 开始创建 return AbstractModel::transaction(function () use ($addItem, $webkitRelativePath, $type, $user, $data, $file) { $file->size = $data['size'] * 1024; $file->saveBeforePP(); // $data = Base::uploadMove($data, "uploads/file/" . $file->type . "/" . date("Ym") . "/" . $file->id . "/"); $content = [ 'from' => '', 'type' => $type, 'ext' => $data['ext'], 'url' => $data['path'], ]; if (isset($data['width'])) { $content['width'] = $data['width']; $content['height'] = $data['height']; } $content = FileContent::createInstance([ 'fid' => $file->id, 'content' => $content, 'text' => '', 'size' => $file->size, 'userid' => $user->userid, ]); $content->save(); // $tmpRow = File::find($file->id); $tmpRow->pushMsg('add', $tmpRow); // $data = File::handleImageUrl($tmpRow->toArray()); $data['full_name'] = $webkitRelativePath ?: $data['name']; // $addItem[] = $data; return Base::retSuccess($data['name'] . ' 上传成功', $addItem); }); } /** * @api {get} api/file/content/history 12. 获取内容历史 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content__history * * @apiParam {Number} id 文件ID * * @apiParam {Number} [page] 当前页,默认:1 * @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:100 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content__history() { $user = User::auth(); // $id = Request::input('id'); // $file = File::permissionFind(intval($id), $user); // $data = FileContent::select(['id', 'size', 'userid', 'created_at']) ->whereFid($file->id) ->orderByDesc('id') ->paginate(Base::getPaginate(100, 20)); return Base::retSuccess('success', $data); } /** * @api {get} api/file/content/restore 13. 恢复文件历史 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName content__restore * * @apiParam {Number} id 文件ID * @apiParam {Number} history_id 历史数据ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function content__restore() { $user = User::auth(); // $id = intval(Request::input('id')); $history_id = intval(Request::input('history_id')); // $file = File::permissionFind($id, $user); // $history = FileContent::whereFid($file->id)->whereId($history_id)->first(); if (empty($history)) { return Base::retError('历史数据不存在或已被删除'); } // $content = $history->replicate(); $content->userid = $user->userid; $content->save(); // $file->size = $content->size; $file->save(); $file->pushMsg('content'); // return Base::retSuccess('还原成功'); } /** * @api {get} api/file/share 14. 获取共享信息 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName share * * @apiParam {Number} id 文件ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function share() { $user = User::auth(); // $id = intval(Request::input('id')); // $file = File::whereId($id)->first(); if (empty($file)) { return Base::retError('文件不存在或已被删除'); } if ($file->userid != $user->userid) { return Base::retError('仅限所有者操作'); } // $list = FileUser::whereFileId($file->id)->get(); // return Base::retSuccess('success', [ 'id' => $file->id, 'list' => $list ]); } /** * @api {get} api/file/share/update 15. 设置共享 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName share__update * * @apiParam {Number} id 文件ID * @apiParam {Array} [userids] 共享成员,格式: [userid1, userid2, userid3] * @apiParam {Number} [permission] 共享方式 * - 0:只读 * - 1:读写 * - -1: 删除 * @apiParam {Number} [force] 设置共享时是否忽略提醒 * - 0:如果子文件夹已存在共享则ret返回-3001(默认) * - 1:忽略提醒 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function share__update() { $user = User::auth(); // $id = intval(Request::input('id')); $userids = Request::input('userids'); $permission = intval(Request::input('permission')); $force = intval(Request::input('force')); // if (!in_array($permission, [-1, 0, 1])) { return Base::retError('参数错误'); } // $file = File::whereId($id)->first(); if (empty($file)) { return Base::retError('文件不存在或已被删除'); } if ($file->userid != $user->userid) { return Base::retError('仅限所有者操作'); } // $share = $file->isNnShare(); if ($share) { $typeCn = $file->type === 'folder' ? '文件夹' : '文件'; return Base::retError("此{$typeCn}已经处于【{$share->name}】共享文件夹中,无法重复共享"); } // if (!is_array($userids) || empty($userids)) { return Base::retError('请选择共享对象'); } // $array = []; if ($permission === -1) { // 取消共享 $action = "delete"; foreach ($userids as $userid) { if (FileUser::deleteFileUser($file->id, $userid)) { $array[] = $userid; } } } else { // 设置共享 $action = "update"; if ($force === 0) { if ($file->isSubShare()) { return Base::retError('此文件夹内已有共享文件夹', [], -3001); } } if (FileUser::whereFileId($file->id)->count() + count($userids) > 100) { return Base::retError('共享人数上限100个成员'); } foreach ($userids as $userid) { if (FileUser::updateInsert([ 'file_id' => $file->id, 'userid' => $userid, ], [ 'permission' => $permission, ])) { $array[] = $userid; } } } // $file->updataShare(); $file->pushMsg($action, $action == "delete" ? null : $file, $array); return Base::retSuccess($action == "delete" ? "删除成功" : "设置成功", $file); } /** * @api {get} api/file/share/out 16. 退出共享 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName share__out * * @apiParam {Number} id 文件ID * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function share__out() { $user = User::auth(); // $id = intval(Request::input('id')); // $file = File::permissionFind($id, $user); // if ($file->userid == $user->userid) { return Base::retError('不能退出自己共享的文件'); } if (FileUser::whereFileId($file->id)->whereUserid(0)->exists()) { return Base::retError('无法退出共享所有人的文件或文件夹'); } FileUser::deleteFileUser($file->id, $user->userid); // $file->updataShare(); return Base::retSuccess("退出成功"); } /** * @api {get} api/file/link 17. 获取链接 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup file * @apiName link * * @apiParam {Number} id 文件ID * @apiParam {String} refresh 刷新链接 * - no: 只获取(默认) * - yes: 刷新链接,之前的将失效 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {Object} data 返回数据 */ public function link() { $user = User::auth(); // $id = intval(Request::input('id')); $refresh = Request::input('refresh', 'no'); // $file = File::permissionFind($id, $user); $fileLink = $file->getShareLink($user->userid, $refresh == 'yes'); // return Base::retSuccess('success', $fileLink); } }