VideoController.php 14 KB

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