File sends a response with the content of the file if file not exists, response 404 for issue #39
(file string)
| 420 | // if file not exists, response 404 |
| 421 | // for issue #39 |
| 422 | func (ctx *HttpContext) File(file string) (err error) { |
| 423 | f, err := os.Open(file) |
| 424 | if err != nil { |
| 425 | HTTPNotFound(ctx) |
| 426 | return nil |
| 427 | } |
| 428 | defer f.Close() |
| 429 | |
| 430 | fi, _ := f.Stat() |
| 431 | if fi.IsDir() { |
| 432 | file = filepath.Join(file, ctx.HttpServer().IndexPage()) |
| 433 | f, err = os.Open(file) |
| 434 | if err != nil { |
| 435 | HTTPNotFound(ctx) |
| 436 | return nil |
| 437 | } |
| 438 | defer f.Close() |
| 439 | if fi, err = f.Stat(); err != nil { |
| 440 | return err |
| 441 | } |
| 442 | } |
| 443 | http.ServeContent(ctx.Response().Writer(), ctx.Request().Request, fi.Name(), fi.ModTime(), f) |
| 444 | return nil |
| 445 | } |
| 446 | |
| 447 | // Attachment sends a response as attachment, prompting client to save the file. |
| 448 | // for issue #39 |
no test coverage detected