-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCabinetFilesController.php
More file actions
653 lines (595 loc) · 18.3 KB
/
CabinetFilesController.php
File metadata and controls
653 lines (595 loc) · 18.3 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<?php
/**
* CabinetEtnriesController
*/
App::uses('CabinetsAppController', 'Cabinets.Controller');
App::uses('ZipDownloader', 'Files.Utility');
App::uses('TemporaryFolder', 'Files.Utility');
/**
* CabinetFiles Controller
*
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @author Kazunori Sakamoto <exkazuu@willbooster.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @property NetCommonsWorkflow $NetCommonsWorkflow
* @property PaginatorComponent $Paginator
* @property CabinetFile $CabinetFile
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class CabinetFilesController extends CabinetsAppController {
/**
* @var array use models
*/
public $uses = array(
'Cabinets.CabinetFile',
'Cabinets.CabinetFileTree',
'Workflow.WorkflowComment',
);
/**
* @var array helpers
* @var array helpers
*/
public $helpers = array(
'NetCommons.BackTo',
'Workflow.Workflow',
'Users.DisplayUser',
'Paginator',
'NetCommons.Token',
'Cabinets.CabinetFile',
);
/**
* Components
*
* @var array
*/
public $components = array(
'Paginator',
//'NetCommons.NetCommonsWorkflow',
//'NetCommons.NetCommonsRoomRole' => array(
// //コンテンツの権限設定
// 'allowedActions' => array(
// 'contentEditable' => array('edit', 'add'),
// 'contentCreatable' => array('edit', 'add'),
// ),
//),
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
//'add,edit,delete' => 'content_creatable',
//'reply' => 'content_comment_creatable',
//'approve' => 'content_comment_publishable',
),
),
'Files.Download',
'AuthorizationKeys.AuthorizationKey' => [
'operationType' => 'popup',
//'operationType' => 'redirect',
//'operationType' => 'redirect',
'targetAction' => 'download',
'model' => 'CabinetFile',
],
);
/**
* @var array 絞り込みフィルタ保持値
*/
protected $_filter = array(
'categoryId' => 0,
'status' => 0,
'yearMonth' => 0,
);
/**
* @var array Cabinet
*/
protected $_cabinet;
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
// ゲストアクセスOKのアクションを設定
$this->Auth->allow(
'index',
'view',
'folder_detail',
'download',
'download_folder',
'get_download_counts',
'check_download_folder',
'load_download_folder'
);
parent::beforeFilter();
$this->_cabinet = $this->Cabinet->find('first', array(
'recursive' => 0,
'conditions' => $this->Cabinet->getBlockConditionById(),
));
$this->set('cabinet', $this->_cabinet);
}
/**
* index
*
* @return void
*/
public function index() {
if (!Current::read('Block.id')) {
$this->autoRender = false;
return;
}
//$this->CabinetFileTree->recover('parent');
// currentFolderを取得
//$folderKey = isset($this->request->params['pass'][1]) ? $this->request->params['pass'][1] : null;
$folderKey = Hash::get($this->request->params, 'key', null);
$currentFolder = $this->_getCurrentFolder($folderKey);
$currentTreeId = $currentFolder['CabinetFileTree']['id'];
$this->set('currentFolder', $currentFolder);
$this->set('currentTreeId', $currentTreeId);
// 全フォルダツリーを得る
$conditions = [
'CabinetFile.is_folder' => 1,
'CabinetFileTree.cabinet_key' => $this->viewVars['cabinet']['Cabinet']['key']
];
$folders = $this->CabinetFileTree->find(
'threaded',
['conditions' => $conditions, 'recursive' => 0, 'order' => 'CabinetFile.filename ASC']
);
$this->set('folders', $folders);
$files = $this->_getCurrentFolderFiles($currentTreeId);
$this->set('cabinetFiles', $files);
// カレントフォルダのツリーパスを得る
$folderPath = $this->CabinetFileTree->getPath($currentTreeId, null, 0);
$this->set('folderPath', $folderPath);
$url = $this->_getParentFolderUrl($currentFolder, $folderPath);
$this->set('parentUrl', $url);
$this->set('listTitle', $this->_cabinetTitle);
}
/**
* フォルダ詳細
*
* @return void
*/
public function folder_detail() {
$folderKey = Hash::get($this->request->params, 'key', null);
$conditions = [
'CabinetFile.key' => $folderKey,
'CabinetFile.cabinet_key' => $this->_cabinet['Cabinet']['key']
];
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
$cabinetFile = $this->CabinetFile->find('first', ['conditions' => $conditions]);
if ($cabinetFile) {
// folderじゃなかったらエラー
if (!$cabinetFile['CabinetFile']['is_folder']) {
return $this->throwBadRequest();
}
$cabinetFile['CabinetFile']['size'] = $this->CabinetFile->getTotalSizeByFolder(
$cabinetFile
);
$cabinetFile['CabinetFile']['has_children'] = $this->CabinetFile->hasChildren($cabinetFile);
//$cabinetFile['CabinetFile']['size'] =
$this->set('cabinetFile', $cabinetFile);
$this->_setFolderPath($cabinetFile);
} else {
return $this->throwBadRequest();
}
}
/**
* フォルダパスをViewにセット
*
* @param array $cabinetFile CabinetFileデータ
* @return void
*/
protected function _setFolderPath($cabinetFile) {
$treeId = $cabinetFile['CabinetFileTree']['id'];
$folderPath = $this->CabinetFileTree->getPath($treeId, null, 0);
$this->set('folderPath', $folderPath);
}
/**
* 権限の取得
*
* @return array
*/
protected function _getPermission() {
$permissionNames = array(
'content_readable',
'content_creatable',
'content_editable',
'content_publishable',
);
$permission = array();
foreach ($permissionNames as $key) {
$permission[$key] = Current::permission($key);
}
return $permission;
}
/**
* view method
*
* @throws NotFoundException
* @return void
*/
public function view() {
$folderKey = Hash::get($this->request->params, 'key', null);
$conditions = [
'CabinetFile.key' => $folderKey,
'CabinetFile.cabinet_key' => $this->_cabinet['Cabinet']['key']
];
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
$cabinetFile = $this->CabinetFile->find('first', ['conditions' => $conditions]);
if ($cabinetFile) {
// ファイルでなければエラー
if ($cabinetFile['CabinetFile']['is_folder']) {
return $this->throwBadRequest();
}
$this->set('cabinetFile', $cabinetFile);
$this->_setFolderPath($cabinetFile);
//新着データを既読にする
$this->CabinetFile->saveTopicUserStatus($cabinetFile);
} else {
return $this->throwBadRequest();
}
}
/**
* ファイルダウンロード
*
* @throws NotFoundException
* @return mixed
*/
public function download() {
// ここから元コンテンツを取得する処理
$folderKey = Hash::get($this->request->params, 'key', null);
$conditions = [
'CabinetFile.key' => $folderKey,
'CabinetFile.cabinet_key' => $this->_cabinet['Cabinet']['key']
];
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
$cabinetFile = $this->CabinetFile->find('first', ['conditions' => $conditions]);
//$this->set('cabinetFile', $cabinetFile);
// ここまで元コンテンツを取得する処理
$this->AuthorizationKey->guard('popup', 'CabinetFile', $cabinetFile);
// ダウンロード実行
if ($cabinetFile) {
return $this->Download->doDownload(
$cabinetFile['CabinetFile']['id'],
[
'field' => 'file',
'download' => true,
'name' => $cabinetFile['CabinetFile']['filename'],
]
);
} else {
// 表示できないファイルへのアクセスなら404
throw new NotFoundException(__d('cabinets', 'Invalid cabinet file'));
}
}
/**
* フォルダのZIPダウンロード
*
* @return CakeResponse|string|void
*/
public function download_folder() {
$this->response->header('Pragma', 'no-cache');
if (! $this->request->is('post')) {
return $this->throwBadRequest();
}
// フォルダを取得
$folderKey = Hash::get($this->request->data, 'CabinetFile.key', null);
$conditions = [
'CabinetFile.key' => $folderKey,
'CabinetFile.cabinet_key' => $this->_cabinet['Cabinet']['key']
];
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
$cabinetFolder = $this->CabinetFile->find('first', ['conditions' => $conditions]);
$tmpFolder = new TemporaryFolder();
try {
$this->_prepareDownload($tmpFolder->path, $cabinetFolder);
} catch (BadRequestException $e) {
//先にcheck_download_folderでチェックしているため、この処理は基本通らない
//もしタイムラグでダウンロードパスワードが設定されたら、フラッシュメッセージを表示して、
//ブラウザをリフレッシュする
$this->NetCommons->setFlashNotification($e->getMessage(), [], $e->getCode());
return $this->redirect($this->referer());
}
$zipDownloader = $this->_getZipDownloader();
list($folders, $files) = $tmpFolder->read(true, false, true);
foreach ($folders as $folder) {
$zipDownloader->addFolder($folder);
}
foreach ($files as $file) {
$zipDownloader->addFile($file);
}
return $zipDownloader->download($cabinetFolder['CabinetFile']['filename'] . '.zip');
}
/**
* ファイルのダウンロード数の取得
*
* @return void
*/
public function get_download_counts() {
$query = [
'fields' => [
'UploadFile.id',
'UploadFile.total_download_count',
],
'conditions' => [
'UploadFile.id' => explode(',', $this->request->query('upload_file_ids')),
],
'recursive' => -1
];
$UploadFile = ClassRegistry::init('Files.UploadFile');
$files = $UploadFile->find('all', $query);
$this->set('_serialize', ['counts']);
$this->set('counts', $files);
}
/**
* フォルダのZIPダウンロードができるか否かチェック
*
* @return void
*/
public function check_download_folder() {
$this->response->header('Pragma', 'no-cache');
if (! $this->request->is('post') || ! $this->request->is('ajax')) {
return $this->throwBadRequest();
}
// フォルダを取得
$folderKey = Hash::get($this->request->data, 'CabinetFile.key', null);
$conditions = [
'CabinetFile.key' => $folderKey,
'CabinetFile.cabinet_key' => $this->_cabinet['Cabinet']['key']
];
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
$cabinetFolder = $this->CabinetFile->find('first', ['conditions' => $conditions]);
$this->_checkDownloadFolder($cabinetFolder);
}
/**
* 圧縮ダウンロード用のCabinetFileデータを取得
*
* @param array $cabinetFolder CabinetFileデータ 処理するフォルダ
* @return array
*/
protected function _findCabinetFilesByFolderDownload($cabinetFolder) {
// フォルダのファイル取得
$files = $this->CabinetFile->find(
'all',
[
'conditions' => $this->CabinetFile->getWorkflowConditions(
[
'CabinetFileTree.parent_id' => $cabinetFolder['CabinetFileTree']['id'],
//'CabinetFile.is_folder' => false,
]
)
]
);
return $files;
}
/**
* ファイルの圧縮ダウンロードのロード処理
*
* @return void
*/
public function load_download_folder() {
$this->response->header('Pragma', 'no-cache');
$View = $this->_getViewObject();
$fileKeys = explode(',', $this->request->query('file_keys'));
foreach ($fileKeys as $fileKey) {
$View->CabinetFile->setZipDownloadToken($fileKey);
}
}
/**
* フォルダのZIPダウンロード前処理
*
* @param string $path ダウンロード処理用テンポラリフォルダのカレントパス
* @param array $cabinetFolder CabinetFileデータ 処理するフォルダ
* @throws BadRequestException
* @return void
*/
protected function _prepareDownload($path, $cabinetFolder) {
// フォルダのファイル取得
$files = $this->_findCabinetFilesByFolderDownload($cabinetFolder);
foreach ($files as $file) {
if ($file['CabinetFile']['is_folder']) {
mkdir($path . DS . $file['CabinetFile']['filename']);
$this->_prepareDownload($path . DS . $file['CabinetFile']['filename'], $file);
} else {
if (isset($file['AuthorizationKey'])) {
throw new BadRequestException(
__d(
'cabinets',
'Folder that contains the files that are password is set can not be downloaded ZIP.'
)
);
}
// ダウンロードカウントアップ
$this->CabinetFile->downloadCountUp($file, 'file');
$filePath = $this->CabinetFile->getRealFilePath($file, 'file');
//$filePath = WWW_ROOT .
// $file['UploadFile']['file']['path'] .
// $file['UploadFile']['file']['id'] .
// DS . $file['UploadFile']['file']['real_file_name'];
copy($filePath, $path . DS . $file['CabinetFile']['filename']);
}
}
}
/**
* フォルダのZIPダウンロードができるか否かチェックする
*
* @param array $cabinetFolder CabinetFileデータ 処理するフォルダ
* @throws BadRequestException
* @return void
*/
protected function _checkDownloadFolder($cabinetFolder) {
// フォルダのファイル取得
$files = $this->_findCabinetFilesByFolderDownload($cabinetFolder);
foreach ($files as $file) {
if ($file['CabinetFile']['is_folder']) {
$this->_checkDownloadFolder($file);
} else {
if (isset($file['AuthorizationKey'])) {
throw new BadRequestException(
__d(
'cabinets',
'Folder that contains the files that are password is set can not be downloaded ZIP.'
)
);
}
}
}
}
/**
* フォルダキーからフォルダを返す
*
* @param string $folderKey フォルダキー(CabinetFile.key)
* @return array|null
*/
protected function _getCurrentFolder($folderKey) {
if (is_null($folderKey)) {
// 指定がなければルートフォルダを取得する
$currentFolder = $this->CabinetFile->getRootFolder($this->_cabinet);
return $currentFolder;
} else {
// Hack: findFolderとかメソッドはやしたほうがいいかな。CabinetFolderってモデルつくるのがいいか?
$this->CabinetFile->Behaviors->disable('AuthorizationKey');
$this->CabinetFile->Behaviors->disable('Attachment');
$currentFolder = $this->CabinetFile->find(
'first',
[
'conditions' => [
'CabinetFile.key' => $folderKey,
//'CabinetFile.language_id' => Current::read('Language.id'),
'CabinetFile.is_latest' => true,
]
]
);
$this->CabinetFile->Behaviors->enable('AuthorizationKey');
$this->CabinetFile->Behaviors->enable('Attachment');
return $currentFolder;
}
}
/**
* 親フォルダのURLを返す
*
* @param array $currentFolder 現在位置のCabinetFileデータ(フォルダ)
* @param array $folderPath 現在位置までのTreeパス
* @return null|string 親フォルダのURL
*/
protected function _getParentFolderUrl($currentFolder, $folderPath) {
// 親フォルダのTreeIDがルートフォルダのTreeIDと違うなら親フォルダは通常フォルダ
$isRootFolder = ($currentFolder['CabinetFileTree']['parent_id'] === null);
$hasParentFolder = ($currentFolder['CabinetFileTree']['parent_id'] !=
$folderPath[0]['CabinetFileTree']['id']);
if ($isRootFolder) {
// root folder
$url = null;
} elseif ($hasParentFolder) {
// 親フォルダあり
$nestCount = count($folderPath);
$url = NetCommonsUrl::actionUrl(
[
'key' => $folderPath[$nestCount - 2]['CabinetFile']['key'],
'block_id' => Current::read('Block.id'),
'frame_id' => Current::read('Frame.id'),
]
);
} else {
// 親はキャビネット(ルートフォルダ)
$url = NetCommonsUrl::backToPageUrl(null, ['frame_id' => Current::read('Frame.id')]);
}
return $url;
}
/**
* ソートパラメータを返す
*
* @return array($sort, $direction)
*/
protected function _getSortParams() {
// ソートに使えるキーかチェック
$allowSortKeys = [
'filename',
'size',
'modified'
];
$sort = Hash::get($this->request->named, 'sort', 'filename');
// asc, descしか入力を許可しない
$direction = Hash::get($this->request->named, 'direction', 'asc');
if (!in_array($sort, $allowSortKeys) || !in_array($direction, ['asc', 'desc'])) {
return $this->throwBadRequest();
}
return array($sort, $direction);
}
/**
* カレントフォルダのファイル・フォルダリストを返す
*
* @param int $currentTreeId 現在のCabinetFileTree.id
* @return array CabinetFileの一覧
*/
protected function _getCurrentFolderFiles($currentTreeId) {
// カレントフォルダのファイル・フォルダリストを得る。
$conditions = [
'CabinetFileTree.parent_id' => $currentTreeId,
'Cabinet.key' => $this->viewVars['cabinet']['Cabinet']['key']
];
// workflowコンディションを混ぜ込む
$conditions = $this->CabinetFile->getWorkflowConditions($conditions);
// ソート順変更
list($sort, $direction) = $this->_getSortParams();
// ソート順変更リンクをPaginatorHelperで出力するときに必要な値をセットしておく。
$this->request->params['paging'] = [
'CabinetFile' => [
'options' => [
'sort' => $sort,
'direction' => $direction
],
'paramType' => 'named',
]
];
// 昇順のときフォルダが先、降順の時フォルダが後
$folderDirection = ($direction === 'asc') ? 'desc' : 'asc';
$order = [
'is_folder' => $folderDirection
];
if ($sort != 'size') {
$order['CabinetFile.' . $sort] = $direction;
}
$results = $this->CabinetFile->find(
'all',
[
'conditions' => $conditions,
'order' => $order
]
);
$folders = array();
$files = array();
foreach ($results as &$file) {
if ($file['CabinetFile']['is_folder']) {
$file['CabinetFile']['size'] = $this->CabinetFile->getTotalSizeByFolder($file);
$file['CabinetFile']['has_children'] = $this->CabinetFile->hasChildren($file);
$folders[] = $file;
} else {
$file['CabinetFile']['size'] = $file['UploadFile']['file']['size'];
$files[] = $file;
}
}
if ($sort == 'size') {
// sizeでのソートは自前でおこなう@
$files = Hash::sort($files, '{n}.CabinetFile.size', $direction, 'numeric');
// フォルダ・ファイルは別れるようにする。
$folders = Hash::sort($folders, '{n}.CabinetFile.size', $direction, 'numeric');
}
if ($direction == 'asc') {
// ascならフォルダ先 Hash::mergeだと上書きされてしまうのであえてarray_merge使用
$files = array_merge($folders, $files);
} else {
$files = array_merge($files, $folders);
}
return $files;
}
/**
* ZipDownloaderを返す
*
* @return ZipDownloader ZipDownloader
*/
protected function _getZipDownloader() {
$zipDownloader = new ZipDownloader();
return $zipDownloader;
}
}