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

FileFromFS #2112

Merged
merged 4 commits into from
Mar 7, 2020
Merged

FileFromFS #2112

merged 4 commits into from
Mar 7, 2020

Conversation

nikandfor
Copy link
Contributor

I was missing feature of Serving file from http.FileSystem.

Use cases:

  • Serve file by alias without redirection and opening file by hands.
  • Return help page on root of /api/ handlers.
  • Choose FileSystem dynamically.

@codecov
Copy link

codecov bot commented Oct 28, 2019

Codecov Report

Merging #2112 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2112   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files          41       41           
  Lines        2290     2296    +6     
=======================================
+ Hits         2253     2259    +6     
  Misses         21       21           
  Partials       16       16           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ff2b19...009461e. Read the comment docs.

@nikandfor
Copy link
Contributor Author

Hi! I'm just remembering about my PR)

@thinkerou thinkerou added this to the 1.6 milestone Feb 27, 2020
Copy link
Member

@thinkerou thinkerou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thinkerou
Copy link
Member

need @appleboy review the PR, thanks1

@thinkerou thinkerou merged commit 1d055af into gin-gonic:master Mar 7, 2020
pull bot pushed a commit to scope-demo/gin that referenced this pull request Mar 7, 2020
* Context.FileFromFS added

* Context File and FileFromFS examples at README

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
byebyebruce pushed a commit to byebyebruce/gin that referenced this pull request Mar 25, 2020
* Context.FileFromFS added

* Context File and FileFromFS examples at README

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@baurine
Copy link

baurine commented Apr 1, 2020

Hi @nikandfor , can you help add more detail for this part:

	var fs http.FileSystem = // ...
	router.GET("/fs/file", func(c *gin.Context) {
		c.FileFromFS("fs/file.go", fs)
	})

What the exact sample content for // ..., how fs/file.go locate in fs?

I tried many cases, they all don't work for me, my sample code:

	endpoint := r.Group("/diagnose")
	endpoint.StaticFS("/assets", http.Dir("ui/build"))
	var fs http.FileSystem = http.Dir("ui/build")
	endpoint.GET("/reports/detail", func(c *gin.Context) {
		// c.File("ui/build/index.html") // this line code can work well
		c.FileFromFS("ui/build/index.html", fs)
	})

For the first parameter of c.FileFormFS(), I tried ui/build/index.html, ./index.html, index.html, build/index.html, all I failed to get the correct response, but the c.File("ui/build/index.html") can success.

Thank you!

@baurine
Copy link

baurine commented Apr 1, 2020

After diving into the source code, I found we just can't use index.html, it will be redirected, the source code in fs.go#serverFile() method.

// name is '/'-separated, not filepath.Separator.
func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
	const indexPage = "/index.html"

	// redirect .../index.html to .../
	// can't use Redirect() because that would make the path absolute,
	// which would be a problem running under StripPrefix
	if strings.HasSuffix(r.URL.Path, indexPage) {
		localRedirect(w, r, "./")
		return
	}

So I change the index.html name to report.html and works by following code:

	endpoint := r.Group("/diagnose")
	endpoint.StaticFS("/assets", http.Dir("ui/build"))
	var fs http.FileSystem = http.Dir("ui/build")
	endpoint.GET("/reports/detail", func(c *gin.Context) {
		c.FileFromFS("report.html", fs)
	})

@nikandfor
Copy link
Contributor Author

@baurine It's really interesting finding.
Would you find solution at your project code?
How do you think if we should add check at gin.Context.FileFromFS? like

const indexPage = "index.html"

filepath = strings.TrimSuffix(filepath, indexPage)

@baurine
Copy link

baurine commented Apr 2, 2020

hi @nikandfor , I just rename my index.html to report.html to workaround it...

@nikandfor
Copy link
Contributor Author

@thinkerou how do you think, do we need to add some fix to avoid such situations in the future?

@ddvk
Copy link

ddvk commented Sep 9, 2020

this was quite annoying, trying to serve an embedded asset called index.html resulting in too many redirects, until finding out what was causing the problem.

Maybe add a comment or throw an error

@lenghan4real
Copy link

@thinkerou how do you think, do we need to add some fix to avoid such situations in the future?

I think so, rename index.html is quite annoying.

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

Successfully merging this pull request may close these issues.

6 participants