VideoController.php 5.9 KB

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