Resolve the issue of returning a 404 if the file name contains a+instead of a space.

This commit is contained in:
2025-09-11 20:20:45 +08:00
parent 768038479c
commit 84bc9ac583

15
file.go
View File

@@ -148,11 +148,22 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
// Construct the complete file path // Construct the complete file path
fullFilePath := filepath.Join("./files", filePath) fullFilePath := filepath.Join("./files", filePath)
// Try replacing '+' with ' ' and check if the file exists
tempFilePath := strings.ReplaceAll(fullFilePath, "+", " ")
if _, err := os.Stat(tempFilePath); err == nil {
fullFilePath = tempFilePath
}
// Get file content // Get file content
fileContent, err := GetFileContent(fullFilePath) fileContent, err := GetFileContent(fullFilePath)
if err != nil { if err != nil {
NotFoundHandler(w, r) // If file not found, try replacing ' ' with '+' and check again
return tempFilePath = strings.ReplaceAll(fullFilePath, " ", "+")
fileContent, err = GetFileContent(tempFilePath)
if err != nil {
NotFoundHandler(w, r)
return
}
} }
// Set appropriate Content-Type based on file extension // Set appropriate Content-Type based on file extension