File.php
5.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace app\files\controller;
use think\Request;
use think\Response;
/**
* 通用文件处理
*/
class File
{
protected $request;
protected $config;
public function __construct(Request $request)
{
$this->request = $request;
$this->config = config('upload_file');
}
/**
* 输出返回数据
* @access protected
* @param mixed $data 要返回的数据
* @param String $type 返回类型 JSON XML
* @param integer $code HTTP状态码
* @return Response
*/
protected function response($msg, $code = 200, $data = '', $type = 'json')
{
$result = [
'code' => $code,
'msg' => $msg,
'data' => $data,
'_link' => '',
];
if ($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
$result['_link'][] = getFileUrl($value, 'file');
}
} else {
$result['_link'] = getFileUrl($data, 'file');
}
}
$header = ['x_end_time' => $this->request->time()];
return Response::create($result, $type)->code($code)->header($header);
}
/**
* 文件上传
*/
public function upload() {
$files = $this->request->file($this->config['name']);
if (empty($files)) {
return $this->response('没有上传的文件!', 400);
}
$type = $this->request->param('type', 'single');
if ($type == 'multi') {
return $this->multi($files);
} else {
return $this->single($files);
}
}
/**
* 文件下载
*
* @return \think\Response
*/
public function index($sha1, $type = 128) {
$info = db('file')->where('sha1', $sha1)->find();
if (empty($info)) {
return $this->response('指定文件不存在!', 404);
}
$path = '../../' . $info['path'];
if (!is_file($path)) {
return $this->response('指定文件已被系统删除!', 404);
}
$content = file_get_contents($path);
$header['Content-Length'] = $info['size'];
if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {
//for IE
$header['Content-Disposition'] = 'attachment; filename="' . rawurlencode(time() . '.' . $info['ext']) . '"';
} else {
$header['Content-Disposition'] = 'attachment; filename="' . time() . '.' . $info['ext'] . '"';
}
return response($content, 200, $header);
}
/**
* 单文件上传
* @param type $file file对象
* @return type
*/
private function single($file) {
if (is_array($file)) {
$file = current($file);
}
if ($file->check($this->config['rule']) === false) {
return $this->response($file->getMime() . $file->getError() . $this->config['rule']['type'], 400);
}
$info = db('file')->where('sha1', $file->hash())->find();
if (empty($info)) {
$res = $file->rule($this->config['savename'])->move(ROOT_PATH . $this->config['path']);
if ($res === false) {
return $this->response($file->getError(), 400);
}
$info = [
'path' => $this->config['path'] . $res->getSaveName(),
'create_time' => $this->request->time(),
'sha1' => $res->hash(),
'size' => $res->getSize(),
'ext' => $res->getExtension(),
];
db('file')->insert($info);
}
return $this->response('上传成功!', 200, $file->hash());
}
/**
* 多文件上传
* @param type $files file对象集合
* @return type
*/
private function multi($files) {
if (!is_array($files)) {
$files = [$files];
}
$savedata = [];
$data = [];
foreach ($files as $file) {
if ($file->check($this->config['rule']) === false) {
return $this->response($file->getError(), 400);
}
$info = db('picture')->where('sha1', $file->hash())->find();
if (empty($info)) {
$res = $file->rule($this->config['savename'])->move(ROOT_PATH . $this->config['path']);
if ($res === false) {
return $this->response($file->getError(), 400);
}
$savedata[] = [
'path' => $this->config['path'] . $res->getSaveName(),
'create_time' => $this->request->time(),
'sha1' => $res->hash(),
'size' => $res->getSize(),
'mime' => $res->getMime(),
'ext' => $res->getExtension(),
];
}
$data[] = $file->hash();
}
if (!empty($savedata)) {
db('file')->insertAll($savedata);
}
return $this->response('上传成功!', 200, $data);
}
}