fix download large file for map download

This commit is contained in:
Beu
2025-07-25 21:49:05 +02:00
parent 1fb7c87925
commit ee0b0ac166
6 changed files with 222 additions and 134 deletions

View File

@@ -36,6 +36,7 @@ class AsyncHttpRequest implements UsageInformationAble {
private $contentType = 'text/xml; charset=UTF-8;';
private $timeout = 60;
private $headers = array();
private $handle = null;
public function __construct($maniaControl, $url) {
$this->maniaControl = $maniaControl;
@@ -57,8 +58,7 @@ class AsyncHttpRequest implements UsageInformationAble {
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
->set(CURLOPT_RETURNTRANSFER, true)//
->set(CURLOPT_FOLLOWLOCATION, true)// support redirect
->set(CURLOPT_SSL_VERIFYPEER, false)
->set(CURLOPT_HEADER, true);
->set(CURLOPT_SSL_VERIFYPEER, false);
return $request;
}
@@ -76,8 +76,14 @@ class AsyncHttpRequest implements UsageInformationAble {
array_push($this->headers, 'Accept-Charset: utf-8');
$request = $this->newRequest($this->url, $this->timeout);
$request->getOptions()->set(CURLOPT_AUTOREFERER, true)// accept link reference
->set(CURLOPT_HTTPHEADER, $this->headers); // headers
$request->getOptions()
->set(CURLOPT_AUTOREFERER, true)// accept link reference
->set(CURLOPT_HTTPHEADER, $this->headers); // headers
if ($this->handle !== null) {
$request->getOptions()
->set(CURLOPT_FILE, $this->handle);
}
$this->processRequest($request);
}
@@ -287,4 +293,22 @@ class AsyncHttpRequest implements UsageInformationAble {
public function setTimeout($timeout) {
$this->timeout = $timeout;
}
/**
* Gets the File Resource handle
*
* @return int
*/
public function getHandle() {
return $this->handle;
}
/**
* Sets the File Resource handle
*
* @param int $handle
*/
public function setHandle($handle) {
$this->handle = $handle;
}
}

View File

@@ -629,137 +629,156 @@ class DirectoryBrowser implements ManialinkPageAnswerListener {
if ($url === "") return;
$folderPath = $player->getCache($this, self::CACHE_FOLDER_PATH);
if (filter_var($url, FILTER_VALIDATE_URL)) {
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setCallable(function ($file, $error, $headers) use ($url, $folderPath, $player) {
if (!$file || $error) {
$message = "Impossible to download the file: " . $error;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error during the download of the zip file "'. $url .'": '. $error);
return;
}
$filePath = "";
$finfo = new finfo(FILEINFO_MIME_TYPE);
if ($finfo->buffer($file) === "application/zip") {
$zip = new ZipArchive();
try {
$tempFile = tempnam(sys_get_temp_dir(), 'map_');
$fp = fopen($tempFile, 'w+');
// Create a temporary file
$tempFile = tempnam(sys_get_temp_dir(), 'zip');
file_put_contents($tempFile, $file);
$this->maniaControl->getChat()->sendSuccess('Starting download...', $player);
$open = $zip->open($tempFile);
if ($open === true) {
$zip->extractTo($folderPath);
$zip->close();
$message = "Successfully extracted zip archive from ". $url;
$this->maniaControl->getChat()->sendSuccess($message, $player);
Logger::log(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') downloaded the zip file "'. $url .'"');
} else {
$message = "Cannot extract archive from ". $url;
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, $url);
$asyncHttpRequest->setHandle($fp);
$asyncHttpRequest->setCallable(function ($file, $error, $headers) use ($url, $folderPath, $tempFile, $fp, $player) {
// closing file handle
fclose($fp);
if ($error) {
$message = "Impossible to download the file: " . $error;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloading the zip file "'. $url .'": Cannot extract the archive');
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error during the download of the zip file "'. $url .'": '. $error);
return;
}
// Clean up the temporary file
unlink($tempFile);
} else {
$fileName = "";
$filePath = "";
$contentdispositionheader = "";
foreach ($headers as $key => $value) {
if (strtolower($key) === "content-disposition") {
$contentdispositionheader = urldecode($value);
break;
}
}
if ($contentdispositionheader !== "") {
$value = $contentdispositionheader;
if (strpos($value, ';') !== false) {
list($type, $attr_parts) = explode(';', $value, 2);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$attr_parts = explode(';', $attr_parts);
$attributes = array();
foreach ($attr_parts as $part) {
if (strpos($part, '=') === false) {
continue;
}
list($key, $value) = explode('=', $part, 2);
$attributes[trim($key)] = trim($value);
}
$fileName = null;
if ($finfo->file($tempFile) === "application/zip") {
$zip = new ZipArchive();
if (array_key_exists('filename*', $attributes)) {
$fileName = trim($attributes['filename*']);
$open = $zip->open($tempFile);
// remove prefix if needed
if (strpos(strtolower($fileName), "utf-8''") === 0) {
$fileName = substr($fileName, strlen("utf-8''"));
}
} else if (array_key_exists('filename', $attributes)) {
$fileName = trim($attributes['filename']);
}
if ($fileName !== null) {
if (substr($fileName, 0, 1) === '"' && substr($fileName, -1, 1) === '"') {
$fileName = substr($fileName, 1, -1);
}
$filePath = $folderPath . FileUtil::getClearedFileName($fileName);
}
}
if (!$this->isMapFileName($filePath)) {
$message = "File is not a map: " . $fileName;
if ($open === true) {
$zip->extractTo($folderPath);
$zip->close();
$message = "Successfully extracted zip archive from ". $url;
$this->maniaControl->getChat()->sendSuccess($message, $player);
Logger::log(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') downloaded the zip file "'. $url .'"');
} else {
$message = "Cannot extract archive from ". $url;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloadeding the map file "'. $fileName .'": File is not a map');
return;
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloading the zip file "'. $url .'": Cannot extract the archive');
}
// Clean up the temporary file
unlink($tempFile);
} else {
$path = parse_url($url, PHP_URL_PATH);
// extracted basename
$fileName = basename($path);
if (!$this->isMapFileName($fileName)) {
$fileName .= ".Map.Gbx";
}
$filePath = $folderPath . $fileName;
}
if ($filePath != "") {
if (file_exists($filePath)) {
$index = 1;
while (file_exists(substr($filePath, 0, -8) . "-" . $index . ".Map.Gbx")) {
$index++;
$fileName = "";
$contentdispositionheader = "";
foreach ($headers as $key => $value) {
if (strtolower($key) === "content-disposition") {
$contentdispositionheader = urldecode($value);
break;
}
$filePath = substr($filePath, 0, -8) . "-" . $index . ".Map.Gbx";
}
$bytes = file_put_contents($filePath, $file);
if (!$bytes || $bytes <= 0) {
$message = "Failed to write file " . $filePath;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloadeding the map file "'. $fileName .'": Failed to write the file');
return;
}
if ($contentdispositionheader !== "") {
$value = $contentdispositionheader;
if (strpos($value, ';') !== false) {
list($type, $attr_parts) = explode(';', $value, 2);
$attr_parts = explode(';', $attr_parts);
$attributes = array();
foreach ($attr_parts as $part) {
if (strpos($part, '=') === false) {
continue;
}
list($key, $value) = explode('=', $part, 2);
$attributes[trim($key)] = trim($value);
}
$fileName = null;
$message = "Successfully downloaded the map ". $fileName;
$this->maniaControl->getChat()->sendSuccess($message, $player);
Logger::log(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') downloaded the map file "'. $filePath .'"');
if (array_key_exists('filename*', $attributes)) {
$fileName = trim($attributes['filename*']);
// remove prefix if needed
if (strpos(strtolower($fileName), "utf-8''") === 0) {
$fileName = substr($fileName, strlen("utf-8''"));
}
} else if (array_key_exists('filename', $attributes)) {
$fileName = trim($attributes['filename']);
}
if ($fileName !== null) {
if (substr($fileName, 0, 1) === '"' && substr($fileName, -1, 1) === '"') {
$fileName = substr($fileName, 1, -1);
}
$filePath = $folderPath . FileUtil::getClearedFileName($fileName);
}
}
if (!$this->isMapFileName($filePath)) {
$message = "File is not a map: " . $fileName;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloadeding the map file "'. $fileName .'": File is not a map');
// Clean up the temporary file
unlink($tempFile);
return;
}
} else {
$path = parse_url($url, PHP_URL_PATH);
// extracted basename
$fileName = basename($path);
if (!$this->isMapFileName($fileName)) {
$fileName .= ".Map.Gbx";
}
$filePath = $folderPath . $fileName;
}
if ($filePath != "") {
if (file_exists($filePath)) {
$index = 1;
while (file_exists(substr($filePath, 0, -8) . "-" . $index . ".Map.Gbx")) {
$index++;
}
$filePath = substr($filePath, 0, -8) . "-" . $index . ".Map.Gbx";
}
$renamed = rename($tempFile, $filePath);
if (!$renamed) {
$message = "Failed to write file " . $filePath;
$this->maniaControl->getChat()->sendError($message, $player);
Logger::logError(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') encountered an error when downloadeding the map file "'. $fileName .'": Failed to write the file');
// Clean up the temporary file
unlink($tempFile);
return;
}
$message = "Successfully downloaded the map ". $fileName;
$this->maniaControl->getChat()->sendSuccess($message, $player);
Logger::log(AuthenticationManager::getAuthLevelName($player->authLevel) .' "'. $player->nickname . '" ('. $player->login .') downloaded the map file "'. $filePath .'"');
}
}
}
$this->showManiaLink($player);
});
$this->showManiaLink($player);
});
$asyncHttpRequest->getData();
$asyncHttpRequest->getData();
} catch (\Throwable $th) {
Logger::logError('Error when downloading file: '. $th->getMessage());
$this->maniaControl->getChat()->sendError('Error when downloading file: '. $th->getMessage(), $player);
}
}
}
}

View File

@@ -47,8 +47,7 @@ abstract class WebReader {
->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION)// user-agent
->set(CURLOPT_RETURNTRANSFER, true)// return instead of output content
->set(CURLOPT_AUTOREFERER, true)// follow redirects
->set(CURLOPT_SSL_VERIFYPEER, false)
->set(CURLOPT_HEADER, true);;
->set(CURLOPT_SSL_VERIFYPEER, false);
return $request;
}