VideoController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if (file_exists($videoPath)) {
  26. $videoContents = file_get_contents($videoPath);
  27. $videoSize = filesize($videoPath);
  28. $mime = $mimeTypes->getMimeType(pathinfo($videoPath, PATHINFO_EXTENSION));
  29. $response = response();
  30. $response->header("Content-Type: $mime");
  31. $response->header("Content-Length: $videoSize");
  32. $response->header("Cache-Control: max-age=86400, public");
  33. return $videoContents;
  34. } else {
  35. throw new VideoNotFoundException($id, 404);
  36. }
  37. }
  38. public static function getInfo(string $id): string
  39. {
  40. $videoInformation = new Video($id);
  41. $videoInformation->Load();
  42. $authorInformation = new Account($videoInformation->author_id);
  43. $authorInformation->Load();
  44. return api_json([
  45. "title" => $videoInformation->title,
  46. "description" => $videoInformation->description,
  47. "likes" => $videoInformation->likes,
  48. "dislikes" => $videoInformation->dislikes,
  49. "comments" => $videoInformation->comments,
  50. "shares" => $videoInformation->shares,
  51. "author" => [
  52. "id" => $authorInformation->id,
  53. "pictureHash" => $authorInformation->picture_hash,
  54. "username" => $authorInformation->username
  55. ],
  56. ]);
  57. }
  58. public static function search(): string {
  59. $query = input("query");
  60. $video = new Video(title: $query);
  61. $videos = $video->LoadMany();
  62. // idk if this is the greatest thing to do but jetbrains phpfart ai recommended it and it looks fine to me /shurg
  63. $accounts = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE id IN (SELECT author_id FROM videos WHERE title LIKE :title)", ["title" => "%$query%"]);
  64. if(empty($accounts)) throw new AccountNotFoundException(0, 404);
  65. $data = [];
  66. foreach($videos as $vid) {
  67. $data[] = [
  68. "title" => $vid["title"],
  69. "description" => $vid["description"],
  70. "likes" => $vid["likes"],
  71. "dislikes" => $vid["dislikes"],
  72. "comments" => $vid["comments"],
  73. "shares" => $vid["shares"],
  74. "author" => [
  75. "id" => $vid["author_id"],
  76. "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
  77. "username" => $accounts[$vid["author_id"] - 1]["username"]
  78. ],
  79. ];
  80. }
  81. return api_json($data);
  82. }
  83. public static function RegisterRoutes(): void
  84. {
  85. SimpleRouter::group([
  86. "prefix" => "/video",
  87. ], function () {
  88. SimpleRouter::get("/{id}", [VideoController::class, "getVideo"]);
  89. //SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  90. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  91. SimpleRouter::post("/search", [VideoController::class, "search"]);
  92. });
  93. }
  94. }