123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- namespace app\Controllers;
- use app\Exceptions\AccountNotFoundException;
- use app\Exceptions\CommentNotFoundException;
- use app\Exceptions\InvalidRequestException;
- use app\Exceptions\InvalidVideoException;
- use app\Exceptions\UnauthenticatedException;
- use app\Exceptions\VideoNotFoundException;
- use app\Hajeebtok;
- use app\Types\DatabaseObjects\View;
- use Cassandra\Exception\UnauthorizedException;
- use Exception;
- use app\Exceptions\SecurityFaultException;
- use Pecee\SimpleRouter\SimpleRouter;
- use Mimey\MimeTypes;
- use app\Interfaces\IRouteController;
- use app\Types\DatabaseObjects\Video;
- use app\Types\DatabaseObjects\Account;
- use app\Logger;
- use FFMpeg;
- class VideoController implements IRouteController
- {
- public static function getVideo(string $id): string
- {
- $signed_in = signed_in(request());
- if($signed_in) {
- $video_information = new Video($id);
- $video_information->Load();
- $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
- }
- else { // not signed in
- $rand = rand(1, 47);
- $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
- }
- CORSHelper();
- Logger::Debug($video_path);
- if (file_exists($video_path)) {
- $mime_types = new MimeTypes();
- $video_contents = file_get_contents($video_path);
- $video_size = filesize($video_path);
- $mime = $mime_types->getMimeType(pathinfo($video_path, PATHINFO_EXTENSION));
- $response = response();
- $response->header("Content-Type: $mime");
- $response->header("Content-Length: $video_size");
- $response->header("Cache-Control: no-cache");
- return $video_contents;
- } else {
- throw new VideoNotFoundException($id, 404);
- }
- }
- public static function getInfo(string $id): string
- {
- $signed_in = signed_in(request());
- if(!$signed_in) {
- $data = [];
- $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
- $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
- $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
- for($i = 0; $i < rand(1, 13); $i++) {
- $data[] = [
- "id" => rand(0, 203222),
- "title" => $titles[rand(0, count($titles) - 1)],
- "description" => $descriptions[rand(0, count($descriptions) - 1)],
- "likes" => rand(0, 48573),
- "dislikes" => rand(0, 202),
- "comments" => rand(0, 2029),
- "shares" => rand(15, 2321),
- "author" => [
- "id" => rand(2, 2020),
- "username" => $usernames[rand(0, count($usernames) - 1)],
- "verified" => rand(1, 10) > 8
- ],
- ];
- }
- CORSHelper();
- return api_json($data);
- }
- $video_information = new Video($id);
- $video_information->Load();
- $author_information = new Account($video_information->author_id);
- $author_information->Load();
- CORSHelper();
- return api_json([
- "id" => $video_information->id,
- "title" => $video_information->title,
- "description" => $video_information->description,
- "likes" => $video_information->likes,
- "dislikes" => $video_information->dislikes,
- "comments" => $video_information->comments,
- "shares" => $video_information->shares,
- "author" => [
- "id" => $author_information->id,
- "verified" => $author_information->verified,
- "username" => $author_information->username
- ],
- ]);
- }
- public static function search(): string
- {
- $signed_in = signed_in(request());
- if(!$signed_in) {
- $data = [];
- $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
- $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
- $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
- for($i = 0; $i < rand(1, 13); $i++) {
- $data[] = [
- "id" => rand(0, 203222),
- "title" => $titles[rand(0, count($titles) - 1)],
- "description" => $descriptions[rand(0, count($descriptions) - 1)],
- "likes" => rand(0, 48573),
- "dislikes" => rand(0, 202),
- "comments" => rand(0, 2029),
- "shares" => rand(15, 2321),
- "author" => [
- "id" => rand(2, 2020),
- "username" => $usernames[rand(0, count($usernames) - 1)],
- "verified" => rand(1, 10) > 8
- ],
- ];
- }
- CORSHelper();
- return api_json($data);
- }
- $query = input("query");
- $video = new Video(title: $query);
- $videos = $video->LoadMany();
- $data = [];
- foreach ($videos as $vid) {
- $account = new Account(id: $vid["author_id"]);
- $account->Load();
- $data[] = [
- "id" => $vid["id"],
- "title" => $vid["title"],
- "description" => $vid["description"],
- "likes" => $vid["likes"],
- "dislikes" => $vid["dislikes"],
- "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $vid["id"]]),
- "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $vid["id"]]),
- "author" => [
- "id" => $account->id,
- "verified" => $account->verified,
- "username" => $account->username
- ],
- ];
- }
- CORSHelper();
- return api_json($data);
- }
- public static function getThumbnail(string $id): string
- {
- $signed_in = signed_in(request());
- if ($signed_in) { // Signed in
- //$video_information = new Video($id);
- //$video_information->Load();
- $seconds = 2;
- $video_path = APP_ROOT . "/usercontent/videos/$id/video.mp4";
- $frame_path = APP_ROOT . "/usercontent/videos/$id/thumbnail.png";
- } else { // not signed in
- $rand = rand(1, 47);
- $seconds = 6;
- $video_path = APP_ROOT . "/usercontent/fake_videos/$rand/video.mp4";
- $frame_path = APP_ROOT . "/usercontent/fake_videos/$rand/thumbnail.png";
- }
- $mime_types = new MimeTypes();
- if (!file_exists($video_path)) throw new VideoNotFoundException($id, 404);
- if (!file_exists($frame_path)) {
- Logger::Debug("Generating thumbnail for video $id");
- $ffmpeg = FFMpeg\FFMpeg::create();
- $video = $ffmpeg->open($video_path);
- $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds($seconds));
- $frame->save($frame_path);
- }
- CORSHelper();
- $mime = $mime_types->getMimeType(pathinfo($frame_path, PATHINFO_EXTENSION));
- $response = response();
- $response->header("Content-Type: $mime");
- $response->header("Cache-Control: max-age=no-cache");
- return file_get_contents($frame_path);
- }
- public static function getFeed(): string
- {
- $offset = intval(input("offset") ?? "0");
- $limit = intval(Hajeebtok::$Config->GetByDotKey("Service.VideoFeedLimit"));
- $signed_in = signed_in(request());
- if (!$signed_in) {
- $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
- $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
- $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
- $feed = [];
- for($i = 0; $i < $limit; $i++) {
- $feed[] = [
- "id" => rand(1, 10000),
- "title" => $titles[rand(0, count($titles) - 1)],
- "description" => $descriptions[rand(0, count($descriptions) - 1)],
- "likes" => rand(20000, 37020),
- "dislikes" => rand(2, 12343),
- "comments" => rand(2, 1029),
- "shares" => rand(2, 200000),
- "author" => [
- "id" => rand(2, 59),
- "verified" => rand(1, 10) > 8,
- "username" => $usernames[rand(0, count($usernames) - 1)],
- ],
- ];
- }
- CORSHelper();
- return api_json($feed);
- }
- $id = get_token_id(request());
- $feed = [];
- $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]);
- $vid_count = count($videos);
- if ($vid_count < $limit) {
- $fallback_videos = Hajeebtok::$Database->Query("SELECT * FROM videos ORDER BY id DESC LIMIT :remainder OFFSET :offset", ["offset" => $offset + $vid_count, "remainder" => $limit - $vid_count]);
- $videos = array_merge($videos, $fallback_videos);
- }
- foreach ($videos as $video) {
- $account = new Account($video["author_id"]);
- $account->Load();
- $feed[] = [
- "id" => $video["id"],
- "title" => $video["title"],
- "description" => $video["description"],
- "likes" => $video["likes"],
- "dislikes" => $video["dislikes"],
- "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
- "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
- "author" => [
- "id" => $video["author_id"],
- "verified" => $account->verified,
- "username" => $account->username,
- ],
- ];
- }
- CORSHelper();
- return api_json($feed);
- }
- public static function uploadVideo(): string
- {
- if (!signed_in(request())) throw new UnauthenticatedException(0, 401);
- $video_file = input()->file("video");
- $title = input("title");
- $description = input("description");
- $author_id = get_token_id(request());
- if (empty($video_file) || empty($title) || empty($author_id)) throw new InvalidRequestException(0, 400);
- if (!file_exists($video_file->getTmpName()))
- throw new InvalidRequestException(400);
- // save database object
- $video = new Video(title: $title, description: $description, author_id: $author_id);
- $video->Save();
- // Save file
- $video_folder = APP_ROOT . "/usercontent/videos/" . $video->id;
- // check video file type
- $mimey = new MimeTypes();
- $mime_type = $mimey->getMimeType($video_file->getExtension());
- if ($mime_type != "video/mp4" && $mime_type != "video/ogg" && $mime_type != "video/webm" && $mime_type != "video/x-matroska") {
- $video->Delete();
- throw new InvalidVideoException($video->id, 415); // media type unsupported
- }
- // move video
- mkdir($video_folder);
- Logger::Debug($video_file->getFilename() . " -> " . $video_folder . "/video.mp4");
- $video_file->move($video_folder . "/video.mp4");
- CORSHelper();
- return api_json([
- "id" => $video->id,
- "title" => $video->title,
- "description" => $video->description,
- "likes" => $video->likes,
- "dislikes" => $video->dislikes,
- "comments" => 0,
- "shares" => 0,
- "author" => [
- "id" => $author_id,
- ]
- ]);
- }
- public static function getExplore() {
- $video_num = intval(input("video") ?? "0");
- $offset = intval(input("offset") ?? "0");
- $limit = intval(Hajeebtok::$Config->GetByDotKey("Service.VideoFeedLimit"));
- $signed_in = signed_in(request());
- if (!$signed_in) {
- $feed = [];
- for($i = 0; $i < $limit; $i++) {
- $titles = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeTitles"));
- $descriptions = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeDescriptions"));
- $usernames = explode(",", Hajeebtok::$Config->GetByDotKey("Service.FakeUsernames"));
- $feed[] = [
- "id" => rand(1, 100000),
- "title" => $titles[rand(0, count($titles) - 1)],
- "description" => $descriptions[rand(0, count($descriptions) - 1)],
- "likes" => rand(20000, 37020),
- "dislikes" => rand(2, 12343),
- "comments" => rand(2, 1029),
- "shares" => rand(2, 200000),
- "author" => [
- "id" => rand(2, 59),
- "verified" => rand(1, 10) > 8,
- "username" => $usernames[rand(0, count($usernames) - 1)],
- ],
- ];
- }
- CORSHelper();
- return api_json($feed);
- }
- $id = get_token_id(request());
- $videos = Hajeebtok::$Database->Query("SELECT * FROM videos ORDER BY RAND() LIMIT :limit OFFSET :offset", ["offset" => $offset, "limit" => $limit]);
- $feed = [];
- $valid_video = false;
- foreach ($videos as $video) {
- if ($video_num === 0) $valid_video = true;
- if ($video["id"] == $video_num) $valid_video = true;
- Logger::Debug($video["id"] . " -> " . $video["title"]);
- if (!$valid_video) continue;
- $account = new Account($video["author_id"]);
- $account->Load();
- $feed[] = [
- "id" => $video["id"],
- "title" => $video["title"],
- "description" => $video["description"],
- "likes" => $video["likes"],
- "dislikes" => $video["dislikes"],
- "comments" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM comments WHERE video_id = :id", ["id" => $video["id"]]),
- "shares" => Hajeebtok::$Database->Single("SELECT COUNT(*) FROM messages INNER JOIN videos ON messages.video_id = videos.id WHERE videos.id = :id", ["id" => $video["id"]]),
- "author" => [
- "id" => $video["author_id"],
- "verified" => $account->verified,
- "username" => $account->username,
- ],
- ];
- }
- CORSHelper();
- return api_json($feed);
- }
- public static function RegisterRoutes(): void
- {
- SimpleRouter::group([
- "prefix" => "/video",
- ], function () {
- SimpleRouter::get("/{id}/get", [VideoController::class, "getVideo"]);
- SimpleRouter::get("/{id}/info", [VideoController::class, "getInfo"]);
- SimpleRouter::get("/{id}/thumbnail", [VideoController::class, "getThumbnail"]);
- SimpleRouter::get("/feed", [VideoController::class, "getFeed"]);
- SimpleRouter::get("/explore", [VideoController::class, "getExplore"]);
- SimpleRouter::post("/upload", [VideoController::class, "uploadVideo"]);
- SimpleRouter::post("/search", [VideoController::class, "search"]);
- SimpleRouter::options("/{id}/get", "CORSHelper");
- SimpleRouter::options("/{id}/info", "CORSHelper");
- SimpleRouter::options("/{id}/thumbnail", "CORSHelper");
- SimpleRouter::options("/upload", "CORSHelper");
- SimpleRouter::options("/search", "CORSHelper");
- SimpleRouter::options("/feed", "CORSHelper");
- SimpleRouter::options("/explore", "CORSHelper");
- });
- }
- }
|