Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: refine code logic in handleDownloadFile #30422

Merged
merged 3 commits into from
Dec 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions server/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package server

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -69,15 +69,18 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
params := mux.Vars(req)
name := params[pFileName]
path := handler.filePath
if isExists(path) {
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename))
exist, err := isExists(path)
if err != nil {
writeError(w, err)
return
}
if exist {
file, err := os.Open(path)
if err != nil {
writeError(w, err)
return
}
_, err = io.Copy(w, file)
content, err := ioutil.ReadAll(file)
if err != nil {
writeError(w, err)
return
Expand All @@ -92,7 +95,13 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
writeError(w, err)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(content)
if err != nil {
writeError(w, err)
return
}
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename))
return
}
if handler.infoGetter == nil {
Expand Down Expand Up @@ -126,10 +135,7 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
if resp.StatusCode != http.StatusOK {
continue
}
// find dump file in one remote tidb-server, return file directly
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename))
_, err = io.Copy(w, resp.Body)
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
writeError(w, err)
return
Expand All @@ -139,11 +145,18 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req
writeError(w, err)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(content)
if err != nil {
writeError(w, err)
return
}
// find dump file in one remote tidb-server, return file directly
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename))
return
}
// we can't find dump file in any tidb-server, return 404 directly
logutil.BgLogger().Info("can't find dump file in any remote server", zap.String("filename", name))
logutil.BgLogger().Error("can't find dump file in any remote server", zap.String("filename", name))
w.WriteHeader(http.StatusNotFound)
}

Expand All @@ -157,10 +170,13 @@ type downloadFileHandler struct {
downloadedFilename string
}

func isExists(path string) bool {
func isExists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil && !os.IsExist(err) {
return false
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true
return true, nil
}