VideoController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\Controllers;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\CommentNotFoundException;
  5. use app\Exceptions\VideoNotFoundException;
  6. use app\Hajeebtok;
  7. use app\Types\DatabaseObjects\View;
  8. use Pecee\SimpleRouter\SimpleRouter;
  9. use Mimey\MimeTypes;
  10. use app\Interfaces\IRouteController;
  11. use app\Types\DatabaseObjects\Video;
  12. use app\Types\DatabaseObjects\Account;
  13. use app\Logger;
  14. use FFMpeg;
  15. class VideoController implements IRouteController
  16. {
  17. public static function getVideo(string $id): string
  18. {
  19. $signed_in = signed_in(request());
  20. $video_information = new Video($id);
  21. $video_information->Load();
  22. $mime_types = new MimeTypes();
  23. if($signed_in) { // Signed in
  24. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  25. } else { // not signed in
  26. $rand = rand(1, 48);
  27. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  28. }
  29. CORSHelper();
  30. if (file_exists($video_path)) {
  31. $video_contents = file_get_contents($video_path);
  32. $video_size = filesize($video_path);
  33. $mime = $mime_types->getMimeType(pathinfo($video_path, PATHINFO_EXTENSION));
  34. $response = response();
  35. $response->header("Content-Type: $mime");
  36. $response->header("Content-Length: $video_size");
  37. $response->header("Cache-Control: max-age=86400, public");
  38. return $video_contents;
  39. } else {
  40. throw new VideoNotFoundException($id, 404);
  41. }
  42. }
  43. public static function getInfo(string $id): string
  44. {
  45. $video_information = new Video($id);
  46. $video_information->Load();
  47. $author_information = new Account($video_information->author_id);
  48. $author_information->Load();
  49. CORSHelper();
  50. return api_json([
  51. "id" => $video_information->id,
  52. "title" => $video_information->title,
  53. "description" => $video_information->description,
  54. "likes" => $video_information->likes,
  55. "dislikes" => $video_information->dislikes,
  56. "comments" => $video_information->comments,
  57. "shares" => $video_information->shares,
  58. "author" => [
  59. "id" => $author_information->id,
  60. "pictureHash" => $author_information->picture_hash,
  61. "username" => $author_information->username
  62. ],
  63. ]);
  64. }
  65. public static function search(): string {
  66. $query = input("query");
  67. $video = new Video(title: $query);
  68. $videos = $video->LoadMany();
  69. // idk if this is the greatest thing to do but jetbrains phpfart ai recommended it and it looks fine to me /shurg
  70. $accounts = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE id IN (SELECT author_id FROM videos WHERE title LIKE :title)", ["title" => "%$query%"]);
  71. if(empty($accounts)) throw new AccountNotFoundException(0, 404);
  72. $data = [];
  73. foreach($videos as $vid) {
  74. $data[] = [
  75. "id" => $vid["id"],
  76. "title" => $vid["title"],
  77. "description" => $vid["description"],
  78. "likes" => $vid["likes"],
  79. "dislikes" => $vid["dislikes"],
  80. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $vid["id"]]),
  81. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $vid["id"]]),
  82. "author" => [
  83. "id" => $vid["author_id"],
  84. "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
  85. "username" => $accounts[$vid["author_id"] - 1]["username"]
  86. ],
  87. ];
  88. }
  89. CORSHelper();
  90. return api_json($data);
  91. }
  92. public static function getThumbnail(string $id): string
  93. {
  94. $signed_in = signed_in(request());
  95. $video_information = new Video($id);
  96. $video_information->Load();
  97. if($signed_in) { // Signed in
  98. $seconds = 2;
  99. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  100. $frame_path = APP_ROOT . "/usercontent/videos/$id/thumbnail.png";
  101. } else { // not signed in
  102. $rand = rand(1, 47);
  103. $seconds = 6;
  104. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  105. $frame_path = APP_ROOT . "/usercontent/fake_videos/$rand/thumbnail.png";
  106. }
  107. $mime_types = new MimeTypes();
  108. if(!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
  109. if(!file_exists($frame_path)) {
  110. Logger::Debug("Generating thumbnail for video $id");
  111. $ffmpeg = FFMpeg\FFMpeg::create();
  112. $video = $ffmpeg->open($video_path);
  113. $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
  114. $frame->save($frame_path);
  115. }
  116. CORSHelper();
  117. $mime = $mime_types->getMimeType(pathinfo($frame_path, PATHINFO_EXTENSION));
  118. $response = response();
  119. $response->header("Content-Type: $mime");
  120. $response->header("Cache-Control: max-age=86400, public");
  121. return file_get_contents($frame_path);
  122. }
  123. public static function getFeed(): string
  124. {
  125. $signed_in = signed_in(request());
  126. if(!$signed_in) throw new AccountNotFoundException(0, 404);
  127. $id = get_token_id(request());
  128. $view = new View(account_id: $id);
  129. $view->LoadMany();
  130. CORSHelper();
  131. return api_json([]);
  132. }
  133. public static function RegisterRoutes(): void
  134. {
  135. SimpleRouter::group([
  136. "prefix" => "/video",
  137. ], function () {
  138. SimpleRouter::get("/{id}", [VideoController::class, "getVideo"]);
  139. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  140. SimpleRouter::get("/{id}/thumbnail", [VideoController::class, "getThumbnail"]);
  141. SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  142. SimpleRouter::post("/search", [VideoController::class, "search"]);
  143. SimpleRouter::options("/{id}", "CORSHelper");
  144. SimpleRouter::options("/{id}/info", "CORSHelper");
  145. SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
  146. SimpleRouter::options("/upload", "CORSHelper");
  147. SimpleRouter::options("/search", "CORSHelper");
  148. });
  149. }
  150. }