-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideosController.php
More file actions
442 lines (391 loc) · 12.5 KB
/
VideosController.php
File metadata and controls
442 lines (391 loc) · 12.5 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
<?php
/**
* 動画表示系 Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('VideosAppController', 'Videos.Controller');
App::uses('ZipDownloader', 'Files.Utility');
App::uses('Video', 'Videos.Model');
/**
* Videos Controller
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Videos\Controller
*/
class VideosController extends VideosAppController {
/**
* 関連動画 もっと見る start limit
*
* @var int
*/
const START_LIMIT_RELATED_VIDEO = 3;
/**
* 関連動画 もっと見る max limit
*
* @var int
*/
const MAX_LIMIT_RELATED_VIDEO = 10;
/**
* (編集長以上が使える)ダウンロードリンクを使用する
* 使用しない場合はfalseに書き換える。
*
* @var bool
*/
public $useDownloadLink = true;
/**
* use model
*
* @var array
*/
public $uses = array(
'Categories.Category',
'ContentComments.ContentComment', // コンテンツコメント
'Videos.Video',
'Videos.VideoSetting',
'Videos.VideoFrameSetting',
);
/**
* use helpers
*
* @var array
* @see NetCommonsAppController::$helpers
*/
public $helpers = array(
'Categories.Category',
'ContentComments.ContentComment' => array(
'viewVarsKey' => array(
'contentKey' => 'video.Video.key',
'contentTitleForMail' => 'video.Video.title',
'useComment' => 'videoSetting.use_comment',
'useCommentApproval' => 'videoSetting.use_comment_approval',
),
),
'Likes.Like',
'NetCommons.DisplayNumber',
'NetCommons.TitleIcon',
'Videos.Video',
'Workflow.Workflow',
'Users.DisplayUser',
);
/**
* use components
*
* @var array
* @link http://book.cakephp.org/2.0/ja/controllers/components.html#configuring-components
* @link http://book.cakephp.org/2.0/ja/core-libraries/collections.html#id6 デフォルトのプライオリティ 10
* @see NetCommonsAppController::$components
* @see ContentCommentsComponent::beforeRender()
*
*/
public $components = array(
'ContentComments.ContentComments' => array(
'viewVarsKey' => array(
'contentKey' => 'video.Video.key',
'useComment' => 'videoSetting.use_comment',
),
'allow' => array('view'),
),
'Cookie',
'Paginator', // ページャ
'Files.Download',
);
/**
* beforeFilter
*
* @return void
* @see NetCommonsAppController::beforeFilter()
*/
public function beforeFilter() {
parent::beforeFilter();
// ブロック未選択は、何も表示しない
if (! Current::read('Block.id')) {
$this->setAction('emptyRender');
return false;
}
// ゲストアクセスOKのアクションを設定
$this->Auth->allow('tag', 'embed');
// FFMPEG有効フラグ
/* @see VideoBehavior::isFfmpegEnable() */
$this->set('isFfmpegEnable', $this->Video->isFfmpegEnable());
// (編集長以上が使える)ダウンロードリンクを使用する
$this->set('useDownloadLink', $this->useDownloadLink);
}
/**
* 一覧表示
*
* @return CakeResponse
*/
public function index() {
$conditions = array();
$this->set('listTitle', Current::read('BlocksLanguage.name'));
$this->set('filterDropDownLabel', __d('videos', 'All videos'));
$categoryId = Hash::get($this->request->params['named'], 'category_id', 0);
// URLから直入力対策
$categoryId = (int)$categoryId;
if ($categoryId) {
$category = $this->Category->find('first', array(
'recursive' => 0,
'conditions' => array('Category.id' => $categoryId),
));
// 存在するカテゴリのみ
if ($category) {
$this->set(
'listTitle',
__d('categories', 'Category') . ':' . $category['CategoriesLanguage']['name']
);
$this->set('filterDropDownLabel', $category['CategoriesLanguage']['name']);
$conditions['Video.category_id'] = $categoryId;
}
}
// 一覧取得
$results = $this->__list($conditions);
$this->set($results);
}
/**
* tag別一覧
*
* @return CakeResponse
*/
public function tag() {
$this->view = 'index';
$conditions = array();
$this->set('listTitle', Current::read('BlocksLanguage.name'));
$this->set('filterDropDownLabel', __d('videos', 'All videos'));
// indexとのちがいはtagIdでの絞り込みだけ
$tagId = Hash::get($this->request->params['named'], 'id', 0);
// URLから直入力対策
$tagId = (int)$tagId;
if ($tagId) {
// タグ名をタイトルに
$tag = $this->Video->getTagByTagId($tagId);
// 存在するタグのみ
if ($tag) {
$this->set('listTitle', __d('tags', 'tag') . ':' . $tag['Tag']['name']);
// これを有効にするにはentry_tag_linkもJOINして検索か。
$conditions['Tag.id'] = $tagId;
}
}
// 一覧取得
$results = $this->__list($conditions);
$this->set($results);
}
/**
* 詳細表示
*
* @return CakeResponse
*/
public function view() {
//動画の取得
$videoKey = $this->params['key'];
/** @see WorkflowBehavior::getWorkflowContents() */
$video = $this->Video->getWorkflowContents('first', array(
'recursive' => 1,
'conditions' => array(
'Video.key' => $videoKey
)
));
if (empty($video)) {
return $this->throwBadRequest();
}
$this->set('video', $video);
//新着データを既読にする
$this->Video->saveTopicUserStatus($video);
//関連動画の取得
$relatedVideos = $this->Video->getWorkflowContents('all', array(
'recursive' => 1,
'conditions' => array(
'Video.block_id' => Current::read('Frame.block_id'),
'Video.created_user' => $video['Video']['created_user'],
'NOT' => array(
'Video.id' => $video['Video']['id'],
),
),
'order' => 'Video.id DESC',
'limit' => self::MAX_LIMIT_RELATED_VIDEO
));
$this->set('relatedVideos', $relatedVideos);
// 利用系(コメント利用、高く評価を利用等)の設定取得
$videoSetting = $this->VideoSetting->getVideoSetting(); //データあり
$this->set('videoSetting', $videoSetting['VideoSetting']);
// クッキー対応
$cookie = $this->Cookie->read('video_history');
$cookieArray = explode(':', $cookie);
if (! in_array($video['Video']['id'], $cookieArray, true)) {
//再生回数 + 1 で更新
$playNumber = $this->Video->countUp($video);
$this->viewVars['video']['Video']['play_number'] = $playNumber;
// cookie value = コンテンツid & 区切り文字
$cookie = $cookie . $video['Video']['id'] . ':';
// CookieComponentは3501byte~3550byte位をwriteしても、cookieに書き込んでくれなくなる。
// このcookieは、動画の視聴回数の抑制に使っていて、そこまで重要ではない&1時間で消えるので、
// 3500byteを超えたら、cookieの内容をクリアする。
if (strlen($cookie) > 3500) {
$cookie = $video['Video']['id'] . ':';
}
// アクセス情報を記録
$this->Cookie->write('video_history', $cookie, false, '1 hour');
}
}
/**
* 埋め込み動画表示
*
* @return CakeResponse
*/
public function embed() {
$this->view();
$this->Components->unload('Pages.PageLayout');
$this->layout = 'Videos.embed';
}
/**
* 動画のzipダウンロード
*
* @return CakeResponse
* @throws NotFoundException 表示できない記事へのアクセス
* @throws ForbiddenException アクセス権なし
* @see DownloadComponent::doDownload()
*/
public function download() {
// ダウンロードリンク使わないなら、400
if (!$this->useDownloadLink) {
return $this->setAction('throwBadRequest');
}
// ブロック編集許可(編集長以上)持っていないなら403
if (!Current::permission('block_editable')) {
throw new ForbiddenException();
}
// ここから元コンテンツを取得する処理
//$this->_prepare();
$key = $this->params['key'];
$conditions = $this->Video->getConditions();
$conditions['Video.key'] = $key;
$query = array(
'conditions' => $conditions,
);
$video = $this->Video->find('first', $query);
// ここまで元コンテンツを取得する処理
// ダウンロード実行
if (!$video) {
// 表示できない記事へのアクセスなら404
throw new NotFoundException(__d('videos', 'Invalid video entry'));
}
// 圧縮用パスワードキーを求める
if (! empty($this->request->data['AuthorizationKey']['authorization_key'])) {
$zipPassword = $this->request->data['AuthorizationKey']['authorization_key'];
} else {
$this->_setFlashMessageAndRedirect($key,
__d('authorization_keys', 'please input compression password'));
return;
}
// ダウンロードファイル名はタイトルにする
$fileName = $video['Video']['title'];
$zipFileName = $fileName . '.zip';
$videoFileName = $fileName . '.mp4';
$realFilePath = UPLOADS_ROOT .
$video['UploadFile'][Video::VIDEO_FILE_FIELD]['path'] .
$video['UploadFile'][Video::VIDEO_FILE_FIELD]['id'] . DS .
$video['UploadFile'][Video::VIDEO_FILE_FIELD]['real_file_name'];
$zip = new ZipDownloader();
$zip->addFile($realFilePath, $videoFileName);
$zip->setPassword($zipPassword);
$zip->close();
return $zip->download($zipFileName);
}
/**
* _setFlashMessageAndRedirect
*
* @param string $contentKey コンテンツキー
* @param string $message flash error message
*
* @return void
*/
protected function _setFlashMessageAndRedirect($contentKey, $message) {
$this->NetCommons->setFlashNotification($message,
array('interval' => NetCommonsComponent::ALERT_VALIDATE_ERROR_INTERVAL));
$url = NetCommonsUrl::actionUrl(array(
'controller' => 'videos',
'action' => 'view',
'block_id' => Current::read('Block.id'),
//'frame_id' => Current::read('Frame.id'),
'key' => $contentKey
), true);
// 暫定対応:どうゆう訳だか、ここだと?frame_idが上記でセットされないので直書き
$url .= '?frame_id=' . Current::read('Frame.id');
$this->redirect($url);
}
/**
* 一覧取得
*
* @param array $extraConditions 追加conditions
* @return array 動画一覧及び、設定
* @throws Exception Paginatorによる例外
*/
private function __list($extraConditions = array()) {
$query['recursive'] = 0;
/* @see WorkflowBehavior::getWorkflowConditions() */
$query['conditions'] = $this->Video->getWorkflowConditions([
'Video.block_id' => Current::read('Block.id'),
]);
$query['conditions'] = Hash::merge($query['conditions'], $extraConditions);
// 表示系(並び順、表示件数)の設定取得
$videoFrameSetting = $this->VideoFrameSetting->getVideoFrameSetting(true);
//ソート
$order = $this->__order($videoFrameSetting);
$query['order'] = $order;
$results['displayOrderPaginator'] = key($order) . '.' . $order[key($order)];
//表示件数
$limit = Hash::get($this->request->params['named'], 'limit');
if ($limit) {
$query['limit'] = (int)$limit;
} else {
$query['limit'] = $videoFrameSetting['VideoFrameSetting']['display_number'];
}
$this->Paginator->settings = $query;
try {
$videos = $this->Paginator->paginate('Video');
} catch (Exception $ex) {
CakeLog::error($ex);
throw $ex;
}
$results['videos'] = $videos;
// 利用系(コメント利用、高く評価を利用等)の設定取得
$videoSetting = $this->VideoSetting->getVideoSetting();
$results['videoSetting'] = $videoSetting['VideoSetting'];
// カテゴリ
$categories = $this->Category->getCategories(Current::read('Block.id'),
Current::read('Room.id'));
$results['categories'] = $categories;
return $results;
}
/**
* ソート条件 取得
*
* @param array $videoFrameSetting videoFrameSetting
* @return array ソート条件
*/
private function __order($videoFrameSetting) {
$sort = Hash::get($this->request->params['named'], 'sort');
$direction = Hash::get($this->request->params['named'], 'direction');
//ソート
if (isset($sort) && isset($direction)) {
$order = array($sort => $direction);
} else {
$displayOrder = $videoFrameSetting['VideoFrameSetting']['display_order'];
if ($displayOrder == VideoFrameSetting::DISPLAY_ORDER_NEW) {
$order = array('Video.created' => 'desc');
} elseif ($displayOrder == VideoFrameSetting::DISPLAY_ORDER_TITLE) {
$order = array('Video.title' => 'asc');
} elseif ($displayOrder == VideoFrameSetting::DISPLAY_ORDER_PLAY) {
$order = array('Video.play_number' => 'desc');
} elseif ($displayOrder == VideoFrameSetting::DISPLAY_ORDER_LIKE) {
// Like.weight = like_count - unlike_count
$order = array('Like.weight' => 'desc');
}
}
return $order;
}
}