VideoController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\Controllers;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\VideoNotFoundException;
  5. use app\Hajeebtok;
  6. use Pecee\SimpleRouter\SimpleRouter;
  7. use Mimey\MimeTypes;
  8. use app\Interfaces\IRouteController;
  9. use app\Types\DatabaseObjects\Video;
  10. use app\Types\DatabaseObjects\Account;
  11. use app\Logger;
  12. class VideoController implements IRouteController
  13. {
  14. public static function getVideo(string $id): string
  15. {
  16. $signedIn = signed_in(request());
  17. $videoInformation = new Video($id);
  18. $videoInformation->Load();
  19. $mimeTypes = new MimeTypes();
  20. if($signedIn) { // Signed in
  21. $videoPath = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  22. } else { // not signed in
  23. $videoPath = APP_ROOT . "/usercontent/videos/1/video.mp4";
  24. }
  25. CORSHelper();
  26. if (file_exists($videoPath)) {
  27. $videoContents = file_get_contents($videoPath);
  28. $videoSize = filesize($videoPath);
  29. $mime = $mimeTypes->getMimeType(pathinfo($videoPath, PATHINFO_EXTENSION));
  30. $response = response();
  31. $response->header("Content-Type: $mime");
  32. $response->header("Content-Length: $videoSize");
  33. $response->header("Cache-Control: max-age=86400, public");
  34. return $videoContents;
  35. } else {
  36. throw new VideoNotFoundException($id, 404);
  37. }
  38. }
  39. public static function getInfo(string $id): string
  40. {
  41. $videoInformation = new Video($id);
  42. $videoInformation->Load();
  43. $authorInformation = new Account($videoInformation->author_id);
  44. $authorInformation->Load();
  45. return api_json([
  46. "title" => $videoInformation->title,
  47. "description" => $videoInformation->description,
  48. "likes" => $videoInformation->likes,
  49. "dislikes" => $videoInformation->dislikes,
  50. "comments" => $videoInformation->comments,
  51. "shares" => $videoInformation->shares,
  52. "author" => [
  53. "id" => $authorInformation->id,
  54. "pictureHash" => $authorInformation->picture_hash,
  55. "username" => $authorInformation->username
  56. ],
  57. ]);
  58. }
  59. public static function search(): string {
  60. $query = input("query");
  61. $video = new Video(title: $query);
  62. $videos = $video->LoadMany();
  63. // idk if this is the greatest thing to do but jetbrains phpfart ai recommended it and it looks fine to me /shurg
  64. $accounts = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE id IN (SELECT author_id FROM videos WHERE title LIKE :title)", ["title" => "%$query%"]);
  65. if(empty($accounts)) throw new AccountNotFoundException(0, 404);
  66. $data = [];
  67. foreach($videos as $vid) {
  68. $data[] = [
  69. "title" => $vid["title"],
  70. "description" => $vid["description"],
  71. "likes" => $vid["likes"],
  72. "dislikes" => $vid["dislikes"],
  73. "comments" => $vid["comments"],
  74. "shares" => $vid["shares"],
  75. "author" => [
  76. "id" => $vid["author_id"],
  77. "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
  78. "username" => $accounts[$vid["author_id"] - 1]["username"]
  79. ],
  80. ];
  81. }
  82. return api_json($data);
  83. }
  84. public static function RegisterRoutes(): void
  85. {
  86. SimpleRouter::group([
  87. "prefix" => "/video",
  88. ], function () {
  89. SimpleRouter::get("/{id}", [VideoController::class, "getVideo"]);
  90. SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  91. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  92. SimpleRouter::post("/search", [VideoController::class, "search"]);
  93. SimpleRouter::options("/{id}", "CORSHelper");
  94. });
  95. }
  96. }