fixed uploading
This commit is contained in:
parent
26cb9e0b8f
commit
7b56601353
77
upload.php
77
upload.php
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
|
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
|
||||||
|
|
||||||
$target_dir = "./maps/";
|
$target_dir = "/home/beatsaber-wip-uploader/maps/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a random map id
|
* Generates a random map id
|
||||||
@ -9,7 +9,7 @@ $target_dir = "./maps/";
|
|||||||
function generateMapId()
|
function generateMapId()
|
||||||
{
|
{
|
||||||
$mapId = "";
|
$mapId = "";
|
||||||
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
for ($i = 0; $i < 4; $i++) {
|
for ($i = 0; $i < 4; $i++) {
|
||||||
$mapId .= $characters[rand(0, strlen($characters) - 1)];
|
$mapId .= $characters[rand(0, strlen($characters) - 1)];
|
||||||
}
|
}
|
||||||
@ -17,60 +17,73 @@ function generateMapId()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the file is a zip file
|
* Checks if the file is a valid beatsaber map
|
||||||
*/
|
*/
|
||||||
function isZipFile($file)
|
function isValidBeatSaberMap($file)
|
||||||
{
|
{
|
||||||
$fileType = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
$zip = new ZipArchive();
|
||||||
return $fileType == "zip";
|
if ($zip->open($file) === true) {
|
||||||
|
if ($zip->locateName("info.dat", ZipArchive::FL_NOCASE) === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$zip->close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mapId = generateMapId(); // the id of the map
|
try {
|
||||||
|
$map = $_FILES["map"]; // the file to upload
|
||||||
|
|
||||||
$map = $_FILES["map"]; // the file to upload
|
if (!isset($map)) { // if the file is not set
|
||||||
|
|
||||||
if (!isset($map)) { // if the file is not set
|
|
||||||
echo "No file was uploaded";
|
echo "No file was uploaded";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = $map["tmp_name"]; // the temporary file path
|
$mapId = generateMapId(); // the id of the map
|
||||||
$size = $map["size"]; // the size of the file
|
$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
|
if ($size > 100000000) { // if the file is larger than 100MB
|
||||||
echo "The file is too large";
|
echo "The file is too large";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isZipFile($map["name"])) {
|
if (!isValidBeatSaberMap($file)) {
|
||||||
echo "The file is not a zip file";
|
echo "The file is not a valid BeatSaber map";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$fileHash = hash_file("sha256", $file); // the hash of the file
|
$fileHash = hash_file("sha256", $file); // the hash of the file
|
||||||
|
|
||||||
$exists = false;
|
$exists = false;
|
||||||
foreach (scandir($target_dir) as $file) { // scan the maps directory for a file with the same hash
|
foreach (scandir($target_dir) as $scannedFile) { // scan the maps directory for a file with the same hash
|
||||||
if ($file == "." || $file == "..") { // ignore the . and .. files
|
if ($scannedFile == "." || $scannedFile == "..") { // ignore the . and .. files
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (hash_file("sha256", $target_dir . $file) == $fileHash) {
|
if (hash_file("sha256", $target_dir . $scannedFile) == $fileHash) {
|
||||||
$mapId = pathinfo($file, PATHINFO_FILENAME);
|
$mapId = pathinfo($scannedFile, PATHINFO_FILENAME);
|
||||||
$exists = true;
|
$exists = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($exists) { // if the file already exists, redirect to the existing file
|
if ($exists) { // if the file already exists, redirect to the existing file
|
||||||
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
|
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$target_file = $target_dir . $mapId . ".zip";
|
$target_file = $target_dir . $mapId . ".zip"; // the output file path
|
||||||
if (!move_uploaded_file($file, $target_file)) {
|
if (!move_uploaded_file($file, $target_file)) {
|
||||||
|
error_log("Error: Failed to move uploaded file from $file to $target_file");
|
||||||
echo "There was an error uploading the file";
|
echo "There was an error uploading the file";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// redirect to /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip
|
error_log("Uploaded file moved from $file to $target_file");
|
||||||
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
|
|
||||||
|
header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo "There was an error uploading the file. Error: " . $e->getMessage();
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user