Repositories | Polyback | Log in to see private repositories


polyback-website / fe552ae720b75aa69ca0c73f420ab929961b458d / api/studio/publish.php

3,440 bytes | parent directory

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

// Publish a new version of a place's file from Studio. The file is sent in chunks (each
// request stays under the site's body size limit):
//   POST /api/studio/publish?id=<place>&upload=<32 hex>&offset=<bytes so far>[&final=1]
//   body: raw bytes (application/octet-stream)
// -> {"success", "received"} for a chunk, {"success", "version", "size"} after the final one.
// Game servers get the new file at their next start (they fetch /polyasset/?id=<place>).
use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Gzip;
use pizzaboxer\ProjectPolygon\Polygon;
use pizzaboxer\ProjectPolygon\Studio;

API::initialize(["method" => "POST"]);
$user = Studio::RequireUser();
$place = Studio::RequirePlace($user, $_GET["id"] ?? 0);
$upload = (string) ($_GET["upload"] ?? "");
$offset = (int) ($_GET["offset"] ?? -1);
$final = isset($_GET["final"]);
if (!preg_match('/^[0-9a-f]{32}$/', $upload)) API::respond(400, false, "Bad upload id");

$dir = Studio::UploadDirectory();
// stale uploads (a day old) are dropped
foreach (glob("$dir/*.part") ?: [] as $old) if (filemtime($old) < time() - 86400) @unlink($old);
$part = "$dir/{$user->id}-{$place->id}-{$upload}.part";

$chunk = file_get_contents("php://input");
if ($chunk === false) API::respond(400, false, "No data");
if (strlen($chunk) > Studio::CHUNK_BYTES) API::respond(413, false, "Chunk too large");
$have = file_exists($part) ? filesize($part) : 0;
if ($offset !== $have) API::respond(409, false, "Upload out of order (expected offset $have)");
if ($have + strlen($chunk) > Studio::MAX_PLACE_BYTES) { @unlink($part); API::respond(413, false, "Places can be at most " . (Studio::MAX_PLACE_BYTES >> 20) . " MB"); }
if (file_put_contents($part, $chunk, FILE_APPEND | LOCK_EX) === false) API::respond(500, false, "Couldn't store the upload");
clearstatcache();
if (!$final) API::respondCustom(["status" => 200, "success" => true, "received" => filesize($part)]);

// finished: check it's a place file, then make it the place's file
$fh = fopen($part, "rb");
$head = fread($fh, 16);
fclose($fh);
$native = substr($head, 0, 8) === "PGPLACE\0";
if (!$native && stripos($head, "<roblox") !== 0) { @unlink($part); API::respond(400, false, "That isn't a place file"); }
$size = filesize($part);
$sha = hash_file("sha256", $part);

$version = 1 + (int) Database::singleton()->run("SELECT COALESCE(MAX(Version), 0) FROM StudioPlaceVersions WHERE PlaceID = :p", [":p" => $place->id])->fetchColumn();
// keep every published version
$versions = Polygon::GetSharedResource("assets/place-versions");
if (!is_dir($versions)) mkdir($versions, 0770, true);
$keep = "$versions/{$place->id}-{$version}";
copy($part, $keep);
Gzip::Compress($keep);

$location = Polygon::GetSharedResource("assets/{$place->id}");
rename($part, $location);
Gzip::Compress($location);

Database::singleton()->run(
	"INSERT INTO StudioPlaceVersions (PlaceID, Version, UserID, Created, Size, Sha256) VALUES (:p, :v, :u, UNIX_TIMESTAMP(), :s, :h)",
	[":p" => $place->id, ":v" => $version, ":u" => $user->id, ":s" => $size, ":h" => $sha]
);
Database::singleton()->run("UPDATE assets SET updated = UNIX_TIMESTAMP() WHERE id = :id", [":id" => $place->id]);
Studio::LogStaff($user, $place, "Published a new version of");
API::respondCustom(["status" => 200, "success" => true, "version" => $version, "size" => $size]);