ServeFileWithRate same as `ServeFile` but it can throttle the speed of reading and though writing the file to the client.
(filename string, limit float64, burst int)
| 5476 | // ServeFileWithRate same as `ServeFile` but it can throttle the speed of reading |
| 5477 | // and though writing the file to the client. |
| 5478 | func (ctx *Context) ServeFileWithRate(filename string, limit float64, burst int) error { |
| 5479 | f, err := os.Open(filename) |
| 5480 | if err != nil { |
| 5481 | ctx.StatusCode(http.StatusNotFound) |
| 5482 | return err |
| 5483 | } |
| 5484 | defer f.Close() |
| 5485 | |
| 5486 | st, err := f.Stat() |
| 5487 | if err != nil { |
| 5488 | code := http.StatusInternalServerError |
| 5489 | if os.IsNotExist(err) { |
| 5490 | code = http.StatusNotFound |
| 5491 | } |
| 5492 | |
| 5493 | if os.IsPermission(err) { |
| 5494 | code = http.StatusForbidden |
| 5495 | } |
| 5496 | |
| 5497 | ctx.StatusCode(code) |
| 5498 | return err |
| 5499 | } |
| 5500 | |
| 5501 | if st.IsDir() { |
| 5502 | return ctx.ServeFile(path.Join(filename, "index.html")) |
| 5503 | } |
| 5504 | |
| 5505 | ctx.ServeContentWithRate(f, st.Name(), st.ModTime(), limit, burst) |
| 5506 | return nil |
| 5507 | } |
| 5508 | |
| 5509 | // SendFile sends a file as an attachment, that is downloaded and saved locally from client. |
| 5510 | // Note that compression can be registered |
no test coverage detected