VideoController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace app\Controllers;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\CommentNotFoundException;
  5. use app\Exceptions\InvalidRequestException;
  6. use app\Exceptions\InvalidVideoException;
  7. use app\Exceptions\UnauthenticatedException;
  8. use app\Exceptions\VideoNotFoundException;
  9. use app\Hajeebtok;
  10. use app\Types\DatabaseObjects\View;
  11. use Cassandra\Exception\UnauthorizedException;
  12. use Exception;
  13. use app\Exceptions\SecurityFaultException;
  14. use Pecee\SimpleRouter\SimpleRouter;
  15. use Mimey\MimeTypes;
  16. use app\Interfaces\IRouteController;
  17. use app\Types\DatabaseObjects\Video;
  18. use app\Types\DatabaseObjects\Account;
  19. use app\Logger;
  20. use FFMpeg;
  21. class VideoController implements IRouteController
  22. {
  23. public static function getVideo(string $id): string
  24. {
  25. $signed_in = signed_in(request());
  26. if($signed_in) {
  27. $video_information = new Video($id);
  28. $video_information->Load();
  29. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  30. }
  31. else { // not signed in
  32. $rand = rand(1, 47);
  33. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  34. }
  35. CORSHelper();
  36. Logger::Debug($video_path);
  37. if (file_exists($video_path)) {
  38. $mime_types = new MimeTypes();
  39. $video_contents = file_get_contents($video_path);
  40. $video_size = filesize($video_path);
  41. $mime = $mime_types->getMimeType(pathinfo($video_path, PATHINFO_EXTENSION));
  42. $response = response();
  43. $response->header("Content-Type: $mime");
  44. $response->header("Content-Length: $video_size");
  45. $response->header("Cache-Control: no-cache");
  46. return $video_contents;
  47. } else {
  48. throw new VideoNotFoundException($id, 404);
  49. }
  50. }
  51. public static function getInfo(string $id): string
  52. {
  53. $signed_in = signed_in(request());
  54. if(!$signed_in) {
  55. $data = [];
  56. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  57. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  58. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  59. for($i = 0; $i < rand(1, 13); $i++) {
  60. $data[] = [
  61. "id" => rand(0, 203222),
  62. "title" => $titles[rand(0, count($titles) - 1)],
  63. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  64. "likes" => rand(0, 48573),
  65. "dislikes" => rand(0, 202),
  66. "comments" => rand(0, 2029),
  67. "shares" => rand(15, 2321),
  68. "author" => [
  69. "id" => rand(2, 2020),
  70. "username" => $usernames[rand(0, count($usernames) - 1)],
  71. "verified" => rand(1, 10) > 8
  72. ],
  73. ];
  74. }
  75. CORSHelper();
  76. return api_json($data);
  77. }
  78. $video_information = new Video($id);
  79. $video_information->Load();
  80. $author_information = new Account($video_information->author_id);
  81. $author_information->Load();
  82. CORSHelper();
  83. return api_json([
  84. "id" => $video_information->id,
  85. "title" => $video_information->title,
  86. "description" => $video_information->description,
  87. "likes" => $video_information->likes,
  88. "dislikes" => $video_information->dislikes,
  89. "comments" => $video_information->comments,
  90. "shares" => $video_information->shares,
  91. "author" => [
  92. "id" => $author_information->id,
  93. "verified" => $author_information->verified,
  94. "username" => $author_information->username
  95. ],
  96. ]);
  97. }
  98. public static function search(): string
  99. {
  100. $signed_in = signed_in(request());
  101. if(!$signed_in) {
  102. $data = [];
  103. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  104. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  105. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  106. for($i = 0; $i < rand(1, 13); $i++) {
  107. $data[] = [
  108. "id" => rand(0, 203222),
  109. "title" => $titles[rand(0, count($titles) - 1)],
  110. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  111. "likes" => rand(0, 48573),
  112. "dislikes" => rand(0, 202),
  113. "comments" => rand(0, 2029),
  114. "shares" => rand(15, 2321),
  115. "author" => [
  116. "id" => rand(2, 2020),
  117. "username" => $usernames[rand(0, count($usernames) - 1)],
  118. "verified" => rand(1, 10) > 8
  119. ],
  120. ];
  121. }
  122. CORSHelper();
  123. return api_json($data);
  124. }
  125. $query = input("query");
  126. $video = new Video(title: $query);
  127. $videos = $video->LoadMany();
  128. $data = [];
  129. foreach ($videos as $vid) {
  130. $account = new Account(id: $vid["author_id"]);
  131. $account->Load();
  132. $data[] = [
  133. "id" => $vid["id"],
  134. "title" => $vid["title"],
  135. "description" => $vid["description"],
  136. "likes" => $vid["likes"],
  137. "dislikes" => $vid["dislikes"],
  138. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $vid["id"]]),
  139. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $vid["id"]]),
  140. "author" => [
  141. "id" => $account->id,
  142. "verified" => $account->verified,
  143. "username" => $account->username
  144. ],
  145. ];
  146. }
  147. CORSHelper();
  148. return api_json($data);
  149. }
  150. public static function getThumbnail(string $id): string
  151. {
  152. $signed_in = signed_in(request());
  153. if ($signed_in) { // Signed in
  154. //$video_information = new Video($id);
  155. //$video_information->Load();
  156. $seconds = 2;
  157. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  158. $frame_path = APP_ROOT . "/usercontent/videos/$id/thumbnail.png";
  159. } else { // not signed in
  160. $rand = rand(1, 47);
  161. $seconds = 6;
  162. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  163. $frame_path = APP_ROOT . "/usercontent/fake_videos/$rand/thumbnail.png";
  164. }
  165. $mime_types = new MimeTypes();
  166. if (!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
  167. if (!file_exists($frame_path)) {
  168. Logger::Debug("Generating thumbnail for video $id");
  169. $ffmpeg = FFMpeg\FFMpeg::create();
  170. $video = $ffmpeg->open($video_path);
  171. $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
  172. $frame->save($frame_path);
  173. }
  174. CORSHelper();
  175. $mime = $mime_types->getMimeType(pathinfo($frame_path, PATHINFO_EXTENSION));
  176. $response = response();
  177. $response->header("Content-Type: $mime");
  178. $response->header("Cache-Control: max-age=no-cache");
  179. return file_get_contents($frame_path);
  180. }
  181. public static function getFeed(): string
  182. {
  183. $offset = intval(input("offset") ?? "0");
  184. $limit = intval(Hajeebtok::$Config->GetByDotKey("Service.VideoFeedLimit"));
  185. $signed_in = signed_in(request());
  186. if (!$signed_in) {
  187. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  188. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  189. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  190. $feed = [];
  191. for($i = 0; $i < $limit; $i++) {
  192. $feed[] = [
  193. "id" => rand(1, 10000),
  194. "title" => $titles[rand(0, count($titles) - 1)],
  195. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  196. "likes" => rand(20000, 37020),
  197. "dislikes" => rand(2, 12343),
  198. "comments" => rand(2, 1029),
  199. "shares" => rand(2, 200000),
  200. "author" => [
  201. "id" => rand(2, 59),
  202. "verified" => rand(1, 10) > 8,
  203. "username" => $usernames[rand(0, count($usernames) - 1)],
  204. ],
  205. ];
  206. }
  207. CORSHelper();
  208. return api_json($feed);
  209. }
  210. $id = get_token_id(request());
  211. $feed = [];
  212. $videos = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE id NOT IN (SELECT video_id FROM views WHERE account_id = :account_id) ORDER BY id DESC LIMIT :limit OFFSET :offset", ["account_id" => $id, "offset" => $offset, "limit" => $limit]);
  213. $vid_count = count($videos);
  214. if ($vid_count < $limit) {
  215. $fallback_videos = Hajeebtok::$Database->Query("SELECT * FROM videos ORDER BY id DESC LIMIT :remainder OFFSET :offset", ["offset" => $offset + $vid_count, "remainder" => $limit - $vid_count]);
  216. $videos = array_merge($videos, $fallback_videos);
  217. }
  218. foreach ($videos as $video) {
  219. $account = new Account($video["author_id"]);
  220. $account->Load();
  221. $feed[] = [
  222. "id" => $video["id"],
  223. "title" => $video["title"],
  224. "description" => $video["description"],
  225. "likes" => $video["likes"],
  226. "dislikes" => $video["dislikes"],
  227. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
  228. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
  229. "author" => [
  230. "id" => $video["author_id"],
  231. "verified" => $account->verified,
  232. "username" => $account->username,
  233. ],
  234. ];
  235. }
  236. CORSHelper();
  237. return api_json($feed);
  238. }
  239. public static function uploadVideo(): string
  240. {
  241. if (!signed_in(request())) throw new UnauthenticatedException(0, 401);
  242. $video_file = input()->file("video");
  243. $title = input("title");
  244. $description = input("description");
  245. $author_id = get_token_id(request());
  246. if (empty($video_file) || empty($title) || empty($author_id)) throw new InvalidRequestException(0, 400);
  247. if (!file_exists($video_file->getTmpName()))
  248. throw new InvalidRequestException(400);
  249. // save database object
  250. $video = new Video(title: $title, description: $description, author_id: $author_id);
  251. $video->Save();
  252. // Save file
  253. $video_folder = APP_ROOT . "/usercontent/videos/" . $video->id;
  254. // check video file type
  255. $mimey = new MimeTypes();
  256. $mime_type = $mimey->getMimeType($video_file->getExtension());
  257. if ($mime_type != "video/mp4" && $mime_type != "video/ogg" && $mime_type != "video/webm" && $mime_type != "video/x-matroska") {
  258. $video->Delete();
  259. throw new InvalidVideoException($video->id, 415); // media type unsupported
  260. }
  261. // move video
  262. mkdir($video_folder);
  263. Logger::Debug($video_file->getFilename() . " -> " . $video_folder . "/video.mp4");
  264. $video_file->move($video_folder . "/video.mp4");
  265. CORSHelper();
  266. return api_json([
  267. "id" => $video->id,
  268. "title" => $video->title,
  269. "description" => $video->description,
  270. "likes" => $video->likes,
  271. "dislikes" => $video->dislikes,
  272. "comments" => 0,
  273. "shares" => 0,
  274. "author" => [
  275. "id" => $author_id,
  276. ]
  277. ]);
  278. }
  279. public static function getExplore() {
  280. $video_num = intval(input("video") ?? "0");
  281. $offset = intval(input("offset") ?? "0");
  282. $limit = intval(Hajeebtok::$Config->GetByDotKey("Service.VideoFeedLimit"));
  283. $signed_in = signed_in(request());
  284. if (!$signed_in) {
  285. $feed = [];
  286. for($i = 0; $i < $limit; $i++) {
  287. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  288. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  289. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  290. $feed[] = [
  291. "id" => rand(1, 100000),
  292. "title" => $titles[rand(0, count($titles) - 1)],
  293. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  294. "likes" => rand(20000, 37020),
  295. "dislikes" => rand(2, 12343),
  296. "comments" => rand(2, 1029),
  297. "shares" => rand(2, 200000),
  298. "author" => [
  299. "id" => rand(2, 59),
  300. "verified" => rand(1, 10) > 8,
  301. "username" => $usernames[rand(0, count($usernames) - 1)],
  302. ],
  303. ];
  304. }
  305. CORSHelper();
  306. return api_json($feed);
  307. }
  308. $id = get_token_id(request());
  309. $videos = Hajeebtok::$Database->Query("SELECT * FROM videos ORDER BY RAND() LIMIT :limit OFFSET :offset", ["offset" => $offset, "limit" => $limit]);
  310. $feed = [];
  311. $valid_video = false;
  312. foreach ($videos as $video) {
  313. if ($video_num === 0) $valid_video = true;
  314. if ($video["id"] == $video_num) $valid_video = true;
  315. Logger::Debug($video["id"] . " -> " . $video["title"]);
  316. if (!$valid_video) continue;
  317. $account = new Account($video["author_id"]);
  318. $account->Load();
  319. $feed[] = [
  320. "id" => $video["id"],
  321. "title" => $video["title"],
  322. "description" => $video["description"],
  323. "likes" => $video["likes"],
  324. "dislikes" => $video["dislikes"],
  325. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
  326. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
  327. "author" => [
  328. "id" => $video["author_id"],
  329. "verified" => $account->verified,
  330. "username" => $account->username,
  331. ],
  332. ];
  333. }
  334. CORSHelper();
  335. return api_json($feed);
  336. }
  337. public static function RegisterRoutes(): void
  338. {
  339. SimpleRouter::group([
  340. "prefix" => "/video",
  341. ], function () {
  342. SimpleRouter::get("/{id}/get", [VideoController::class, "getVideo"]);
  343. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  344. SimpleRouter::get("/{id}/thumbnail", [VideoController::class, "getThumbnail"]);
  345. SimpleRouter::get("/feed", [VideoController::class, "getFeed"]);
  346. SimpleRouter::get("/explore", [VideoController::class, "getExplore"]);
  347. SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  348. SimpleRouter::post("/search", [VideoController::class, "search"]);
  349. SimpleRouter::options("/{id}/get", "CORSHelper");
  350. SimpleRouter::options("/{id}/info", "CORSHelper");
  351. SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
  352. SimpleRouter::options("/upload", "CORSHelper");
  353. SimpleRouter::options("/search", "CORSHelper");
  354. SimpleRouter::options("/feed", "CORSHelper");
  355. SimpleRouter::options("/explore", "CORSHelper");
  356. });
  357. }
  358. }