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

11
file.go
View File

@@ -148,12 +148,23 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
// Construct the complete file path
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
fileContent, err := GetFileContent(fullFilePath)
if err != nil {
// If file not found, try replacing ' ' with '+' and check again
tempFilePath = strings.ReplaceAll(fullFilePath, " ", "+")
fileContent, err = GetFileContent(tempFilePath)
if err != nil {
NotFoundHandler(w, r)
return
}
}
// Set appropriate Content-Type based on file extension
ext := filepath.Ext(filePath)