This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-wip-uploader/upload.php

68 lines
1.7 KiB
PHP

<?php
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
$target_dir = "./maps/";
/**
* Generates a random map id
*/
function generateMapId()
{
$mapId = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i = 0; $i < 4; $i++) {
$mapId .= $characters[rand(0, strlen($characters) - 1)];
}
return $mapId;
}
/**
* Checks if the file is a zip file
*/
function isZipFile($file)
{
$fileType = strtolower(pathinfo($file, PATHINFO_EXTENSION));
return $fileType == "zip";
}
$mapId = generateMapId(); // the id of the map
$map = $_FILES["map"]; // the file to upload
$file = $map["tmp_name"]; // the temporary file path
$size = $map["size"]; // the size of the file
if ($size > 100000000) { // if the file is larger than 100MB
echo "The file is too large";
die();
}
if (!isZipFile($map["name"])) {
echo "The file is not a zip file";
die();
}
$fileHash = hash_file("sha256", $file); // the hash of the file
$exists = false;
foreach (scandir($target_dir) as $file) { // scan the maps directory for a file with the same hash
if (hash_file("sha256", $target_dir . $file) == $fileHash) {
$mapId = pathinfo($file, PATHINFO_FILENAME);
$exists = true;
break;
}
}
if ($exists) { // if the file already exists, redirect to the existing file
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
die();
}
$target_file = $target_dir . $mapId . ".zip";
if (!move_uploaded_file($file, $target_file)) {
echo "There was an error uploading the file";
die();
}
// redirect to /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");