作者 liyuhang

gx

@@ -100,25 +100,25 @@ class BaseLogic extends Logic @@ -100,25 +100,25 @@ class BaseLogic extends Logic
100 if(empty($image)){ 100 if(empty($image)){
101 return $this->fail('没有上传图片',Code::USER_ERROR); 101 return $this->fail('没有上传图片',Code::USER_ERROR);
102 } 102 }
  103 + $hash = hash_file('md5', $image->getPathname());
103 $url = './../uploads/images/'; 104 $url = './../uploads/images/';
104 $filename = date('ymdHis').rand(10000,99999); 105 $filename = date('ymdHis').rand(10000,99999);
105 $res = $this->request->file('image')->move($url,$filename); 106 $res = $this->request->file('image')->move($url,$filename);
106 if ($res === false) { 107 if ($res === false) {
107 return $this->fail($image->getError(), Code::USER_ERROR); 108 return $this->fail($image->getError(), Code::USER_ERROR);
108 } 109 }
  110 + $imageModel = new ImageModel();
109 $data = [ 111 $data = [
110 'path' => $url.$filename, 112 'path' => $url.$filename,
111 'created_at' => date('Y-m-d H:i:s',time()), 113 'created_at' => date('Y-m-d H:i:s',time()),
112 'size' => $res->getSize(), 114 'size' => $res->getSize(),
113 - 'hash' => hash_file('md5', $res->getPathname()), 115 + 'hash' => $hash.$filename,
114 'type'=>$image->getClientOriginalExtension(), 116 'type'=>$image->getClientOriginalExtension(),
115 -// 'mime'=>$image->getMimeType()  
116 ]; 117 ];
117 - $imageModel = new ImageModel();  
118 $rs = $imageModel->add($data); 118 $rs = $imageModel->add($data);
119 if ($rs === false) { 119 if ($rs === false) {
120 return $this->fail('添加失败', Code::USER_ERROR); 120 return $this->fail('添加失败', Code::USER_ERROR);
121 } 121 }
122 - return $data['hash']; 122 + return $hash.$filename;
123 } 123 }
124 } 124 }
@@ -87,12 +87,13 @@ class BlogLogic extends BaseLogic @@ -87,12 +87,13 @@ class BlogLogic extends BaseLogic
87 try { 87 try {
88 if(isset($this->param['image']) && is_file($this->param['image'])){ 88 if(isset($this->param['image']) && is_file($this->param['image'])){
89 //查看当前用户是否已有头像 89 //查看当前用户是否已有头像
90 - $info = $this->model->read(['id'=>$this->param['id']],['id','hash']);  
91 - if($info !== false && !empty($info['hash'])){ 90 + $info = $this->model->read(['id'=>$this->param['id']],['id','image']);
  91 + if($info !== false && !empty($info['image'])){
92 //TODO::删除资源 92 //TODO::删除资源
93 $imageModel = new Image(); 93 $imageModel = new Image();
94 - $image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);  
95 - shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*'); 94 + $image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
  95 + shell_exec('sudo rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['image'] . '*');
  96 + $imageModel->del(['hash'=>$info['image']]);
96 } 97 }
97 $this->param['image'] = $this->upload(); 98 $this->param['image'] = $this->upload();
98 } 99 }
@@ -72,7 +72,6 @@ class NewsLogic extends BaseLogic @@ -72,7 +72,6 @@ class NewsLogic extends BaseLogic
72 DB::rollBack(); 72 DB::rollBack();
73 $this->fail('添加失败',Code::USER_ERROR); 73 $this->fail('添加失败',Code::USER_ERROR);
74 } 74 }
75 -  
76 //TODO::写入日志 75 //TODO::写入日志
77 $this->success(); 76 $this->success();
78 } 77 }
@@ -93,12 +92,14 @@ class NewsLogic extends BaseLogic @@ -93,12 +92,14 @@ class NewsLogic extends BaseLogic
93 //上传图片 92 //上传图片
94 if(isset($this->param['image']) && is_file($this->param['image'])){ 93 if(isset($this->param['image']) && is_file($this->param['image'])){
95 //查看当前用户是否已有头像 94 //查看当前用户是否已有头像
96 - $info = $this->model->read(['id'=>$this->param['id']],['id','hash']);  
97 - if($info !== false && !empty($info['hash'])){ 95 + $info = $this->model->read(['id'=>$this->param['id']],['id','image']);
  96 + if($info !== false && !empty($info['image'])){
98 //TODO::删除资源 97 //TODO::删除资源
99 $imageModel = new Image(); 98 $imageModel = new Image();
100 - $image_info = $imageModel->read(['hash'=>$info['hash']],['id','path']);  
101 - shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['hash'] . '*'); 99 + $image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
  100 + shell_exec('rm -rf '.$image_info['path'] . ' ./../uploads/images/cache_'. $info['image'] . '*');
  101 + //清空数据库
  102 + $imageModel->del(['hash'=>$info['image']]);
102 } 103 }
103 $this->param['image'] = $this->upload(); 104 $this->param['image'] = $this->upload();
104 } 105 }