1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fabrica\Http\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use Fabrica\Http\Requests; |
8
|
|
|
use Fabrica\Http\Api\Controller; |
9
|
|
|
use Fabrica\Events\DocumentEvent; |
10
|
|
|
use Fabrica\Project\Eloquent\DocumentFavorites; |
11
|
|
|
use Fabrica\Acl\Acl; |
12
|
|
|
use DB; |
13
|
|
|
use Fabrica\Utils\File; |
14
|
|
|
|
15
|
|
|
use MongoDB\BSON\ObjectID; |
16
|
|
|
use Zipper; |
17
|
|
|
|
18
|
|
|
class DocumentController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* search path. |
22
|
|
|
* @param \Illuminate\Http\Request $request |
23
|
|
|
* @param string $project_key |
24
|
|
|
* @return \Illuminate\Http\Response |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function searchPath(Request $request, $project_key) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$s = $request->input('s'); |
29
|
|
|
if (!$s) |
30
|
|
|
{ |
31
|
|
|
return Response()->json(['ecode' => 0, 'data' => []]); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($s === '/') |
35
|
|
|
{ |
36
|
|
|
return Response()->json(['ecode' => 0, 'data' => [ [ 'id' => '0', 'name' => '/' ] ] ]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$query = DB::collection('document_' . $project_key) |
40
|
|
|
->where('d', 1) |
41
|
|
|
->where('del_flag', '<>', 1) |
42
|
|
|
->where('name', 'like', '%' . $s . '%'); |
43
|
|
|
|
44
|
|
|
$moved_path = $request->input('moved_path'); |
45
|
|
|
if (isset($moved_path) && $moved_path) |
46
|
|
|
{ |
47
|
|
|
$query->where('pt', '<>', $moved_path); |
48
|
|
|
$query->where('_id', '<>', $moved_path); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$directories = $query->take(20)->get(['name', 'pt']); |
52
|
|
|
|
53
|
|
|
$ret = []; |
54
|
|
|
foreach ($directories as $d) |
55
|
|
|
{ |
56
|
|
|
$parents = []; |
57
|
|
|
$path = ''; |
58
|
|
|
$ps = DB::collection('document_' . $project_key) |
59
|
|
|
->whereIn('_id', $d['pt']) |
60
|
|
|
->get([ 'name' ]); |
61
|
|
|
foreach ($ps as $val) |
62
|
|
|
{ |
63
|
|
|
$parents[$val['_id']->__toString()] = $val['name']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($d['pt'] as $pid) |
67
|
|
|
{ |
68
|
|
|
if (isset($parents[$pid])) |
69
|
|
|
{ |
70
|
|
|
$path .= '/' . $parents[$pid]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
$path .= '/' . $d['name']; |
74
|
|
|
$ret[] = [ 'id' => $d['_id']->__toString(), 'name' => $path ]; |
75
|
|
|
} |
76
|
|
|
return Response()->json(['ecode' => 0, 'data' => parent::arrange($ret)]); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* get the directory children. |
81
|
|
|
* @param string $project_key |
82
|
|
|
* @param string $directory |
83
|
|
|
* @return \Illuminate\Http\Response |
84
|
|
|
*/ |
85
|
|
|
public function getDirChildren(Request $request, $project_key, $directory) |
86
|
|
|
{ |
87
|
|
|
$sub_dirs = DB::collection('document_' . $project_key) |
88
|
|
|
->where('parent', $directory) |
89
|
|
|
->where('d', 1) |
90
|
|
|
->where('del_flag', '<>', 1) |
91
|
|
|
->get(); |
92
|
|
|
|
93
|
|
|
$res = []; |
94
|
|
|
foreach ($sub_dirs as $val) |
95
|
|
|
{ |
96
|
|
|
$res[] = [ 'id' => $val['_id']->__toString(), 'name' => $val['name'] ]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => $res ]); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* get the directory tree. |
104
|
|
|
* @param string $project_key |
105
|
|
|
* @return \Illuminate\Http\Response |
106
|
|
|
*/ |
107
|
|
|
public function getDirTree(Request $request, $project_key) |
108
|
|
|
{ |
109
|
|
|
$dt = [ 'id' => '0', 'name' => '根目录' ]; |
110
|
|
|
|
111
|
|
|
$curdir = $request->input('currentdir'); |
112
|
|
|
if (!$curdir) |
113
|
|
|
{ |
114
|
|
|
$curdir = '0'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$pt = [ '0' ]; |
118
|
|
|
if ($curdir !== '0') |
119
|
|
|
{ |
120
|
|
|
$node = DB::collection('document_' . $project_key) |
121
|
|
|
->where('_id', $curdir) |
122
|
|
|
->first(); |
123
|
|
|
|
124
|
|
|
if ($node) |
125
|
|
|
{ |
126
|
|
|
$pt = $node['pt']; |
127
|
|
|
array_push($pt, $curdir); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
foreach($pt as $val) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$sub_dirs = DB::collection('document_' . $project_key) |
134
|
|
|
->where('parent', $val) |
135
|
|
|
->where('d', 1) |
136
|
|
|
->where('del_flag', '<>', 1) |
137
|
|
|
->get(); |
138
|
|
|
|
139
|
|
|
$this->addChildren2Tree($dt, $val, $sub_dirs); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => $dt ]); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* add children to tree. |
147
|
|
|
* |
148
|
|
|
* @param array $dt |
149
|
|
|
* @param string $parent_id |
150
|
|
|
* @param array $sub_dirs |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function addChildren2Tree(&$dt, $parent_id, $sub_dirs) |
154
|
|
|
{ |
155
|
|
|
$new_dirs = []; |
156
|
|
|
foreach($sub_dirs as $val) |
157
|
|
|
{ |
158
|
|
|
$new_dirs[] = [ 'id' => $val['_id']->__toString(), 'name' => $val['name'] ]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
if ($dt['id'] == $parent_id) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$dt['children'] = $new_dirs; |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
else |
167
|
|
|
{ |
168
|
|
|
if (isset($dt['children']) && $dt['children']) |
169
|
|
|
{ |
170
|
|
|
$children_num = count($dt['children']); |
171
|
|
|
for ($i = 0; $i < $children_num; $i++) |
172
|
|
|
{ |
173
|
|
|
$res = $this->addChildren2Tree($dt['children'][$i], $parent_id, $sub_dirs); |
174
|
|
|
if ($res === true) |
175
|
|
|
{ |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Display a listing of the resource. |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Http\Response |
188
|
|
|
*/ |
189
|
|
|
public function getOptions(Request $request, $project_key) |
190
|
|
|
{ |
191
|
|
|
$uploaders = DB::collection('document_' . $project_key) |
192
|
|
|
->where('del_flag', '<>' , 1) |
193
|
|
|
->distinct('uploader') |
194
|
|
|
->get([ 'uploader' ]); |
195
|
|
|
|
196
|
|
|
return Response()->json(['ecode' => 0, 'data' => [ 'uploader' => $uploaders ]]); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Display a listing of the resource. |
201
|
|
|
* |
202
|
|
|
* @return \Illuminate\Http\Response |
203
|
|
|
*/ |
204
|
|
|
public function index(Request $request, $project_key, $directory) |
205
|
|
|
{ |
206
|
|
|
$documents = []; |
|
|
|
|
207
|
|
|
$mode = 'list'; |
208
|
|
|
$query = DB::collection('document_' . $project_key); |
209
|
|
|
|
210
|
|
|
$uploader_id = $request->input('uploader_id'); |
211
|
|
|
if (isset($uploader_id) && $uploader_id) |
212
|
|
|
{ |
213
|
|
|
$mode = 'search'; |
214
|
|
|
$query->where('uploader.id', $uploader_id == 'me' ? $this->user->id : $uploader_id); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$name = $request->input('name'); |
218
|
|
View Code Duplication |
if (isset($name) && $name) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$mode = 'search'; |
221
|
|
|
$query = $query->where('name', 'like', '%' . $name . '%'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$uploaded_at = $request->input('uploaded_at'); |
225
|
|
|
if (isset($uploaded_at) && $uploaded_at) |
226
|
|
|
{ |
227
|
|
|
$mode = 'search'; |
228
|
|
|
$unitMap = [ 'w' => 'week', 'm' => 'month', 'y' => 'year' ]; |
229
|
|
|
$unit = substr($uploaded_at, -1); |
230
|
|
|
$val = abs(substr($uploaded_at, 0, -1)); |
231
|
|
|
$query->where('uploaded_at', '>=', strtotime(date('Ymd', strtotime('-' . $val . ' ' . $unitMap[$unit])))); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$favorite_documents = DocumentFavorites::where('project_key', $project_key) |
235
|
|
|
->where('user.id', $this->user->id) |
236
|
|
|
->get() |
237
|
|
|
->toArray(); |
238
|
|
|
$favorite_dids = array_column($favorite_documents, 'did'); |
239
|
|
|
|
240
|
|
|
$myfavorite = $request->input('myfavorite'); |
241
|
|
View Code Duplication |
if (isset($myfavorite) && $myfavorite == '1') |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$mode = 'search'; |
244
|
|
|
$favoritedIds = []; |
245
|
|
|
foreach ($favorite_dids as $did) |
246
|
|
|
{ |
247
|
|
|
$favoritedIds[] = new ObjectID($did); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$query->whereIn('_id', $favoritedIds); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
if ($directory !== '0') |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
$query = $query->where($mode === 'search' ? 'pt' : 'parent', $directory); |
256
|
|
|
} |
257
|
|
|
else |
258
|
|
|
{ |
259
|
|
|
if ($mode === 'list') |
260
|
|
|
{ |
261
|
|
|
$query = $query->where('parent', $directory); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$query->where('del_flag', '<>', 1); |
266
|
|
|
$query->orderBy('d', 'desc')->orderBy('_id', 'desc'); |
267
|
|
|
|
268
|
|
|
$limit = 1000; // fix me |
269
|
|
|
$query->take($limit); |
270
|
|
|
$documents = $query->get(); |
271
|
|
|
|
272
|
|
View Code Duplication |
foreach ($documents as $k => $d) |
|
|
|
|
273
|
|
|
{ |
274
|
|
|
if (in_array($d['_id']->__toString(), $favorite_dids)) |
275
|
|
|
{ |
276
|
|
|
$documents[$k]['favorited'] = true; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$path = []; |
281
|
|
|
if ($directory === '0') |
282
|
|
|
{ |
283
|
|
|
$path[] = [ 'id' => '0', 'name' => 'root' ]; |
284
|
|
|
} |
285
|
|
|
else |
286
|
|
|
{ |
287
|
|
|
$path[] = [ 'id' => '0', 'name' => 'root' ]; |
288
|
|
|
$d = DB::collection('document_' . $project_key) |
289
|
|
|
->where('_id', $directory) |
290
|
|
|
->first(); |
291
|
|
|
if ($d && isset($d['pt'])) |
292
|
|
|
{ |
293
|
|
|
$parents = []; |
294
|
|
|
$ps = DB::collection('document_' . $project_key) |
295
|
|
|
->whereIn('_id', $d['pt']) |
296
|
|
|
->get([ 'name' ]); |
297
|
|
|
foreach ($ps as $val) |
298
|
|
|
{ |
299
|
|
|
$parents[$val['_id']->__toString()] = $val['name']; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
foreach ($d['pt'] as $pid) |
303
|
|
|
{ |
304
|
|
View Code Duplication |
if (isset($parents[$pid])) |
|
|
|
|
305
|
|
|
{ |
306
|
|
|
$path[] = [ 'id' => $pid, 'name' => $parents[$pid] ]; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
$path[] = [ 'id' => $directory, 'name' => $d['name'] ]; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => parent::arrange($documents), 'options' => [ 'path' => $path ] ]); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Store a newly created resource in storage. |
318
|
|
|
* |
319
|
|
|
* @param \Illuminate\Http\Request $request |
320
|
|
|
* @param string $project_key |
321
|
|
|
* @return \Illuminate\Http\Response |
322
|
|
|
*/ |
323
|
|
View Code Duplication |
public function createFolder(Request $request, $project_key) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
$insValues = []; |
326
|
|
|
|
327
|
|
|
$parent = $request->input('parent'); |
328
|
|
|
if (!isset($parent)) |
329
|
|
|
{ |
330
|
|
|
throw new \UnexpectedValueException('the parent directory can not be empty.', -11905); |
331
|
|
|
} |
332
|
|
|
$insValues['parent'] = $parent; |
333
|
|
|
|
334
|
|
|
if ($parent !== '0') |
335
|
|
|
{ |
336
|
|
|
$isExists = DB::collection('document_' . $project_key) |
337
|
|
|
->where('_id', $parent) |
338
|
|
|
->where('d', 1) |
339
|
|
|
->where('del_flag', '<>', 1) |
340
|
|
|
->exists(); |
341
|
|
|
if (!$isExists) |
342
|
|
|
{ |
343
|
|
|
throw new \UnexpectedValueException('the parent directory does not exist.', -11906); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$name = $request->input('name'); |
348
|
|
|
if (!isset($name) || !$name) |
349
|
|
|
{ |
350
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -11900); |
351
|
|
|
} |
352
|
|
|
$insValues['name'] = $name; |
353
|
|
|
|
354
|
|
|
$isExists = DB::collection('document_' . $project_key) |
355
|
|
|
->where('parent', $parent) |
356
|
|
|
->where('name', $name) |
357
|
|
|
->where('d', 1) |
358
|
|
|
->where('del_flag', '<>', 1) |
359
|
|
|
->exists(); |
360
|
|
|
if ($isExists) |
361
|
|
|
{ |
362
|
|
|
throw new \UnexpectedValueException('the name cannot be repeated.', -11901); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
$insValues['pt'] = $this->getParentTree($project_key, $parent); |
366
|
|
|
$insValues['d'] = 1; |
367
|
|
|
$insValues['creator'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
368
|
|
|
$insValues['created_at'] = time(); |
369
|
|
|
|
370
|
|
|
$id = DB::collection('document_' . $project_key)->insertGetId($insValues); |
371
|
|
|
|
372
|
|
|
$document = DB::collection('document_' . $project_key)->where('_id', $id)->first(); |
373
|
|
|
return Response()->json(['ecode' => 0, 'data' => parent::arrange($document)]); |
|
|
|
|
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* get parent treee. |
378
|
|
|
* @param string $project_key |
379
|
|
|
* @param string $directory |
380
|
|
|
* @return array |
381
|
|
|
*/ |
382
|
|
View Code Duplication |
public function getParentTree($project_key, $directory) |
|
|
|
|
383
|
|
|
{ |
384
|
|
|
$pt = []; |
|
|
|
|
385
|
|
|
if ($directory === '0') |
386
|
|
|
{ |
387
|
|
|
$pt = [ '0' ]; |
388
|
|
|
} |
389
|
|
|
else |
390
|
|
|
{ |
391
|
|
|
$d = DB::collection('document_' . $project_key) |
392
|
|
|
->where('_id', $directory) |
393
|
|
|
->first(); |
394
|
|
|
$pt = array_merge($d['pt'], [ $directory ]); |
395
|
|
|
} |
396
|
|
|
return $pt; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Update the specified resource in storage. |
401
|
|
|
* |
402
|
|
|
* @param \Illuminate\Http\Request $request |
403
|
|
|
* @param string $project_key |
404
|
|
|
* @param string $id |
405
|
|
|
* @return \Illuminate\Http\Response |
406
|
|
|
*/ |
407
|
|
|
public function update(Request $request, $project_key, $id) |
408
|
|
|
{ |
409
|
|
|
$name = $request->input('name'); |
410
|
|
|
if (!isset($name) || !$name) |
411
|
|
|
{ |
412
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -11900); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
$old_document = DB::collection('document_' . $project_key) |
416
|
|
|
->where('_id', $id) |
417
|
|
|
->where('del_flag', '<>', 1) |
418
|
|
|
->first(); |
419
|
|
|
if (!$old_document) |
420
|
|
|
{ |
421
|
|
|
throw new \UnexpectedValueException('the object does not exist.', -11902); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
View Code Duplication |
if (isset($old_document['d']) && $old_document['d'] === 1) |
|
|
|
|
425
|
|
|
{ |
426
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project')) |
427
|
|
|
{ |
428
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
|
|
|
|
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
else |
432
|
|
|
{ |
433
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project') && $old_document['uploader']['id'] !== $this->user->id) |
434
|
|
|
{ |
435
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
View Code Duplication |
if ($old_document['name'] !== $name) |
|
|
|
|
440
|
|
|
{ |
441
|
|
|
$query = DB::collection('document_' . $project_key) |
442
|
|
|
->where('parent', $old_document['parent']) |
443
|
|
|
->where('name', $name) |
444
|
|
|
->where('del_flag', '<>', 1); |
445
|
|
|
|
446
|
|
|
if (isset($old_document['d']) && $old_document['d'] === 1) |
447
|
|
|
{ |
448
|
|
|
$query->where('d', 1); |
449
|
|
|
} |
450
|
|
|
else |
451
|
|
|
{ |
452
|
|
|
$query->where('d', '<>', 1); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$isExists = $query->exists(); |
456
|
|
|
|
457
|
|
|
if ($isExists) |
458
|
|
|
{ |
459
|
|
|
throw new \UnexpectedValueException('the name cannot be repeated.', -11901); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
DB::collection('document_' . $project_key)->where('_id', $id)->update([ 'name' => $name ]); |
464
|
|
|
$new_document = DB::collection('document_' . $project_key)->where('_id', $id)->first(); |
465
|
|
|
|
466
|
|
|
return Response()->json(['ecode' => 0, 'data' => parent::arrange($new_document)]); |
|
|
|
|
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* move the document. |
471
|
|
|
* |
472
|
|
|
* @param \Illuminate\Http\Request $request |
473
|
|
|
* @param string $project_key |
474
|
|
|
* @return \Illuminate\Http\Response |
475
|
|
|
*/ |
476
|
|
|
public function move(Request $request, $project_key) |
477
|
|
|
{ |
478
|
|
|
$id = $request->input('id'); |
479
|
|
|
if (!isset($id) || !$id) |
480
|
|
|
{ |
481
|
|
|
throw new \UnexpectedValueException('the move object can not be empty.', -11911); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
$dest_path = $request->input('dest_path'); |
485
|
|
|
if (!isset($dest_path)) |
486
|
|
|
{ |
487
|
|
|
throw new \UnexpectedValueException('the dest directory can not be empty.', -11912); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$document = DB::collection('document_' . $project_key) |
491
|
|
|
->where('_id', $id) |
492
|
|
|
->where('del_flag', '<>', 1) |
493
|
|
|
->first(); |
494
|
|
|
if (!$document) |
495
|
|
|
{ |
496
|
|
|
throw new \UnexpectedValueException('the move object does not exist.', -11913); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
View Code Duplication |
if (isset($document['d']) && $document['d'] === 1) |
|
|
|
|
500
|
|
|
{ |
501
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project')) |
502
|
|
|
{ |
503
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
|
|
|
|
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
else |
507
|
|
|
{ |
508
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project') && $document['uploader']['id'] !== $this->user->id) |
509
|
|
|
{ |
510
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
$dest_directory = []; |
515
|
|
|
if ($dest_path !== '0') |
516
|
|
|
{ |
517
|
|
|
$dest_directory = DB::collection('document_' . $project_key) |
518
|
|
|
->where('_id', $dest_path) |
519
|
|
|
->where('d', 1) |
520
|
|
|
->where('del_flag', '<>', 1) |
521
|
|
|
->first(); |
522
|
|
|
if (!$dest_directory) |
523
|
|
|
{ |
524
|
|
|
throw new \UnexpectedValueException('the dest directory does not exist.', -11914); |
525
|
|
|
} |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
$isExists = DB::collection('document_' . $project_key) |
529
|
|
|
->where('parent', $dest_path) |
530
|
|
|
->where('name', $document['name']) |
531
|
|
|
->where('d', isset($document['d']) && $document['d'] === 1 ? '=' : '<>', 1) |
532
|
|
|
->where('del_flag', '<>', 1) |
533
|
|
|
->exists(); |
534
|
|
|
if ($isExists) |
535
|
|
|
{ |
536
|
|
|
throw new \UnexpectedValueException('the name cannot be repeated.', -11901); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
$updValues = []; |
540
|
|
|
$updValues['parent'] = $dest_path; |
541
|
|
|
$updValues['pt'] = array_merge(isset($dest_directory['pt']) ? $dest_directory['pt'] : [], [$dest_path]); |
542
|
|
|
DB::collection('document_' . $project_key)->where('_id', $id)->update($updValues); |
543
|
|
|
|
544
|
|
View Code Duplication |
if (isset($document['d']) && $document['d'] === 1) |
|
|
|
|
545
|
|
|
{ |
546
|
|
|
$subs = DB::collection('document_' . $project_key) |
547
|
|
|
->where('pt', $id) |
548
|
|
|
->where('del_flag', '<>', 1) |
549
|
|
|
->get(); |
550
|
|
|
foreach ($subs as $sub) |
551
|
|
|
{ |
552
|
|
|
$pt = isset($sub['pt']) ? $sub['pt'] : []; |
553
|
|
|
$pind = array_search($id, $pt); |
554
|
|
|
if ($pind !== false) |
555
|
|
|
{ |
556
|
|
|
$tail = array_slice($pt, $pind); |
557
|
|
|
$pt = array_merge($updValues['pt'], $tail); |
558
|
|
|
DB::collection('document_' . $project_key)->where('_id', $sub['_id']->__toString())->update(['pt' => $pt]); |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
$document = DB::collection('document_' . $project_key)->where('_id', $id)->first(); |
564
|
|
|
return Response()->json(['ecode' => 0, 'data' => parent::arrange($document)]); |
|
|
|
|
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Remove the specified resource from storage. |
569
|
|
|
* |
570
|
|
|
* @param string $project_key |
571
|
|
|
* @param string $id |
572
|
|
|
* @return \Illuminate\Http\Response |
573
|
|
|
*/ |
574
|
|
|
public function destroy($project_key, $id) |
575
|
|
|
{ |
576
|
|
|
$document = DB::collection('document_' . $project_key) |
577
|
|
|
->where('_id', $id) |
578
|
|
|
->where('del_flag', '<>', 1) |
579
|
|
|
->first(); |
580
|
|
|
if (!$document) |
581
|
|
|
{ |
582
|
|
|
throw new \UnexpectedValueException('the object does not exist.', -11902); |
583
|
|
|
} |
584
|
|
|
|
585
|
|
View Code Duplication |
if (isset($document['d']) && $document['d'] === 1) |
|
|
|
|
586
|
|
|
{ |
587
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project')) |
588
|
|
|
{ |
589
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
|
|
|
|
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
else |
593
|
|
|
{ |
594
|
|
|
if (!$this->isPermissionAllowed($project_key, 'manage_project') && $document['uploader']['id'] !== $this->user->id) |
595
|
|
|
{ |
596
|
|
|
return Response()->json(['ecode' => -10002, 'emsg' => 'permission denied.']); |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
DB::collection('document_' . $project_key)->where('_id', $id)->update([ 'del_flag' => 1 ]); |
601
|
|
|
|
602
|
|
View Code Duplication |
if (isset($document['d']) && $document['d'] === 1) |
|
|
|
|
603
|
|
|
{ |
604
|
|
|
DB::collection('document_' . $project_key)->whereRaw([ 'pt' => $id ])->update([ 'del_flag' => 1 ]); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
return Response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Upload file. |
612
|
|
|
* |
613
|
|
|
* @param \Illuminate\Http\Request $request |
614
|
|
|
* @param String $project_key |
615
|
|
|
* @param String $directory |
616
|
|
|
* @return \Illuminate\Http\Response |
617
|
|
|
*/ |
618
|
|
|
public function upload(Request $request, $project_key, $directory) |
619
|
|
|
{ |
620
|
|
|
set_time_limit(0); |
621
|
|
|
|
622
|
|
|
if (!is_writable(config('filesystems.disks.local.root', '/tmp'))) |
623
|
|
|
{ |
624
|
|
|
throw new \UnexpectedValueException('the user has not the writable permission to the directory.', -15103); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
if ($directory !== '0') |
628
|
|
|
{ |
629
|
|
|
$isExists = DB::collection('document_' . $project_key) |
630
|
|
|
->where('_id', $directory) |
631
|
|
|
->where('d', 1) |
632
|
|
|
->where('del_flag', '<>', 1) |
633
|
|
|
->exists(); |
634
|
|
|
if (!$isExists) |
635
|
|
|
{ |
636
|
|
|
throw new \UnexpectedValueException('the parent directory does not exist.', -11905); |
637
|
|
|
} |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
$fields = array_keys($_FILES); |
641
|
|
|
$field = array_pop($fields); |
642
|
|
View Code Duplication |
if (empty($_FILES) || $_FILES[$field]['error'] > 0) |
|
|
|
|
643
|
|
|
{ |
644
|
|
|
throw new \UnexpectedValueException('upload file errors.', -11903); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
$basename = md5(microtime() . $_FILES[$field]['name']); |
648
|
|
|
$sub_save_path = config('filesystems.disks.local.root', '/tmp') . '/' . substr($basename, 0, 2) . '/'; |
649
|
|
|
if (!is_dir($sub_save_path)) |
650
|
|
|
{ |
651
|
|
|
@mkdir($sub_save_path); |
|
|
|
|
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
$filename = $sub_save_path . $basename; |
655
|
|
|
move_uploaded_file($_FILES[$field]['tmp_name'], $filename); |
656
|
|
|
|
657
|
|
|
$data = []; |
658
|
|
|
|
659
|
|
|
$thumbnail_size = 190; |
660
|
|
View Code Duplication |
if (in_array($_FILES[$field]['type'], [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif' ])) |
|
|
|
|
661
|
|
|
{ |
662
|
|
|
$size = getimagesize($filename); |
663
|
|
|
$width = $size[0]; $height = $size[1]; |
664
|
|
|
$scale = $width < $height ? $height : $width; |
665
|
|
|
$thumbnails_width = floor($thumbnail_size * $width / $scale); |
666
|
|
|
$thumbnails_height = floor($thumbnail_size * $height / $scale); |
667
|
|
|
$thumbnails_filename = $filename . '_thumbnails'; |
668
|
|
|
if ($scale <= $thumbnail_size) |
669
|
|
|
{ |
670
|
|
|
@copy($filename, $thumbnails_filename); |
|
|
|
|
671
|
|
|
} |
672
|
|
|
else if ($_FILES[$field]['type'] == 'image/jpeg' || $_FILES[$field]['type'] == 'image/jpg') |
673
|
|
|
{ |
674
|
|
|
$src_image = imagecreatefromjpeg($filename); |
675
|
|
|
$dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
676
|
|
|
imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
677
|
|
|
imagejpeg($dst_image, $thumbnails_filename); |
678
|
|
|
} |
679
|
|
|
else if ($_FILES[$field]['type'] == 'image/png') |
680
|
|
|
{ |
681
|
|
|
$src_image = imagecreatefrompng($filename); |
682
|
|
|
$dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
683
|
|
|
imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
684
|
|
|
imagepng($dst_image, $thumbnails_filename); |
685
|
|
|
} |
686
|
|
|
else if ($_FILES[$field]['type'] == 'image/gif') |
687
|
|
|
{ |
688
|
|
|
$src_image = imagecreatefromgif($filename); |
689
|
|
|
$dst_image = imagecreatetruecolor($thumbnails_width, $thumbnails_height); |
690
|
|
|
imagecopyresized($dst_image, $src_image, 0, 0, 0, 0, $thumbnails_width, $thumbnails_height, $width, $height); |
691
|
|
|
imagegif($dst_image, $thumbnails_filename); |
692
|
|
|
} |
693
|
|
|
else |
694
|
|
|
{ |
695
|
|
|
@copy($filename, $thumbnails_filename); |
|
|
|
|
696
|
|
|
} |
697
|
|
|
$data['thumbnails_index'] = $basename . '_thumbnails'; |
698
|
|
|
// move the thumbnails |
699
|
|
|
@rename($thumbnails_filename, $sub_save_path . $data['thumbnails_index']); |
|
|
|
|
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
$fname = $_FILES[$field]['name']; |
703
|
|
|
$extname = ''; |
704
|
|
|
$segments = explode('.', $fname); |
705
|
|
|
if (count($segments) > 1) |
706
|
|
|
{ |
707
|
|
|
$extname = '.' . array_pop($segments); |
708
|
|
|
$fname = implode('.', $segments); |
709
|
|
|
} |
710
|
|
|
$i = 1; |
711
|
|
|
while(true) |
712
|
|
|
{ |
713
|
|
|
$isExists = DB::collection('document_' . $project_key) |
714
|
|
|
->where('parent', $directory) |
715
|
|
|
->where('name', $fname . ($i < 2 ? '' : ('(' . $i . ')')) . $extname) |
716
|
|
|
->where('d', '<>', 1) |
717
|
|
|
->where('del_flag', '<>', 1) |
718
|
|
|
->exists(); |
719
|
|
|
if (!$isExists) |
720
|
|
|
{ |
721
|
|
|
break; |
722
|
|
|
} |
723
|
|
|
$i++; |
724
|
|
|
} |
725
|
|
|
$data['name'] = $fname . ($i < 2 ? '' : ('(' . $i . ')')) . $extname; |
726
|
|
|
|
727
|
|
|
$data['pt'] = $this->getParentTree($project_key, $directory); |
728
|
|
|
$data['parent'] = $directory; |
729
|
|
|
$data['size'] = $_FILES[$field]['size']; |
730
|
|
|
$data['type'] = $_FILES[$field]['type']; |
731
|
|
|
$data['index'] = $basename; |
732
|
|
|
|
733
|
|
|
$data['uploader'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
734
|
|
|
$data['uploaded_at'] = time(); |
735
|
|
|
|
736
|
|
|
$id = DB::collection('document_' . $project_key)->insertGetId($data); |
737
|
|
|
$document = DB::collection('document_' . $project_key)->where('_id', $id)->first(); |
738
|
|
|
|
739
|
|
|
return Response()->json(['ecode' => 0, 'data' => parent::arrange($document)]); |
|
|
|
|
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* Download Thumbnails file. |
744
|
|
|
* |
745
|
|
|
* @param \Illuminate\Http\Request $request |
746
|
|
|
* @param String $project_key |
747
|
|
|
* @param String $id |
748
|
|
|
* @return \Illuminate\Http\Response |
749
|
|
|
*/ |
750
|
|
|
public function downloadThumbnails(Request $request, $project_key, $id) |
751
|
|
|
{ |
752
|
|
|
set_time_limit(0); |
753
|
|
|
|
754
|
|
|
$document = DB::collection('document_' . $project_key) |
755
|
|
|
->where('_id', $id) |
756
|
|
|
->first(); |
757
|
|
|
if (!$document) |
758
|
|
|
{ |
759
|
|
|
throw new \UnexpectedValueException('the object does not exist.', -11902); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
$filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($document['index'], 0, 2); |
763
|
|
|
$filename = $filepath . '/' . $document['thumbnails_index']; |
764
|
|
|
if (!file_exists($filename)) |
765
|
|
|
{ |
766
|
|
|
throw new \UnexpectedValueException('file does not exist.', -11904); |
767
|
|
|
} |
768
|
|
|
|
769
|
|
|
File::download($filename, $document['name']); |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Download file or directory. |
774
|
|
|
* |
775
|
|
|
* @param \Illuminate\Http\Request $request |
776
|
|
|
* @param String $project_key |
777
|
|
|
* @param String $id |
778
|
|
|
* @return \Illuminate\Http\Response |
779
|
|
|
*/ |
780
|
|
|
public function download(Request $request, $project_key, $id) |
781
|
|
|
{ |
782
|
|
|
set_time_limit(0); |
783
|
|
|
|
784
|
|
|
$document = DB::collection('document_' . $project_key) |
785
|
|
|
->where('_id', $id) |
786
|
|
|
->first(); |
787
|
|
|
if (!$document) |
788
|
|
|
{ |
789
|
|
|
throw new \UnexpectedValueException('the object does not exist.', -11902); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
if (isset($document['d']) && $document['d'] === 1) |
793
|
|
|
{ |
794
|
|
|
$this->downloadFolder($project_key, $document['name'], $id); |
795
|
|
|
} |
796
|
|
|
else |
797
|
|
|
{ |
798
|
|
|
$this->downloadFile($document['name'], $document['index']); |
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
/** |
803
|
|
|
* Download file. |
804
|
|
|
* |
805
|
|
|
* @param String $name |
806
|
|
|
* @param String $directory |
807
|
|
|
* @return \Illuminate\Http\Response |
808
|
|
|
*/ |
809
|
|
|
public function downloadFolder($project_key, $name, $directory) |
810
|
|
|
{ |
811
|
|
|
setlocale(LC_ALL, 'zh_CN.UTF-8'); |
812
|
|
|
|
813
|
|
|
$basepath = '/tmp/' . md5($this->user->id . microtime()); |
814
|
|
|
@mkdir($basepath); |
|
|
|
|
815
|
|
|
|
816
|
|
|
$this->contructFolder($project_key, $basepath . '/' . $name, $directory); |
817
|
|
|
|
818
|
|
|
$filename = $basepath . '/' . $name . '.zip'; |
819
|
|
|
|
820
|
|
|
Zipper::make($filename)->folder($name)->add($basepath . '/' . $name); |
821
|
|
|
Zipper::close(); |
822
|
|
|
|
823
|
|
|
File::download($filename, $name . '.zip'); |
824
|
|
|
|
825
|
|
|
exec('rm -rf ' . $basepath); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* contruct file folder. |
830
|
|
|
* |
831
|
|
|
* @param String $fullpath |
832
|
|
|
* @param String $id |
833
|
|
|
* @return void |
834
|
|
|
*/ |
835
|
|
|
public function contructFolder($project_key, $fullpath, $id) |
836
|
|
|
{ |
837
|
|
|
@mkdir($fullpath); |
|
|
|
|
838
|
|
|
|
839
|
|
|
$documents = DB::collection('document_' . $project_key) |
840
|
|
|
->where('parent', $id) |
841
|
|
|
->where('del_flag', '<>', 1) |
842
|
|
|
->get(); |
843
|
|
|
foreach ($documents as $doc) |
844
|
|
|
{ |
845
|
|
|
if (isset($doc['d']) && $doc['d'] === 1) |
846
|
|
|
{ |
847
|
|
|
$this->contructFolder($project_key, $fullpath . '/' . $doc['name'], $doc['_id']->__toString()); |
848
|
|
|
} |
849
|
|
|
else |
850
|
|
|
{ |
851
|
|
|
$filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($doc['index'], 0, 2); |
852
|
|
|
$filename = $filepath . '/' . $doc['index']; |
853
|
|
|
if (file_exists($filename)) |
854
|
|
|
{ |
855
|
|
|
@copy($filename, $fullpath . '/' . $doc['name']); |
|
|
|
|
856
|
|
|
} |
857
|
|
|
} |
858
|
|
|
} |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
/** |
862
|
|
|
* Download file. |
863
|
|
|
* |
864
|
|
|
* @param String $name |
865
|
|
|
* @param String $index |
866
|
|
|
* @return \Illuminate\Http\Response |
867
|
|
|
*/ |
868
|
|
View Code Duplication |
public function downloadFile($name, $index) |
|
|
|
|
869
|
|
|
{ |
870
|
|
|
$filepath = config('filesystems.disks.local.root', '/tmp') . '/' . substr($index, 0, 2); |
871
|
|
|
$filename = $filepath . '/' . $index; |
872
|
|
|
if (!file_exists($filename)) |
873
|
|
|
{ |
874
|
|
|
throw new \UnexpectedValueException('file does not exist.', -11904); |
875
|
|
|
} |
876
|
|
|
|
877
|
|
|
File::download($filename, $name); |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
/** |
881
|
|
|
* favorite action. |
882
|
|
|
* |
883
|
|
|
* @param string $project_key |
884
|
|
|
* @param string $id |
885
|
|
|
* @return \Illuminate\Http\Response |
886
|
|
|
*/ |
887
|
|
View Code Duplication |
public function favorite(Request $request, $project_key, $id) |
|
|
|
|
888
|
|
|
{ |
889
|
|
|
$document = DB::collection('document_' . $project_key) |
890
|
|
|
->where('_id', $id) |
891
|
|
|
->where('del_flag', '<>', 1) |
892
|
|
|
->first(); |
893
|
|
|
if (!$document) |
894
|
|
|
{ |
895
|
|
|
throw new \UnexpectedValueException('the object does not exist.', -11902); |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
DocumentFavorites::where('did', $id)->where('user.id', $this->user->id)->delete(); |
899
|
|
|
|
900
|
|
|
$cur_user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ]; |
901
|
|
|
|
902
|
|
|
$flag = $request->input('flag'); |
903
|
|
|
if (isset($flag) && $flag) |
904
|
|
|
{ |
905
|
|
|
DocumentFavorites::create([ 'project_key' => $project_key, 'did' => $id, 'user' => $cur_user ]); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
return Response()->json(['ecode' => 0, 'data' => ['id' => $id, 'user' => $cur_user, 'favorited' => $flag]]); |
|
|
|
|
909
|
|
|
} |
910
|
|
|
} |
911
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.