-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoControl.php
More file actions
210 lines (174 loc) · 6.18 KB
/
VideoControl.php
File metadata and controls
210 lines (174 loc) · 6.18 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
<?php
require_once "Atrox/Core/Data/Data.php";
require_once "Gawk/Video/VideoDataEntity.php";
class VideoControl extends DataControl {
public $table = "Video";
public $key = "Id";
public $sequence = "Video_Id_seq";
public $defaultOrder = "Id";
public $searchFields = array("Id");
const SOURCE_FLASH = "flash";
const SOURCE_IPHONE = "iphone";
const SOURCE_HTML = "html";
const SOURCE_ANDROID = "android";
public function init() {
$this->fieldMeta["Id"] = new FieldMeta(
"Id", "", FM_TYPE_INTEGER, null, FM_STORE_NEVER, false);
$this->fieldMeta["SecureId"] = new FieldMeta(
"Secure ID", "", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["WallSecureId"] = new FieldMeta(
"Wall Secure ID", "main", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["MemberSecureId"] = new FieldMeta(
"Member Secure ID", "", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["Filename"] = new FieldMeta(
"Filename", "", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["DateCreated"] = new FieldMeta(
"DateCreated", "", FM_TYPE_DATE, null, FM_STORE_NEVER, false);
$this->fieldMeta["Approved"] = new FieldMeta(
"Approved", "t", FM_TYPE_BOOLEAN, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["Banned"] = new FieldMeta(
"Banned", "", FM_TYPE_BOOLEAN, null, FM_STORE_ALWAYS, true);
$this->fieldMeta["IpAddress"] = new FieldMeta(
"IP Address", "", FM_TYPE_IP, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["UploadSource"] = new FieldMeta(
"Upload Source", "", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["Rating"] = new FieldMeta(
"Rating", 0, FM_TYPE_INTEGER, null, FM_STORE_ALWAYS, false);
$this->fieldMeta["Hash"] = new FieldMeta(
"Hash", "", FM_TYPE_STRING, null, FM_STORE_ALWAYS, false);
}
public function saveVideo(Video $video, array $filesData = null) {
$wallControl = Factory::getWallControl();
$wallId = $video->wallSecureId ? $video->wallSecureId : "main";
if (!$wall = $wallControl->itemByField($wallId, "SecureId")) {
$this->errorControl->addError("Wall not found: {$video->wallSecureId}");
return false;
}
$videoFileUpload = Factory::getVideoFileUpload();
if (!$fileName = $videoFileUpload->saveFile($video, $this, $filesData, $video->uploadSource)) {
return false;
}
$video->filename = $fileName;
$videoDataEntity = $this->mapVideoToVideoDataEntity($video);
if ($videoDataEntity->save()) {
return $videoDataEntity;
}
}
public function validateHash(VideoDataEntity $video) {
$hash = sha1_file($this->application->registry->get("Binary/Path") . "/" . $video->get("Filename"));
if ($hash != $video->get("Hash")) {
$this->errorControl->addError("Hash does not match", "InvalidHash");
return false;
}
return true;
}
/**
* Map a Video to VideoDataEntity
* @param Video $video
* @param VideoDataEntity $videoDataEntity
* @return VideoDataEntity
*/
public function mapVideoToVideoDataEntity(Video $video, VideoDataEntity $videoDataEntity = null) {
if (!$videoDataEntity) {
$videoDataEntity = $this->makeNew();
}
foreach ((array)$video as $key => $value) {
if (($value !== null) && ($value !== "") && is_scalar($value)) {
$fieldCamelCase = ucfirst($key);
if (isset($this->fieldMeta[$fieldCamelCase])) {
$videoDataEntity->set($fieldCamelCase, $value);
}
}
}
return $videoDataEntity;
}
/**
* Get Video by request URL
* @param string $requestUrl
* @return Video
*/
public function getVideoByRequestUrl($requestUrl) {
$requestPieces = explode("/", trim($requestUrl));
$videoSecureId = $requestPieces[4];
if (!$videoDataEntity = $this->getVideoDataEntityBySecureId($videoSecureId, true)) {
return false;
}
return $videoDataEntity->toObject();
}
public function makeNew() {
$videoDataEntity = parent::makeNew();
$videoDataEntity->set("SecureId", $this->getRandomSecureId());
return $videoDataEntity;
}
public function isVideoFilePresent($fileName) {
return file_exists($this->application->registry->get("Binary/Path") . "/" . $fileName);
}
public function getRandomSecureId() {
return uniqid();
}
public function getFileExtensionByMimeType($mimeType) {
switch ($mimeType) {
case "video/quicktime":
return "mov";
case "video/x-flv":
return "flv";
case "video/mp4":
return "mp4";
default:
$this->errorControl->addError("Unsupported mime type $mimeType");
break;
}
}
/**
* @param string $secureId
* @param boolean $allowCaseInsensitive
* @return VideoDataEntity
*/
public function getVideoDataEntityBySecureId($secureId, $allowCaseInsensitive = false) {
$this->reset();
$filter = CoreFactory::getFilter();
$filter->addConditional($this->table, "SecureId", $secureId, $allowCaseInsensitive ? "ILIKE" : "=");
$this->setFilter($filter);
return $this->getNext();
}
public function deleteBySecureId($secureId) {
if (!$video = $this->getVideoDataEntityBySecureId($secureId)) {
$this->errorControl->addError("Unable to retrieve video: $secureId");
return false;
}
$this->delete($video->get("Id"));
return true;
}
public function getDataEntity() {
return new VideoDataEntity($this);
}
/**
* Utility function to find out the number of Gawks a member has made
* @param Member $member
* @return integer
*/
public function getVideoCountByMember(Member $member) {
$sql = <<<SQL
SELECT COUNT(*) FROM "Video" WHERE "MemberSecureId" = '{$member->secureId}';
SQL;
$this->initTable();
$result = $this->databaseControl->query($sql);
$row = $this->databaseControl->fetchRow($result);
return $row[0];
}
/**
* @param Member $member
* @return Video
*/
public function getLastVideoForMember(Member $member) {
$this->reset();
$filter = CoreFactory::getFilter();
$filter->addConditional($this->table, "MemberSecureId", $member->secureId);
$filter->addOrder("DateCreated", true);
$filter->addLimit(1);
$this->setFilter($filter);
if ($videoDataEntity = $this->getNext()) {
return $videoDataEntity->toObject();
}
}
}