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

SetOutput 和setfile #926

Open
xwinie opened this issue Dec 3, 2024 · 7 comments
Open

SetOutput 和setfile #926

xwinie opened this issue Dec 3, 2024 · 7 comments

Comments

@xwinie
Copy link

xwinie commented Dec 3, 2024

希望这两个支持目录设置,不需要指定文件名设置

@jeevatkm
Copy link
Member

jeevatkm commented Dec 5, 2024

Translated to English with Google -

From
希望这两个支持目录设置,不需要指定文件名设置

To
Hopefully these two will support directory settings without having to specify a file name

I need help understanding your question/ask. Can you provide additional details?

@RA341
Copy link

RA341 commented Dec 20, 2024

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

@jeevatkm
Copy link
Member

jeevatkm commented Dec 20, 2024

@RA341 Did you mean to get the filename from the content disposition header instead of using SetOutput?

@RA341
Copy link

RA341 commented Dec 20, 2024

yeah

@jeevatkm
Copy link
Member

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.

@jeevatkm jeevatkm self-assigned this Dec 20, 2024
@RA341
Copy link

RA341 commented Dec 20, 2024

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

@jeevatkm
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants