Repositories | Polyback | Log in to see private repositories


polyback-website / 32de824ed1c6d1185126d78087d2c781572f8c28 / api/studio/assets.php

3,227 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// Studio's toolbox: browse and search assets.
//   GET source=roblox|polyback  type=model|decal|mesh|audio|hat|gear|place|animation|all
//       q=<search>  page=<1..>  limit=<1..60>
// -> {"success", "items":[{"id","name","type","url","thumbnail","creator"}], "page", "more"}
// url is what a place references: rbxassetid://<id> (the Roblox archive) or
// polyasset://<id> (Polyback's own assets).
use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Studio;
use pizzaboxer\ProjectPolygon\Thumbnails;

API::initialize(["method" => "GET"]);
$user = Studio::RequireUser();

$TYPES = [
	"model" => [10], "decal" => [13, 1], "mesh" => [4], "audio" => [3], "hat" => [8],
	"gear" => [19], "place" => [9], "animation" => [24], "all" => [1, 3, 4, 8, 9, 10, 13, 19, 24],
];
$TYPE_NAMES = [1 => "Image", 3 => "Audio", 4 => "Mesh", 8 => "Hat", 9 => "Place", 10 => "Model", 13 => "Decal", 19 => "Gear", 24 => "Animation"];

$source = ($_GET["source"] ?? "roblox") === "polyback" ? "polyback" : "roblox";
$types = $TYPES[$_GET["type"] ?? "model"] ?? $TYPES["model"];
$q = trim(substr((string) ($_GET["q"] ?? ""), 0, 100));
$page = max(1, (int) ($_GET["page"] ?? 1));
$limit = min(60, max(1, (int) ($_GET["limit"] ?? 30)));
$offset = ($page - 1) * $limit;
$in = implode(",", array_map("intval", $types));
$args = [];
$where = "";
if ($q !== "")
{
	$where = " AND name LIKE :q";
	$args[":q"] = "%" . addcslashes($q, "%_\\") . "%";
}

$items = [];
if ($source === "roblox")
{
	$rows = Database::singleton()->run(
		"SELECT id, name, asset_type_id, roblox_creator_name FROM roblox_assets
		 WHERE asset_type_id IN ($in) AND sha256 IS NOT NULL $where
		 ORDER BY (thumbnail_sha256 IS NOT NULL) DESC, (name IS NOT NULL) DESC, id DESC LIMIT " . ($limit + 1) . " OFFSET $offset", $args
	)->fetchAll(PDO::FETCH_OBJ);
	foreach ($rows as $r)
	{
		$items[] = [
			"id" => (int) $r->id, "name" => (string) ($r->name ?? ($TYPE_NAMES[(int) $r->asset_type_id] ?? "Asset") . " {$r->id}"),
			"type" => $TYPE_NAMES[(int) $r->asset_type_id] ?? "Asset", "typeId" => (int) $r->asset_type_id,
			"url" => "rbxassetid://{$r->id}",
			"thumbnail" => "/Thumbs/Asset.ashx?roblox=1&id={$r->id}&x=150&y=150",
			"creator" => (string) ($r->roblox_creator_name ?? ""),
		];
	}
}
else
{
	$args[":u"] = $user->id;
	$rows = Database::singleton()->run(
		"SELECT assets.*, users.username FROM assets JOIN users ON users.id = assets.creator
		 WHERE assets.type IN ($in) AND assets.approved = 1 AND (assets.publicDomain = 1 OR assets.creator = :u) $where
		 ORDER BY assets.updated DESC LIMIT " . ($limit + 1) . " OFFSET $offset", $args
	)->fetchAll(PDO::FETCH_OBJ);
	foreach ($rows as $r)
	{
		$items[] = [
			"id" => (int) $r->id, "name" => (string) $r->name,
			"type" => $TYPE_NAMES[(int) $r->type] ?? "Asset", "typeId" => (int) $r->type,
			"url" => "polyasset://{$r->id}",
			"thumbnail" => Thumbnails::GetAsset($r, 420, 420),
			"creator" => (string) $r->username,
		];
	}
}
$more = count($items) > $limit;
if ($more) array_pop($items);
API::respondCustom(["status" => 200, "success" => true, "items" => $items, "page" => $page, "more" => $more]);