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

88 lines
2.3 KiB
PHP
Raw Normal View History

2023-04-15 13:13:25 +00:00
<?php
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
2023-04-15 18:44:02 +00:00
$target_dir = "/home/beatsaber-wip-uploader/maps/";
2023-04-15 13:13:25 +00:00
/**
* Generates a random map id
*/
function generateMapId()
{
$mapId = "";
2023-04-15 18:44:02 +00:00
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
2023-04-15 17:11:15 +00:00
for ($i = 0; $i < 4; $i++) {
2023-04-15 13:13:25 +00:00
$mapId .= $characters[rand(0, strlen($characters) - 1)];
}
return $mapId;
}
/**
2023-04-15 18:44:02 +00:00
* Checks if the file is a valid beatsaber map
2023-04-15 13:13:25 +00:00
*/
2023-04-15 18:44:02 +00:00
function isValidBeatSaberMap($file)
2023-04-15 13:13:25 +00:00
{
2023-04-15 18:44:02 +00:00
$zip = new ZipArchive();
if ($zip->open($file) === true) {
if ($zip->locateName("info.dat", ZipArchive::FL_NOCASE) === false) {
return false;
}
$zip->close();
return true;
}
return false;
2023-04-15 13:13:25 +00:00
}
2023-04-15 18:44:02 +00:00
try {
$map = $_FILES["map"]; // the file to upload
2023-04-15 13:13:25 +00:00
2023-04-15 18:44:02 +00:00
if (!isset($map)) { // if the file is not set
2023-04-15 18:45:50 +00:00
header("Location: /?map=No file was uploaded");
2023-04-15 18:44:02 +00:00
die();
}
2023-04-15 18:00:51 +00:00
2023-04-15 18:44:02 +00:00
$mapId = generateMapId(); // the id of the map
$file = $map["tmp_name"]; // the temporary file path
$size = $map["size"]; // the size of the file
2023-04-15 18:00:51 +00:00
2023-04-15 18:44:02 +00:00
if ($size > 100000000) { // if the file is larger than 100MB
2023-04-15 18:45:50 +00:00
header("Location: /?map=File is too large");
2023-04-15 18:44:02 +00:00
die();
}
2023-04-15 17:41:20 +00:00
2023-04-15 18:44:02 +00:00
if (!isValidBeatSaberMap($file)) {
2023-04-15 18:45:50 +00:00
header("Location: /?map=This is not a valid BeatSaber map");
2023-04-15 18:44:02 +00:00
die();
}
2023-04-15 13:13:25 +00:00
2023-04-15 18:44:02 +00:00
$fileHash = hash_file("sha256", $file); // the hash of the file
2023-04-15 13:13:25 +00:00
2023-04-15 18:44:02 +00:00
$exists = false;
foreach (scandir($target_dir) as $scannedFile) { // scan the maps directory for a file with the same hash
if ($scannedFile == "." || $scannedFile == "..") { // ignore the . and .. files
continue;
}
if (hash_file("sha256", $target_dir . $scannedFile) == $fileHash) {
$mapId = pathinfo($scannedFile, PATHINFO_FILENAME);
$exists = true;
break;
}
}
2023-04-15 18:44:02 +00:00
if ($exists) { // if the file already exists, redirect to the existing file
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
die();
2023-04-15 18:04:15 +00:00
}
2023-04-15 18:44:02 +00:00
$target_file = $target_dir . $mapId . ".zip"; // the output file path
if (!move_uploaded_file($file, $target_file)) {
error_log("Error: Failed to move uploaded file from $file to $target_file");
2023-04-15 18:45:50 +00:00
header("Location: /?map=Failed to upload file");
2023-04-15 18:44:02 +00:00
die();
}
2023-04-15 18:44:02 +00:00
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
} catch (Exception $e) {
echo "There was an error uploading the file. Error: " . $e->getMessage();
2023-04-15 13:25:54 +00:00
die();
2023-04-15 13:13:25 +00:00
}