Repositories | Polyback | Log in to see private repositories
1,672 bytes | parent directory
<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';
// Which original asset references does this server already have?
// POST "sources" = newline-separated reference keys ("id:12345"). Auth as in upload.php.
// Response: {"success":true,"found":{"id:12345":{"assetId":7,"sha256":"..."}}}
use pizzaboxer\ProjectPolygon\Database;
header("content-type: application/json");
function respond($status, $data)
{
http_response_code($status);
die(json_encode($data));
}
if ($_SERVER["REQUEST_METHOD"] !== "POST") respond(405, ["success" => false, "message" => "Method Not Allowed"]);
$configuredKey = SITE_CONFIG["keys"]["AssetImport"] ?? "";
$givenKey = $_SERVER["HTTP_X_POLYGON_IMPORT_KEY"] ?? "";
if (strlen($configuredKey) < 16) respond(503, ["success" => false, "message" => "Asset import is not enabled on this server"]);
if (!hash_equals($configuredKey, $givenKey)) respond(401, ["success" => false, "message" => "Unauthorized"]);
$sources = array_slice(array_values(array_filter(array_map("trim", explode("\n", $_POST["sources"] ?? "")))), 0, 5000);
$found = new stdClass();
foreach (array_chunk($sources, 500) as $chunk)
{
$params = [];
$holders = [];
foreach ($chunk as $i => $src)
{
$params[":s$i"] = substr($src, 0, 255);
$holders[] = ":s$i";
}
$rows = Database::singleton()->run(
"SELECT i.source, i.assetId, i.sha256 FROM import_assets i JOIN assets a ON a.id = i.assetId WHERE i.source IN (" . implode(",", $holders) . ")",
$params
)->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $r) $found->{$r["source"]} = ["assetId" => (int) $r["assetId"], "sha256" => $r["sha256"]];
}
respond(200, ["success" => true, "found" => $found]);