123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\Controllers;
- use app\Exceptions\AccountNotFoundException;
- use app\Exceptions\VideoNotFoundException;
- use app\Hajeebtok;
- use Pecee\SimpleRouter\SimpleRouter;
- use Mimey\MimeTypes;
- use app\Interfaces\IRouteController;
- use app\Types\DatabaseObjects\Video;
- use app\Types\DatabaseObjects\Account;
- use app\Logger;
- class VideoController implements IRouteController
- {
- public static function getVideo(string $id): string
- {
- $signedIn = signed_in(request());
- $videoInformation = new Video($id);
- $videoInformation->Load();
- $mimeTypes = new MimeTypes();
- if($signedIn) { // Signed in
- $videoPath = APP_ROOT . "/usercontent/videos/$id/video.mp4";
- } else { // not signed in
- $videoPath = APP_ROOT . "/usercontent/videos/1/video.mp4";
- }
- CORSHelper();
- if (file_exists($videoPath)) {
- $videoContents = file_get_contents($videoPath);
- $videoSize = filesize($videoPath);
- $mime = $mimeTypes->getMimeType(pathinfo($videoPath, PATHINFO_EXTENSION));
- $response = response();
- $response->header("Content-Type: $mime");
- $response->header("Content-Length: $videoSize");
- $response->header("Cache-Control: max-age=86400, public");
- return $videoContents;
- } else {
- throw new VideoNotFoundException($id, 404);
- }
- }
- public static function getInfo(string $id): string
- {
- $videoInformation = new Video($id);
- $videoInformation->Load();
- $authorInformation = new Account($videoInformation->author_id);
- $authorInformation->Load();
- return api_json([
- "title" => $videoInformation->title,
- "description" => $videoInformation->description,
- "likes" => $videoInformation->likes,
- "dislikes" => $videoInformation->dislikes,
- "comments" => $videoInformation->comments,
- "shares" => $videoInformation->shares,
- "author" => [
- "id" => $authorInformation->id,
- "pictureHash" => $authorInformation->picture_hash,
- "username" => $authorInformation->username
- ],
- ]);
- }
- public static function search(): string {
- $query = input("query");
- $video = new Video(title: $query);
- $videos = $video->LoadMany();
- // idk if this is the greatest thing to do but jetbrains phpfart ai recommended it and it looks fine to me /shurg
- $accounts = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE id IN (SELECT author_id FROM videos WHERE title LIKE :title)", ["title" => "%$query%"]);
- if(empty($accounts)) throw new AccountNotFoundException(0, 404);
- $data = [];
- foreach($videos as $vid) {
- $data[] = [
- "title" => $vid["title"],
- "description" => $vid["description"],
- "likes" => $vid["likes"],
- "dislikes" => $vid["dislikes"],
- "comments" => $vid["comments"],
- "shares" => $vid["shares"],
- "author" => [
- "id" => $vid["author_id"],
- "pictureHash" => $accounts[$vid["author_id"] - 1]["picture_hash"], // kinda scuffed fix
- "username" => $accounts[$vid["author_id"] - 1]["username"]
- ],
- ];
- }
- return api_json($data);
- }
- public static function RegisterRoutes(): void
- {
- SimpleRouter::group([
- "prefix" => "/video",
- ], function () {
- SimpleRouter::get("/{id}", [VideoController::class, "getVideo"]);
- SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
- SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
- SimpleRouter::post("/search", [VideoController::class, "search"]);
- SimpleRouter::options("/{id}", "CORSHelper");
- });
- }
- }
|