diff --git a/trunk/web/admin/problem_import_md.php b/trunk/web/admin/problem_import_md.php
index 9a12f81a0f5..748e852194f 100644
--- a/trunk/web/admin/problem_import_md.php
+++ b/trunk/web/admin/problem_import_md.php
@@ -112,80 +112,125 @@ function import_dir($json) {
return $pid;
}
+function create_upload_dir($base_dir)
+{
+ $date_dir = date("Ymd");
+ $full_path = $base_dir . "/" . $date_dir;
+
+ if (!file_exists($full_path)) {
+ mkdir($full_path, 0755, true);
+ }
+
+ return $full_path;
+}
+
+function process_md_and_images($zip_path, $upload_dir)
+{
+ $zip = new ZipArchive();
+ if ($zip->open($zip_path) === TRUE) {
+ $upload_date_dir = create_upload_dir($upload_dir);
+ $temp_dir = sys_get_temp_dir() . "/md_import_" . uniqid();
+
+ if (!file_exists($temp_dir)) {
+ mkdir($temp_dir, 0755, true);
+ }
+
+ // Extract all files to a temporary directory
+ $zip->extractTo($temp_dir);
+ $zip->close();
+
+ $md_files = glob($temp_dir . "/*.md");
+ $output_files = [];
+
+ foreach ($md_files as $md_file) {
+ $md_content = file_get_contents($md_file);
+
+ // Find and process image paths in the Markdown file
+ $md_content = preg_replace_callback('/!\[.*?\]\((.*?)\)/', function ($matches) use ($temp_dir, $upload_date_dir) {
+ $original_path = $matches[1];
+ $image_path = realpath($temp_dir . "/" . $original_path);
+
+ if (file_exists($image_path)) {
+ // Create a unique name for the uploaded image
+ $image_ext = pathinfo($image_path, PATHINFO_EXTENSION);
+ $new_image_name = date("YmdHis") . "_" . uniqid() . "." . $image_ext;
+ $new_image_path = $upload_date_dir . "/" . $new_image_name;
+
+ if (copy($image_path, $new_image_path)) {
+ // Return the new path in Markdown format
+ return "![{$new_image_name}](/upload/image/" . basename($upload_date_dir) . "/" . $new_image_name . ")";
+ }
+ }
+
+ return $matches[0]; // Return original if unable to process
+ }, $md_content);
+
+ $output_files[] = [
+ 'filename' => basename($md_file),
+ 'content' => $md_content
+ ];
+ }
+
+ // Cleanup the temp directory
+ array_map('unlink', glob("$temp_dir/*.*"));
+ rmdir($temp_dir);
+
+ return $output_files;
+ } else {
+ return [];
+ }
+}
if ($_FILES ["fps"] ["error"] > 0) {
echo " - Error: ".$_FILES ["fps"] ["error"]."File size is too big, change in PHP.ini
";
}
else {
$tempdir = sys_get_temp_dir()."/import_markdown".time();
- mkdir($tempdir);
+ mkdir($tempdir, 0755, true); // 确保目录存在并可写
$tempfile = $_FILES ["fps"] ["tmp_name"];
if (get_extension( $_FILES ["fps"] ["name"])=="zip") {
echo " - zip file, only Markdown .md file is supported