mirror of
https://gitee.com/ulthon/ul-file-share.git
synced 2026-07-01 11:02:49 +08:00
完成分享存储
This commit is contained in:
@@ -5,16 +5,84 @@ namespace app\index\controller;
|
||||
use app\model\Category;
|
||||
use app\model\Nav;
|
||||
use app\model\Post;
|
||||
use app\model\User;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
use think\helper\Str;
|
||||
use UserHub\Client;
|
||||
|
||||
class Common extends BaseController
|
||||
{
|
||||
public $modelUser = null;
|
||||
|
||||
public function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
|
||||
$user_id = Session::get('user_id');
|
||||
|
||||
if (!empty($user_id)) {
|
||||
$this->modelUser = User::find($user_id);
|
||||
View::assign('user', $this->modelUser);
|
||||
}else{
|
||||
$this->tempLogin();
|
||||
}
|
||||
|
||||
$this->ulthonLogin();
|
||||
}
|
||||
|
||||
public function tempLogin()
|
||||
{
|
||||
if (empty($this->modelUser)) {
|
||||
$temp_user = [];
|
||||
$temp_user['account'] = uniqid();
|
||||
|
||||
$temp_user['type'] = 'temp';
|
||||
|
||||
$model_user = User::create($temp_user);
|
||||
|
||||
$this->modelUser = $model_user;
|
||||
Session::set('user_id', $model_user->id);
|
||||
View::assign('user', $this->modelUser);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function ulthonLogin()
|
||||
{
|
||||
|
||||
if (empty($this->modelUser) || $this->modelUser->type != 'ulthon') {
|
||||
$code = $this->request->param('code');
|
||||
|
||||
$user_hub_client = new Client([
|
||||
'key' => get_system_config('user_hub_key'),
|
||||
'secret' => get_system_config('user_hub_secret'),
|
||||
'host' => get_system_config('user_hub_host'),
|
||||
]);
|
||||
if (empty($code)) {
|
||||
$url = $user_hub_client->getBowserRedirectUrl($this->request->url(true));
|
||||
View::assign('ulthon_login_url',$url);
|
||||
|
||||
} else {
|
||||
$user_info = $user_hub_client->getUserinfoByCode($code);
|
||||
|
||||
$model_user = User::where('uid', $user_info['uid'])->find();
|
||||
|
||||
if (empty($model_user)) {
|
||||
unset($user_info['id']);
|
||||
// TODO:处理数据,或者中转数据
|
||||
// TODO:下载头像
|
||||
|
||||
$user_info['type'] = 'ulthon';
|
||||
|
||||
$model_user = User::create((array)$user_info);
|
||||
}
|
||||
|
||||
$this->modelUser = $model_user;
|
||||
}
|
||||
Session::set('user_id', $this->modelUser->id);
|
||||
View::assign('user', $this->modelUser);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ class File extends Common
|
||||
{
|
||||
# code...
|
||||
|
||||
UploadFiles::setDisks('safe');
|
||||
|
||||
return UploadFiles::save($request);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,21 @@
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use Alchemy\Zippy\Zippy;
|
||||
use app\model\Category;
|
||||
use app\model\Nav;
|
||||
use app\model\Post;
|
||||
use app\model\PostCategory;
|
||||
use app\model\Share;
|
||||
use app\model\ShareFiles;
|
||||
use app\UploadFiles;
|
||||
use think\facade\App;
|
||||
use think\facade\Session;
|
||||
use think\facade\Validate;
|
||||
use think\facade\View;
|
||||
use think\File;
|
||||
use think\Request;
|
||||
use think\validate\ValidateRule;
|
||||
|
||||
class Index extends Common
|
||||
{
|
||||
@@ -20,6 +29,10 @@ class Index extends Common
|
||||
{
|
||||
//
|
||||
|
||||
$list_active_shares = Share::where('user_id',$this->modelUser->id)->where('status',0)->select();
|
||||
|
||||
View::assign('list_active_shares',$list_active_shares);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
@@ -43,6 +56,143 @@ class Index extends Common
|
||||
public function save(Request $request)
|
||||
{
|
||||
//
|
||||
|
||||
$post_data = $request->post();
|
||||
|
||||
$post_data['user_id'] = $this->modelUser->id;
|
||||
|
||||
$root_dir = App::getRootPath();
|
||||
|
||||
$safe_dir = '/safe/';
|
||||
|
||||
$file_preifx = $root_dir . $safe_dir;
|
||||
|
||||
|
||||
$validate = Validate::rule('files_list', ValidateRule::isRequire()->isArray()->requireCallback(function ($value) use ($file_preifx) {
|
||||
foreach ($value as $key_v => $value_v) {
|
||||
$file_path = $file_preifx . $value_v['save_name'];
|
||||
|
||||
if (!file_exists($file_path)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}, '文件不存在'))
|
||||
->rule('password', ValidateRule::length('6,18'))
|
||||
->rule('times', ValidateRule::requireCallback(function ($value) {
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is_numeric($value)) {
|
||||
if ($value > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}))
|
||||
->rule('expire', ValidateRule::isRequire()->isNumber()->min(1));
|
||||
|
||||
if (!$validate->check($post_data)) {
|
||||
return json_message($validate->getError());
|
||||
}
|
||||
|
||||
|
||||
$model_share = Share::create($post_data);
|
||||
|
||||
$total_size = 0;
|
||||
|
||||
$files_list = [];
|
||||
|
||||
foreach ($post_data['files_list'] as $key_file => $value_file) {
|
||||
|
||||
$file_path = $file_preifx . $value_file['save_name'];
|
||||
|
||||
UploadFiles::use($value_file['save_name']);
|
||||
|
||||
$file = new File($file_path);
|
||||
|
||||
$model_share_file = new ShareFiles();
|
||||
|
||||
$model_share_file->save_name = $value_file['save_name'];
|
||||
$model_share_file->file_name = $value_file['file_name'];
|
||||
|
||||
$model_share_file->mime_type = $file->getMime();
|
||||
|
||||
$model_share_file->file_size = $file->getSize();
|
||||
|
||||
$model_share_file->ext_name = $file->extension();
|
||||
|
||||
$model_share_file->file_md5 = $file->md5();
|
||||
$model_share_file->file_sha1 = $file->sha1();
|
||||
|
||||
$model_share_file->share_id = $model_share->id;
|
||||
|
||||
$model_share_file->save();
|
||||
|
||||
$file_name = $value_file['file_name'];
|
||||
|
||||
$file_name = $this->build_files_list_key($files_list, $file_name);
|
||||
|
||||
$files_list[$file_name] = $file_path;
|
||||
|
||||
|
||||
$total_size += $file->getSize();
|
||||
}
|
||||
|
||||
// 合成大文件
|
||||
|
||||
$zippy = Zippy::load();
|
||||
|
||||
$download_zip_save_name = 'build_download_zip/' . uniqid() . '.zip';
|
||||
|
||||
$build_download_path = $file_preifx . $download_zip_save_name;
|
||||
|
||||
$build_download_dir = dirname($build_download_path);
|
||||
|
||||
if (!is_dir($build_download_dir)) {
|
||||
mkdir($build_download_dir, 0777, true);
|
||||
}
|
||||
|
||||
$build_archive = $zippy->create($build_download_path, $files_list, true);
|
||||
|
||||
$model_share->build_download_save_name = $download_zip_save_name;
|
||||
|
||||
$model_share->total_size = $total_size;
|
||||
|
||||
$model_share->save();
|
||||
|
||||
return json_message();
|
||||
}
|
||||
|
||||
public function build_files_list_key($file_list, $file_key)
|
||||
{
|
||||
if (!isset($file_list[$file_key])) {
|
||||
return $file_key;
|
||||
}
|
||||
|
||||
|
||||
$file_name = $file_key;
|
||||
$file_name_others = [];
|
||||
if (strpos($file_key, '.') !== false) {
|
||||
$file_key_array = explode('.', $file_key);
|
||||
$file_name = array_shift($file_key_array);
|
||||
|
||||
$file_name_others = $file_key_array;
|
||||
}
|
||||
|
||||
$new_file_name = $file_name . '(1).' . implode('.', $file_name_others);
|
||||
|
||||
return $this->build_files_list_key($file_list, $new_file_name);
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
Session::clear();
|
||||
|
||||
return $this->success('已成功退出', url('index'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user