作者 liyuhang

gx

... ... @@ -100,25 +100,25 @@ class BaseLogic extends Logic
if(empty($image)){
return $this->fail('没有上传图片',Code::USER_ERROR);
}
$hash = hash_file('md5', $image->getPathname());
$url = './../uploads/images/';
$filename = date('ymdHis').rand(10000,99999);
$res = $this->request->file('image')->move($url,$filename);
if ($res === false) {
return $this->fail($image->getError(), Code::USER_ERROR);
}
$imageModel = new ImageModel();
$data = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => hash_file('md5', $res->getPathname()),
'hash' => $hash.$filename,
'type'=>$image->getClientOriginalExtension(),
// 'mime'=>$image->getMimeType()
];
$imageModel = new ImageModel();
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->fail('添加失败', Code::USER_ERROR);
}
return $data['hash'];
return $hash.$filename;
}
}
... ...
... ... @@ -87,12 +87,13 @@ class BlogLogic extends BaseLogic
try {
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
if($info !== false && !empty($info['hash'])){
$info = $this->model->read(['id'=>$this->param['id']],['id','image']);
if($info !== false && !empty($info['image'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
$image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
shell_exec('sudo rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['image'] . '*');
$imageModel->del(['hash'=>$info['image']]);
}
$this->param['image'] = $this->upload();
}
... ...
... ... @@ -72,7 +72,6 @@ class NewsLogic extends BaseLogic
DB::rollBack();
$this->fail('添加失败',Code::USER_ERROR);
}
//TODO::写入日志
$this->success();
}
... ... @@ -93,12 +92,14 @@ class NewsLogic extends BaseLogic
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','hash']);
if($info !== false && !empty($info['hash'])){
$info = $this->model->read(['id'=>$this->param['id']],['id','image']);
if($info !== false && !empty($info['image'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*');
$image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] . ' ./../uploads/images/cache_'. $info['image'] . '*');
//清空数据库
$imageModel->del(['hash'=>$info['image']]);
}
$this->param['image'] = $this->upload();
}
... ...