fixed uploading
This commit is contained in:
parent
26cb9e0b8f
commit
7b56601353
45
upload.php
45
upload.php
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
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
|
||||
@ -9,7 +9,7 @@ $target_dir = "./maps/";
|
||||
function generateMapId()
|
||||
{
|
||||
$mapId = "";
|
||||
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$mapId .= $characters[rand(0, strlen($characters) - 1)];
|
||||
}
|
||||
@ -17,16 +17,22 @@ 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));
|
||||
return $fileType == "zip";
|
||||
$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;
|
||||
}
|
||||
|
||||
$mapId = generateMapId(); // the id of the map
|
||||
|
||||
try {
|
||||
$map = $_FILES["map"]; // the file to upload
|
||||
|
||||
if (!isset($map)) { // if the file is not set
|
||||
@ -34,6 +40,7 @@ if (!isset($map)) { // if the file is not set
|
||||
die();
|
||||
}
|
||||
|
||||
$mapId = generateMapId(); // the id of the map
|
||||
$file = $map["tmp_name"]; // the temporary file path
|
||||
$size = $map["size"]; // the size of the file
|
||||
|
||||
@ -42,20 +49,20 @@ if ($size > 100000000) { // if the file is larger than 100MB
|
||||
die();
|
||||
}
|
||||
|
||||
if (!isZipFile($map["name"])) {
|
||||
echo "The file is not a zip file";
|
||||
if (!isValidBeatSaberMap($file)) {
|
||||
echo "The file is not a valid BeatSaber map";
|
||||
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 ($file == "." || $file == "..") { // ignore the . and .. files
|
||||
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 . $file) == $fileHash) {
|
||||
$mapId = pathinfo($file, PATHINFO_FILENAME);
|
||||
if (hash_file("sha256", $target_dir . $scannedFile) == $fileHash) {
|
||||
$mapId = pathinfo($scannedFile, PATHINFO_FILENAME);
|
||||
$exists = true;
|
||||
break;
|
||||
}
|
||||
@ -66,11 +73,17 @@ if ($exists) { // if the file already exists, redirect to the existing file
|
||||
die();
|
||||
}
|
||||
|
||||
$target_file = $target_dir . $mapId . ".zip";
|
||||
$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");
|
||||
echo "There was an error uploading the file";
|
||||
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");
|
||||
} catch (Exception $e) {
|
||||
echo "There was an error uploading the file. Error: " . $e->getMessage();
|
||||
die();
|
||||
}
|
||||
|
Reference in New Issue
Block a user