VideoController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "title" => $vid["title"],
  73. "description" => $vid["description"],
  74. "likes" => $vid["likes"],
  75. "dislikes" => $vid["dislikes"],
  76. "comments" => $vid["comments"],
  77. "shares" => $vid["shares"],
  78. "author" => [
  79. "id" => $vid["author_id"],
  80. "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
  81. "username" => $accounts[$vid["author_id"] - 1]["username"]
  82. ],
  83. ];
  84. }
  85. return api_json($data);
  86. }
  87. public static function getThumbnail(string $id): string
  88. {
  89. $signed_in = signed_in(request());
  90. $video_information = new Video($id);
  91. $video_information->Load();
  92. if($signed_in) { // Signed in
  93. $seconds = 2;
  94. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  95. $frame_path = APP_ROOT . "/usercontent/videos/$id/thumbnail.png";
  96. } else { // not signed in
  97. $rand = rand(1, 47);
  98. $seconds = 6;
  99. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  100. $frame_path = APP_ROOT . "/usercontent/fake_videos/$rand/thumbnail.png";
  101. }
  102. $mime_types = new MimeTypes();
  103. if(!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
  104. if(!file_exists($frame_path)) {
  105. $ffmpeg = FFMpeg\FFMpeg::create();
  106. $video = $ffmpeg->open($video_path);
  107. $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
  108. $frame->save($frame_path);
  109. }
  110. CORSHelper();
  111. $mime = $mime_types->getMimeType(pathinfo($frame_path, PATHINFO_EXTENSION));
  112. $response = response();
  113. $response->header("Content-Type: $mime");
  114. $response->header("Cache-Control: max-age=86400, public");
  115. return file_get_contents($frame_path);
  116. }
  117. public static function RegisterRoutes(): void
  118. {
  119. SimpleRouter::group([
  120. "prefix" => "/video",
  121. ], function () {
  122. SimpleRouter::get("/{id}", [VideoController::class, "getVideo"]);
  123. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  124. SimpleRouter::get("/{id}/thumbnail", [VideoController::class, "getThumbnail"]);
  125. SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  126. SimpleRouter::post("/search", [VideoController::class, "search"]);
  127. SimpleRouter::options("/{id}", "CORSHelper");
  128. SimpleRouter::options("/{id}/info", "CORSHelper");
  129. SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
  130. });
  131. }
  132. }