-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideo.php
More file actions
413 lines (360 loc) · 10.9 KB
/
Video.php
File metadata and controls
413 lines (360 loc) · 10.9 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
<?php
/**
* Video Model
*
* @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('VideosAppModel', 'Videos.Model');
App::uses('VideoValidationBehavior', 'Videos.Model/Behavior');
/**
* Video Model
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Videos\Model
*/
class Video extends VideosAppModel {
/**
* @var string file field name 動画ファイル
*/
const VIDEO_FILE_FIELD = 'video_file';
/**
* @var string file field name サムネイル
*/
const THUMBNAIL_FIELD = 'thumbnail';
/**
* @var string 動画 拡張子
*/
const VIDEO_EXTENSION = 'mpeg,mpg,mpe,avi,mov,wmv,asf,flv,mp4';
/**
* @var string 動画 MIMEタイプ
*/
// @codingStandardsIgnoreStart
// [.]よる文字連結で複数行にすると syntax error になるため、phpcs(1行100文字制限)を除外
const VIDEO_MIME_TYPE = 'video/mpeg,video/x-mpeg,video/avi,video/msvideo,video/x-msvideo,video/quicktime,video/x-ms-wmv,video/x-ms-asf,video/x-flv,video/mp4';
// @codingStandardsIgnoreEnd
/**
* @var string サムネイル 拡張子
*/
const THUMBNAIL_EXTENSION = 'jpg,png,gif';
/**
* @var string サムネイル MIMEタイプ
*/
const THUMBNAIL_MIME_TYPE = 'image/jpeg,image/png,image/gif';
/**
* use behaviors
*
* @var array
* @see MailQueueBehavior
*/
public $actsAs = array(
'ContentComments.ContentComment',
'Likes.Like', // いいね
'NetCommons.OriginalKey', // 自動でkeyセット
'Tags.Tag',
'Videos.Video', // 動画変換
'Videos.VideoValidation', // Validation rules
'Workflow.Workflow', // 自動でis_active, is_latestセット
'Workflow.WorkflowComment',
// 自動でメールキューの登録, 削除。ワークフロー利用時はWorkflow.Workflowより下に記述する
'Mails.MailQueue' => array(
'embedTags' => array(
'X-SUBJECT' => 'Video.title',
'X-BODY' => 'Video.description',
),
'embedTagsWysiwyg' => array(),
),
'Mails.MailQueueDelete',
'Files.Attachment' => [
Video::VIDEO_FILE_FIELD,
Video::THUMBNAIL_FIELD,
],
'Topics.Topics' => array(
'fields' => array(
'title' => 'Video.title',
'summary' => 'Video.description',
'path' => '/:plugin_key/:plugin_key/view/:block_id/:content_key',
),
),
'Wysiwyg.Wysiwyg' => array(
'fields' => array('description'),
),
//多言語
'M17n.M17n' => array(
'commonFields' => array('category_id', 'title_icon'),
'associations' => array(
'UploadFilesContent' => array(
'class' => 'Files.UploadFilesContent',
'foreignKey' => 'content_id',
'isM17n' => true
),
'TagsContent' => array(
'class' => 'Tags.TagsContent',
'foreignKey' => 'content_id',
'fieldForIdentifyPlugin' => array('field' => 'model', 'value' => 'Video'),
'isM17n' => true
),
),
),
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Category' => array(
'className' => 'Categories.Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Block' => array(
'className' => 'Blocks.Block',
'foreignKey' => 'block_id',
'conditions' => '',
'fields' => '',
'order' => '',
'counterCache' => array(
'content_count' => array(
'Video.is_origin' => true,
'Video.is_latest' => true
),
),
),
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* Called before each find operation. Return false if you want to halt the find
* call, otherwise return the (modified) query data.
*
* @param array $query Data used to execute this query, i.e. conditions, order, etc.
* @return mixed true if the operation should continue, false if it should abort; or, modified
* $query to continue with new $query
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforefind
*/
public function beforeFind($query) {
if (Hash::get($query, 'recursive') > -1 && ! $this->id) {
$belongsTo = $this->Category->bindModelCategoryLang('Video.category_id');
$this->bindModel($belongsTo, true);
}
return true;
}
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
/* @see VideoValidationBehavior::setVideoValidationSettings() */
/* @see VideoBehavior::isFfmpegEnable() */
$this->setVideoValidationSettings(VideoValidationBehavior::IS_FFMPEG_ENABLE,
$this->isFfmpegEnable());
/* @see VideoValidationBehavior::rules() */
$this->validate = $this->rules($options);
return parent::beforeValidate($options);
}
/**
* 総容量取得
*
* @return int
*/
public function getTotalSize() {
$this->loadModels(array(
'UploadFile' => 'Files.UploadFile',
));
$video = $this->find('first', array(
'recursive' => -1,
'joins' => array (
array (
'type' => 'LEFT',
'table' => $this->UploadFile->table,
'alias' => 'UploadFile',
'conditions' => 'UploadFile.content_key = Video.key',
),
),
'fields' => array(
'SUM(UploadFile.size) AS total_size',
),
'conditions' => array(
'Video.block_id' => Current::read('Block.id'),
'Video.is_latest' => true,
),
));
return (int)$video[0]['total_size'];
}
/**
* UserIdと権限から参照可能なEntryを取得するCondition配列を返す
*
* @return array condition
*/
public function getConditions() {
// contentReadable falseなら何も見えない
if (! Current::permission('content_readable')) {
$conditions = array('Video.id' => 0); // ありえない条件でヒット0にしてる
return $conditions;
}
// デフォルト絞り込み条件
$conditions = array(
'Video.block_id' => Current::read('Block.id')
);
$conditions = $this->getWorkflowConditions($conditions);
return $conditions;
}
/**
* 再生回数 + 1 で更新
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function countUp($data) {
$this->id = $data['Video']['id'];
$playNumber = $data['Video']['play_number'];
//再生回数 + 1
$playNumber++;
//トランザクションBegin
$this->begin();
try {
// コールバックoff
$validate = array(
'validate' => false,
'callbacks' => false,
);
// 再生回数のみ更新
if (! $this->saveField('play_number', $playNumber, $validate)) {
throw new InternalErrorException('Failed ' . __METHOD__);
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return $playNumber;
}
/**
* Videoデータ保存
*
* @param array $data received post data
* @param int $isEdit 編集か
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveVideo($data, $isEdit = 0) {
$this->loadModels(array(
'VideoSetting' => 'Videos.VideoSetting',
));
//トランザクションBegin
$this->begin();
if ($isEdit) {
$options = array('edit');
} else {
$options = array('add');
}
$data['Video']['category_id'] = Hash::get($data, 'Video.category_id', '0');
//バリデーション
$this->set($data);
/* @see beforeValidate() */
if (! $this->validates($options)) {
return false;
}
try {
if (! $video = $this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// Ffmpeg=ON and 動画あり
/* @see VideoBehavior::isFfmpegEnable() */
if ($this->isFfmpegEnable() &&
isset($data[$this->alias][Video::VIDEO_FILE_FIELD]['size']) &&
$data[$this->alias][Video::VIDEO_FILE_FIELD]['size'] !== 0) {
// 動画変換とデータ保存
/* @see VideoBehavior::saveConvertVideo() */
$this->saveConvertVideo($video);
}
// 総容量更新
$totalSize = $this->getTotalSize();
$this->VideoSetting->saveTotalSize($totalSize);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return $video;
}
/**
* Videoデータ削除
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function deleteVideo($data) {
$this->loadModels(array(
'Like' => 'Likes.Like',
'TagsContent' => 'Tags.TagsContent',
'UploadFile' => 'Files.UploadFile',
'VideoSetting' => 'Videos.VideoSetting',
));
//トランザクションBegin
$this->begin();
// アップロードファイル
$uploadFiles = $this->UploadFile->find('all', array(
'recursive' => 1,
'conditions' => array($this->UploadFile->alias . '.content_key' => $data['Video']['key']),
'callbacks' => false,
));
try {
// 動画削除($callbacks = true)
$this->contentKey = $data['Video']['key'];
if (! $this->deleteAll(array($this->alias . '.key' => $data['Video']['key']), false, true)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// 動画とサムネイルのデータと物理ファイル削除
foreach ($uploadFiles as $uploadFile) {
foreach ($uploadFile['UploadFilesContent'] as $uploadFilesContent) {
$this->UploadFile->removeFile($uploadFilesContent['content_id'],
$uploadFilesContent['upload_file_id']);
}
}
// アップロードファイル 削除
$conditions = array($this->UploadFile->alias . '.content_key' => $data['Video']['key']);
if (! $this->UploadFile->deleteAll($conditions, false, true)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// タグコンテンツ 削除
$conditions = array($this->TagsContent->alias . '.content_id' => $data['Video']['id']);
if (! $this->TagsContent->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// いいね 削除
$conditions = array($this->Like->alias . '.content_key' => $data['Video']['key']);
if (! $this->Like->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// 総容量更新
$totalSize = $this->getTotalSize();
$this->VideoSetting->saveTotalSize($totalSize);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
}