-
Notifications
You must be signed in to change notification settings - Fork 718
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
SetOutput 和setfile #926
Comments
Translated to English with Google -
I need help understanding your question/ask. Can you provide additional details? |
I think he means using the content dispostiton headers, so that we don't have to specify a file name manually. I need something like this |
@RA341 Did you mean to get the filename from the content disposition header instead of using SetOutput? |
yeah |
I see. Currently, the SetOutput method does two things: it gets the filename input from the user and instructs the Resty to save the file. Let me figure it out for the upcoming Resty v3. |
this is the snippet I use currently resp, err := http.Get(downloadLink)
if err != nil {
return "", fmt.Errorf("failed to make request: %v", err)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("invalid http code: %d, reason: %s", resp.StatusCode, resp.Status)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal().Err(err).Msgf("failed to close response body")
}
}(resp.Body)
// Get filename from Content-Disposition header
_, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
filename := ""
if err == nil && params["filename"] != "" {
filename = params["filename"]
} else {
// Fallback to URL path if header not available
filename = path.Base(downloadLink)
}
if err := os.MkdirAll(downloadPath, 0775); err != nil {
return "", fmt.Errorf("failed to create directories: %v", err)
}
destPath := fmt.Sprintf("%s/%s", downloadPath, filename)
file, err := os.Create(destPath)
if err != nil {
return "", fmt.Errorf("failed to create file: %v", err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatal().Err(err).Msgf("failed to close response body")
}
}(file)
_, err = io.Copy(file, resp.Body)
if err != nil {
return "", fmt.Errorf("failed to copy response body: %v", err)
} no sure how much of its applicable |
@RA341 Thanks for sharing. This part is applicable, the remaining lines are already present in the middleware. // Get filename from Content-Disposition header
_, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
filename := ""
if err == nil && params["filename"] != "" {
filename = params["filename"]
} else {
// Fallback to URL path if header not available
filename = path.Base(downloadLink)
} It needs to be blended with Resty Flow. Plus, a new method needs to be introduced beside the SetOutput. |
希望这两个支持目录设置,不需要指定文件名设置
The text was updated successfully, but these errors were encountered: