VideoController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\Controllers;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\CommentNotFoundException;
  5. use app\Exceptions\InvalidRequestException;
  6. use app\Exceptions\InvalidVideoException;
  7. use app\Exceptions\UnauthenticatedException;
  8. use app\Exceptions\VideoNotFoundException;
  9. use app\Hajeebtok;
  10. use app\Types\DatabaseObjects\View;
  11. use Cassandra\Exception\UnauthorizedException;
  12. use Exception;
  13. use app\Exceptions\SecurityFaultException;
  14. use Pecee\SimpleRouter\SimpleRouter;
  15. use Mimey\MimeTypes;
  16. use app\Interfaces\IRouteController;
  17. use app\Types\DatabaseObjects\Video;
  18. use app\Types\DatabaseObjects\Account;
  19. use app\Logger;
  20. use FFMpeg;
  21. class VideoController implements IRouteController
  22. {
  23. public static function getVideo(string $id): string
  24. {
  25. $signed_in = signed_in(request());
  26. if($signed_in) {
  27. $video_information = new Video($id);
  28. $video_information->Load();
  29. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  30. }
  31. else { // not signed in
  32. $rand = rand(1, 47);
  33. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  34. }
  35. CORSHelper();
  36. Logger::Debug($video_path);
  37. if (file_exists($video_path)) {
  38. $mime_types = new MimeTypes();
  39. $video_contents = file_get_contents($video_path);
  40. $video_size = filesize($video_path);
  41. $mime = $mime_types->getMimeType(pathinfo($video_path, PATHINFO_EXTENSION));
  42. $response = response();
  43. $response->header("Content-Type: $mime");
  44. $response->header("Content-Length: $video_size");
  45. $response->header("Cache-Control: no-cache");
  46. return $video_contents;
  47. } else {
  48. throw new VideoNotFoundException($id, 404);
  49. }
  50. }
  51. public static function getInfo(string $id): string
  52. {
  53. $video_information = new Video($id);
  54. $video_information->Load();
  55. $author_information = new Account($video_information->author_id);
  56. $author_information->Load();
  57. CORSHelper();
  58. return api_json([
  59. "id" => $video_information->id,
  60. "title" => $video_information->title,
  61. "description" => $video_information->description,
  62. "likes" => $video_information->likes,
  63. "dislikes" => $video_information->dislikes,
  64. "comments" => $video_information->comments,
  65. "shares" => $video_information->shares,
  66. "author" => [
  67. "id" => $author_information->id,
  68. "pictureHash" => $author_information->picture_hash,
  69. "username" => $author_information->username
  70. ],
  71. ]);
  72. }
  73. public static function search(): string
  74. {
  75. $query = input("query");
  76. $video = new Video(title: $query);
  77. $videos = $video->LoadMany();
  78. // idk if this is the greatest thing to do but jetbrains phpfart ai recommended it and it looks fine to me /shurg
  79. $accounts = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE id IN (SELECT author_id FROM videos WHERE title LIKE :title)", ["title" => "%$query%"]);
  80. if (empty($accounts)) throw new AccountNotFoundException(0, 404);
  81. $data = [];
  82. foreach ($videos as $vid) {
  83. $data[] = [
  84. "id" => $vid["id"],
  85. "title" => $vid["title"],
  86. "description" => $vid["description"],
  87. "likes" => $vid["likes"],
  88. "dislikes" => $vid["dislikes"],
  89. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $vid["id"]]),
  90. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $vid["id"]]),
  91. "author" => [
  92. "id" => $vid["author_id"],
  93. "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
  94. "username" => $accounts[$vid["author_id"] - 1]["username"]
  95. ],
  96. ];
  97. }
  98. CORSHelper();
  99. return api_json($data);
  100. }
  101. public static function getThumbnail(string $id): string
  102. {
  103. $signed_in = signed_in(request());
  104. if ($signed_in) { // Signed in
  105. //$video_information = new Video($id);
  106. //$video_information->Load();
  107. $seconds = 2;
  108. $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
  109. $frame_path = APP_ROOT . "/usercontent/videos/$id/thumbnail.png";
  110. } else { // not signed in
  111. $rand = rand(1, 47);
  112. $seconds = 6;
  113. $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
  114. $frame_path = APP_ROOT . "/usercontent/fake_videos/$rand/thumbnail.png";
  115. }
  116. $mime_types = new MimeTypes();
  117. if (!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
  118. if (!file_exists($frame_path)) {
  119. Logger::Debug("Generating thumbnail for video $id");
  120. $ffmpeg = FFMpeg\FFMpeg::create();
  121. $video = $ffmpeg->open($video_path);
  122. $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
  123. $frame->save($frame_path);
  124. }
  125. CORSHelper();
  126. $mime = $mime_types->getMimeType(pathinfo($frame_path, PATHINFO_EXTENSION));
  127. $response = response();
  128. $response->header("Content-Type: $mime");
  129. $response->header("Cache-Control: max-age=no-cache");
  130. return file_get_contents($frame_path);
  131. }
  132. public static function getFeed(): string
  133. {
  134. $offset = intval(input("offset") ?? "0");
  135. $limit = intval(Hajeebtok::$Config->GetByDotKey("Service.VideoFeedLimit"));
  136. $signed_in = signed_in(request());
  137. if (!$signed_in) {
  138. $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
  139. $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
  140. $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
  141. $feed = [];
  142. for($i = 0; $i < $limit; $i++) {
  143. $feed[] = [
  144. "id" => rand(1, 10),
  145. "title" => $titles[rand(0, count($titles) - 1)],
  146. "description" => $descriptions[rand(0, count($descriptions) - 1)],
  147. "likes" => rand(20000, 37020),
  148. "dislikes" => rand(2, 12343),
  149. "comments" => rand(2, 1029),
  150. "shares" => rand(2, 200000),
  151. "author" => [
  152. "id" => rand(2, 59),
  153. "pictureHash" => null,
  154. "username" => $usernames[rand(0, count($usernames) - 1)],
  155. ],
  156. ];
  157. }
  158. CORSHelper();
  159. return api_json($feed);
  160. }
  161. $id = get_token_id(request());
  162. $feed = [];
  163. $videos = Hajeebtok::$Database->Query("SELECT * FROM videos WHERE id NOT IN (SELECT video_id FROM views WHERE account_id = :account_id) ORDER BY id DESC LIMIT :limit OFFSET :offset", ["account_id" => $id, "offset" => $offset, "limit" => $limit]);
  164. $vid_count = count($videos);
  165. if ($vid_count < $limit) {
  166. $fallback_videos = Hajeebtok::$Database->Query("SELECT * FROM videos ORDER BY id DESC LIMIT :remainder OFFSET :offset", ["offset" => $offset + $vid_count, "remainder" => $limit - $vid_count]);
  167. $videos = array_merge($videos, $fallback_videos);
  168. }
  169. foreach ($videos as $video) {
  170. $account = new Account($video["author_id"]);
  171. $account->Load();
  172. $feed[] = [
  173. "id" => $video["id"],
  174. "title" => $video["title"],
  175. "description" => $video["description"],
  176. "likes" => $video["likes"],
  177. "dislikes" => $video["dislikes"],
  178. "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
  179. "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
  180. "author" => [
  181. "id" => $video["author_id"],
  182. "pictureHash" => $account->picture_hash,
  183. "username" => $account->username,
  184. ],
  185. ];
  186. }
  187. CORSHelper();
  188. return api_json($feed);
  189. }
  190. public static function uploadVideo(): string
  191. {
  192. if (!signed_in(request())) throw new UnauthenticatedException(0, 401);
  193. $video_file = input()->file("video");
  194. $title = input("title");
  195. $description = input("description");
  196. $author_id = get_token_id(request());
  197. if (empty($video_file) || empty($title) || empty($author_id)) throw new InvalidRequestException(0, 400);
  198. if (!file_exists($video_file->getTmpName()))
  199. throw new InvalidRequestException(400);
  200. // save database object
  201. $video = new Video(title: $title, description: $description, author_id: $author_id);
  202. $video->Save();
  203. // Save file
  204. $video_folder = APP_ROOT . "/usercontent/videos/" . $video->id;
  205. // check video file type
  206. $mimey = new MimeTypes();
  207. $mime_type = $mimey->getMimeType($video_file->getExtension());
  208. if ($mime_type != "video/mp4" && $mime_type != "video/ogg" && $mime_type != "video/webm" && $mime_type != "video/x-matroska") {
  209. $video->Delete();
  210. throw new InvalidVideoException($video->id, 415); // media type unsupported
  211. }
  212. // move video
  213. mkdir($video_folder);
  214. Logger::Debug($video_file->getFilename() . " -> " . $video_folder . "/video.mp4");
  215. $video_file->move($video_folder . "/video.mp4");
  216. CORSHelper();
  217. return api_json([
  218. "id" => $video->id,
  219. "title" => $video->title,
  220. "description" => $video->description,
  221. "likes" => $video->likes,
  222. "dislikes" => $video->dislikes,
  223. "comments" => 0,
  224. "shares" => 0,
  225. "author" => [
  226. "id" => $author_id,
  227. ]
  228. ]);
  229. }
  230. public static function RegisterRoutes(): void
  231. {
  232. SimpleRouter::group([
  233. "prefix" => "/video",
  234. ], function () {
  235. SimpleRouter::get("/{id}/get", [VideoController::class, "getVideo"]);
  236. SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
  237. SimpleRouter::get("/{id}/thumbnail", [VideoController::class, "getThumbnail"]);
  238. SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
  239. SimpleRouter::post("/search", [VideoController::class, "search"]);
  240. SimpleRouter::get("/feed", [VideoController::class, "getFeed"]);
  241. SimpleRouter::options("/{id}/get", "CORSHelper");
  242. SimpleRouter::options("/{id}/info", "CORSHelper");
  243. SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
  244. SimpleRouter::options("/upload", "CORSHelper");
  245. SimpleRouter::options("/search", "CORSHelper");
  246. SimpleRouter::options("/feed", "CORSHelper");
  247. });
  248. }
  249. }