Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
package router
|
2017-01-02 20:20:17 +01:00
|
|
|
|
|
|
|
import (
|
2019-06-21 18:43:25 +02:00
|
|
|
"bytes"
|
2017-03-13 00:40:57 +01:00
|
|
|
"fmt"
|
2020-07-05 22:27:32 +02:00
|
|
|
"html"
|
|
|
|
"html/template"
|
2017-03-13 00:40:57 +01:00
|
|
|
"io"
|
2017-01-02 20:20:17 +01:00
|
|
|
"net/http"
|
2017-03-13 00:40:57 +01:00
|
|
|
"net/url"
|
2017-01-02 20:20:17 +01:00
|
|
|
"os"
|
2017-03-13 00:40:57 +01:00
|
|
|
"path"
|
2017-02-16 21:19:44 +01:00
|
|
|
"path/filepath"
|
2017-03-13 00:40:57 +01:00
|
|
|
"sort"
|
2017-01-02 20:20:17 +01:00
|
|
|
"strings"
|
2017-03-13 00:40:57 +01:00
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/context"
|
2017-01-02 20:20:17 +01:00
|
|
|
)
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
const indexName = "/index.html"
|
|
|
|
|
2020-07-05 22:27:32 +02:00
|
|
|
// DirListFunc is the function signature for customizing directory and file listing.
|
2020-07-10 22:21:09 +02:00
|
|
|
type DirListFunc func(ctx *context.Context, dirOptions DirOptions, dirName string, dir http.File) error
|
2020-07-05 22:27:32 +02:00
|
|
|
|
2020-07-07 14:40:12 +02:00
|
|
|
// Attachments options for files to be downloaded and saved locally by the client.
|
|
|
|
// See `DirOptions`.
|
|
|
|
type Attachments struct {
|
|
|
|
// Set to true to enable the files to be downloaded and
|
|
|
|
// saved locally by the client, instead of serving the file.
|
|
|
|
Enable bool
|
|
|
|
// Options to send files with a limit of bytes sent per second.
|
|
|
|
Limit float64
|
|
|
|
Burst int
|
|
|
|
// Use this function to change the sent filename.
|
|
|
|
NameFunc func(systemName string) (attachmentName string)
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:28:44 +02:00
|
|
|
// DirOptions contains the optional settings that
|
|
|
|
// `FileServer` and `Party#HandleDir` can use to serve files and assets.
|
2019-06-21 18:43:25 +02:00
|
|
|
type DirOptions struct {
|
|
|
|
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
|
|
|
|
// then it redirects to **/*(/) which another handler is handling it,
|
|
|
|
// that another handler, called index handler, is auto-registered by the framework
|
|
|
|
// if end developer does not managed to handle it by hand.
|
|
|
|
IndexName string
|
2020-07-17 11:03:20 +02:00
|
|
|
// PushTargets filenames (map's value) to
|
|
|
|
// be served without additional client's requests (HTTP/2 Push)
|
|
|
|
// when a specific request path (map's key WITHOUT prefix)
|
|
|
|
// is requested and it's not a directory (it's an `IndexFile`).
|
2020-07-16 07:03:55 +02:00
|
|
|
PushTargets map[string][]string
|
2019-06-21 18:43:25 +02:00
|
|
|
// When files should served under compression.
|
2020-07-10 22:21:09 +02:00
|
|
|
Compress bool
|
2019-06-21 18:43:25 +02:00
|
|
|
|
|
|
|
// List the files inside the current requested directory if `IndexName` not found.
|
|
|
|
ShowList bool
|
2020-07-07 14:40:12 +02:00
|
|
|
// If `ShowList` is true then this function will be used instead
|
|
|
|
// of the default one to show the list of files of a current requested directory(dir).
|
|
|
|
// See `DirListRich` package-level function too.
|
2020-07-05 22:27:32 +02:00
|
|
|
DirList DirListFunc
|
2019-06-21 18:43:25 +02:00
|
|
|
|
2020-07-07 14:40:12 +02:00
|
|
|
// Files downloaded and saved locally.
|
|
|
|
Attachments Attachments
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// When embedded.
|
|
|
|
Asset func(name string) ([]byte, error) // we need this to make it compatible os.File.
|
|
|
|
AssetInfo func(name string) (os.FileInfo, error) // we need this for range support on embedded files.
|
|
|
|
AssetNames func() []string // called once.
|
|
|
|
|
|
|
|
// Optional validator that loops through each requested resource.
|
2020-07-10 22:21:09 +02:00
|
|
|
AssetValidator func(ctx *context.Context, name string) bool
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDirOptions(opts ...DirOptions) (options DirOptions) {
|
|
|
|
if len(opts) > 0 {
|
|
|
|
options = opts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.IndexName == "" {
|
|
|
|
options.IndexName = indexName
|
|
|
|
} else {
|
|
|
|
options.IndexName = prefix(options.IndexName, "/")
|
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
if !options.Attachments.Enable {
|
|
|
|
// make sure rate limiting is not used when attachments are not.
|
|
|
|
options.Attachments.Limit = 0
|
|
|
|
options.Attachments.Burst = 0
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:03:20 +02:00
|
|
|
// Make sure PushTarget's paths are in the proper form.
|
|
|
|
for path, filenames := range options.PushTargets {
|
|
|
|
for idx, filename := range filenames {
|
|
|
|
filenames[idx] = filepath.ToSlash(filename)
|
|
|
|
}
|
|
|
|
options.PushTargets[path] = filenames
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type embeddedFile struct {
|
|
|
|
os.FileInfo
|
|
|
|
io.ReadSeeker
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.File = (*embeddedFile)(nil)
|
|
|
|
|
|
|
|
func (f *embeddedFile) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (f *embeddedFile) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
// // this should never happen, show dirs is already checked on the handler level before this call.
|
|
|
|
// if count != -1 {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
// list := make([]os.FileInfo, len(f.dir.assetNames))
|
|
|
|
// var err error
|
|
|
|
// for i, name := range f.dir.assetNames {
|
|
|
|
// list[i], err = f.dir.assetInfo(name)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return list, nil
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (f *embeddedFile) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
return nil, nil // should never happen, read directories is done by `embeddedDir`.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *embeddedFile) Stat() (os.FileInfo, error) {
|
|
|
|
return f.FileInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (f *embeddedFile) Name() string {
|
2019-12-29 18:14:41 +01:00
|
|
|
// return strings.TrimPrefix(f.vdir, f.FileInfo.Name())
|
2019-06-21 18:43:25 +02:00
|
|
|
// }
|
|
|
|
|
|
|
|
type embeddedFileSystem struct {
|
|
|
|
vdir string
|
|
|
|
dirNames map[string]*embeddedDir // embedded tools doesn't give that info, so we initialize it in order to support `ShowList` on embedded files as well.
|
|
|
|
|
2019-06-21 22:17:27 +02:00
|
|
|
asset func(name string) ([]byte, error)
|
|
|
|
assetInfo func(name string) (os.FileInfo, error)
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.FileSystem = (*embeddedFileSystem)(nil)
|
|
|
|
|
|
|
|
func (fs *embeddedFileSystem) Open(name string) (http.File, error) {
|
2020-07-16 10:42:45 +02:00
|
|
|
if name != "/" {
|
|
|
|
// http://localhost:8080/app2/app2app3/dirs/
|
|
|
|
// = http://localhost:8080/app2/app2app3/dirs
|
|
|
|
name = strings.TrimSuffix(name, "/")
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
if d, ok := fs.dirNames[name]; ok {
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := fs.assetInfo(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, err := fs.asset(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &embeddedFile{
|
|
|
|
FileInfo: info,
|
|
|
|
ReadSeeker: bytes.NewReader(b),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type embeddedBaseFileInfo struct {
|
|
|
|
baseName string
|
|
|
|
os.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (info *embeddedBaseFileInfo) Name() string {
|
|
|
|
return info.baseName
|
|
|
|
}
|
|
|
|
|
|
|
|
type embeddedDir struct {
|
|
|
|
name string
|
2020-07-16 10:42:45 +02:00
|
|
|
baseName string
|
2019-06-21 18:43:25 +02:00
|
|
|
modTimeUnix int64
|
|
|
|
list []os.FileInfo
|
|
|
|
*bytes.Reader // never used, will always be nil.
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.File = (*embeddedDir)(nil)
|
|
|
|
|
|
|
|
func (f *embeddedDir) Close() error { return nil }
|
2020-07-16 10:42:45 +02:00
|
|
|
func (f *embeddedDir) Name() string { return f.baseName }
|
2019-06-21 18:43:25 +02:00
|
|
|
func (f *embeddedDir) Size() int64 { return 0 }
|
|
|
|
func (f *embeddedDir) Mode() os.FileMode { return os.ModeDir }
|
|
|
|
func (f *embeddedDir) ModTime() time.Time { return time.Unix(f.modTimeUnix, 0) }
|
|
|
|
func (f *embeddedDir) IsDir() bool { return true }
|
|
|
|
func (f *embeddedDir) Sys() interface{} { return f }
|
|
|
|
func (f *embeddedDir) Stat() (os.FileInfo, error) { return f, nil }
|
|
|
|
|
|
|
|
func (f *embeddedDir) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
// this should never happen, show dirs is already checked on the handler level before this call.
|
|
|
|
if count != -1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.list, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:28:44 +02:00
|
|
|
// FileServer returns a Handler which serves files from a specific system, phyisical, directory
|
|
|
|
// or an embedded one.
|
|
|
|
// The first parameter is the directory, relative to the executable program.
|
|
|
|
// The second optional parameter is any optional settings that the caller can use.
|
|
|
|
//
|
|
|
|
// See `Party#HandleDir` too.
|
2019-06-21 18:43:25 +02:00
|
|
|
// Examples can be found at: https://github.com/kataras/iris/tree/master/_examples/file-server
|
|
|
|
func FileServer(directory string, opts ...DirOptions) context.Handler {
|
|
|
|
if directory == "" {
|
|
|
|
panic("FileServer: directory is empty. The directory parameter should point to a physical system directory or to an embedded one")
|
|
|
|
}
|
|
|
|
|
|
|
|
options := getDirOptions(opts...)
|
|
|
|
|
|
|
|
// `embeddedFileSystem` (if AssetInfo, Asset and AssetNames are defined) or `http.Dir`.
|
|
|
|
var fs http.FileSystem = http.Dir(directory)
|
|
|
|
|
|
|
|
if options.Asset != nil && options.AssetInfo != nil && options.AssetNames != nil {
|
|
|
|
// Depends on the command the user gave to the go-bindata
|
|
|
|
// the assset path (names) may be or may not be prepended with a slash.
|
|
|
|
// What we do: we remove the ./ from the vdir which should be
|
|
|
|
// the same with the asset path (names).
|
|
|
|
// we don't pathclean, because that will prepend a slash
|
|
|
|
// go-bindata should give a correct path format.
|
|
|
|
// On serve time we check the "paramName" (which is the path after the "requestPath")
|
|
|
|
// so it has the first directory part missing, we use the "vdir" to complete it
|
|
|
|
// and match with the asset path (names).
|
|
|
|
vdir := directory
|
|
|
|
|
2017-06-15 19:02:08 +02:00
|
|
|
if vdir[0] == '.' {
|
|
|
|
vdir = vdir[1:]
|
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
|
|
|
|
// second check for /something, (or ./something if we had dot on 0 it will be removed)
|
|
|
|
if vdir[0] == '/' || vdir[0] == os.PathSeparator {
|
2017-06-15 19:02:08 +02:00
|
|
|
vdir = vdir[1:]
|
|
|
|
}
|
2018-03-16 11:38:13 +01:00
|
|
|
|
|
|
|
// check for trailing slashes because new users may be do that by mistake
|
|
|
|
// although all examples are showing the correct way but you never know
|
|
|
|
// i.e "./assets/" is not correct, if was inside "./assets".
|
|
|
|
// remove last "/".
|
|
|
|
if trailingSlashIdx := len(vdir) - 1; vdir[trailingSlashIdx] == '/' {
|
|
|
|
vdir = vdir[0:trailingSlashIdx]
|
|
|
|
}
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// select only the paths that we care;
|
|
|
|
// that have prefix of the directory and
|
|
|
|
// skip any unnecessary the end-dev or the 3rd party tool may set.
|
|
|
|
var names []string
|
|
|
|
for _, name := range options.AssetNames() {
|
|
|
|
// i.e: name = static/css/main.css (including the directory, see `embeddedFileSystem.vdir`)
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
if !strings.HasPrefix(name, vdir) {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-12-29 18:14:41 +01:00
|
|
|
names = append(names, strings.TrimPrefix(name, vdir))
|
2017-06-15 19:02:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
if len(names) == 0 {
|
|
|
|
panic("FileServer: zero embedded files")
|
|
|
|
}
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
asset := func(name string) ([]byte, error) {
|
|
|
|
return options.Asset(vdir + name)
|
|
|
|
}
|
2018-03-14 06:17:35 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
assetInfo := func(name string) (os.FileInfo, error) {
|
|
|
|
return options.AssetInfo(vdir + name)
|
|
|
|
}
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
dirNames := make(map[string]*embeddedDir)
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-10-25 00:04:08 +02:00
|
|
|
// sort filenames by smaller path.
|
|
|
|
sort.Slice(names, func(i, j int) bool {
|
|
|
|
return strings.Count(names[j], "/") > strings.Count(names[i], "/")
|
|
|
|
})
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2019-10-25 00:04:08 +02:00
|
|
|
for _, name := range names {
|
|
|
|
dirName := path.Dir(name)
|
|
|
|
d, ok := dirNames[dirName]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
d = &embeddedDir{
|
|
|
|
name: dirName,
|
2020-07-16 10:42:45 +02:00
|
|
|
baseName: path.Base(dirName),
|
2019-10-25 00:04:08 +02:00
|
|
|
modTimeUnix: time.Now().Unix(),
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
2019-10-25 00:04:08 +02:00
|
|
|
dirNames[dirName] = d
|
2018-03-14 06:17:35 +01:00
|
|
|
}
|
|
|
|
|
2019-10-25 00:04:08 +02:00
|
|
|
info, err := assetInfo(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("FileServer: report as bug: file info: %s not found in: %s", name, dirName))
|
|
|
|
}
|
2020-07-16 10:42:45 +02:00
|
|
|
|
|
|
|
// Add the directory file info (=this dir) to the parent one,
|
|
|
|
// so `ShowList` can render sub-directories of this dir.
|
|
|
|
if parent, hasParent := dirNames[path.Dir(dirName)]; hasParent {
|
|
|
|
parent.list = append(parent.list, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &embeddedBaseFileInfo{path.Base(name), info}
|
|
|
|
d.list = append(d.list, f)
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
2018-03-14 06:17:35 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
fs = &embeddedFileSystem{
|
|
|
|
vdir: vdir,
|
|
|
|
dirNames: dirNames,
|
|
|
|
|
2019-06-21 22:17:27 +02:00
|
|
|
asset: asset,
|
|
|
|
assetInfo: assetInfo,
|
2017-06-15 19:02:08 +02:00
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
2019-06-21 22:17:27 +02:00
|
|
|
// Let it for now.
|
|
|
|
// else if !DirectoryExists(directory) {
|
|
|
|
// panic("FileServer: system directory: " + directory + " does not exist")
|
|
|
|
// }
|
2017-06-15 19:02:08 +02:00
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
plainStatusCode := func(ctx *context.Context, statusCode int) {
|
|
|
|
if writer, ok := ctx.ResponseWriter().(*context.CompressResponseWriter); ok {
|
|
|
|
writer.Disabled = true
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
ctx.StatusCode(statusCode)
|
2017-06-15 19:02:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
dirList := options.DirList
|
|
|
|
if dirList == nil {
|
2020-07-10 22:21:09 +02:00
|
|
|
dirList = func(ctx *context.Context, dirOptions DirOptions, dirName string, dir http.File) error {
|
2019-06-21 18:43:25 +02:00
|
|
|
dirs, err := dir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-14 01:58:56 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
ctx.ContentType(context.ContentHTMLHeaderValue)
|
2020-02-02 15:29:06 +01:00
|
|
|
_, err = ctx.WriteString("<pre>\n")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-10 22:21:09 +02:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
for _, d := range dirs {
|
|
|
|
name := d.Name()
|
2020-07-05 04:39:48 +02:00
|
|
|
|
|
|
|
upath := ""
|
2020-07-16 10:42:45 +02:00
|
|
|
if !strings.HasSuffix(ctx.Path(), "/") && dirName != "" {
|
|
|
|
upath = "./" + path.Base(dirName) + "/" + name
|
2020-07-05 04:39:48 +02:00
|
|
|
} else {
|
2020-07-16 10:42:45 +02:00
|
|
|
upath = "./" + name
|
2020-07-05 04:39:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:42:45 +02:00
|
|
|
url := url.URL{Path: upath}
|
2020-07-05 04:39:48 +02:00
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
downloadAttr := ""
|
|
|
|
if dirOptions.Attachments.Enable && !d.IsDir() {
|
|
|
|
downloadAttr = " download" // fixes chrome Resource interpreted, other browsers will just ignore this <a> attribute.
|
|
|
|
}
|
2020-07-16 10:42:45 +02:00
|
|
|
|
|
|
|
viewName := name
|
|
|
|
if d.IsDir() {
|
|
|
|
viewName += "/"
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// name may contain '?' or '#', which must be escaped to remain
|
|
|
|
// part of the URL path, and not indicate the start of a query
|
|
|
|
// string or fragment.
|
2020-07-16 10:42:45 +02:00
|
|
|
_, err = ctx.Writef("<a href=\"%s\"%s>%s</a>\n", url.String(), downloadAttr, html.EscapeString(viewName))
|
2020-02-02 15:29:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
2020-02-02 15:29:06 +01:00
|
|
|
_, err = ctx.WriteString("</pre>\n")
|
|
|
|
return err
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
2017-02-16 21:19:44 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
h := func(ctx *context.Context) {
|
2020-07-17 11:03:20 +02:00
|
|
|
r := ctx.Request()
|
|
|
|
name := prefix(r.URL.Path, "/")
|
|
|
|
r.URL.Path = name
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
f, err := fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
plainStatusCode(ctx, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
info, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
plainStatusCode(ctx, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
indexFound := false
|
2019-06-21 18:43:25 +02:00
|
|
|
// use contents of index.html for directory, if present
|
|
|
|
if info.IsDir() && options.IndexName != "" {
|
|
|
|
// Note that, in contrast of the default net/http mechanism;
|
|
|
|
// here different handlers may serve the indexes
|
|
|
|
// if manually then this will block will never fire,
|
|
|
|
// if index handler are automatically registered by the framework
|
|
|
|
// then this block will be fired on indexes because the static site routes are registered using the static route's handler.
|
|
|
|
//
|
|
|
|
// End-developers must have the chance to register different logic and middlewares
|
|
|
|
// to an index file, useful on Single Page Applications.
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
index := strings.TrimSuffix(name, "/") + options.IndexName
|
|
|
|
fIndex, err := fs.Open(index)
|
|
|
|
if err == nil {
|
|
|
|
defer fIndex.Close()
|
|
|
|
infoIndex, err := fIndex.Stat()
|
|
|
|
if err == nil {
|
|
|
|
info = infoIndex
|
|
|
|
f = fIndex
|
2020-07-10 22:21:09 +02:00
|
|
|
indexFound = true
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2020-07-17 11:03:20 +02:00
|
|
|
if indexFound && !options.Attachments.Enable {
|
|
|
|
if indexAssets, ok := options.PushTargets[name]; ok {
|
|
|
|
if pusher, ok := ctx.ResponseWriter().(http.Pusher); ok {
|
|
|
|
for _, indexAsset := range indexAssets {
|
|
|
|
// pushOpts := &http.PushOptions{
|
|
|
|
// Method: "GET",
|
|
|
|
// Header: http.Header{
|
|
|
|
// "Vary": []string{"Accept-Encoding"},
|
|
|
|
// "Content-Encoding": []string{"gzip"},
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
if indexAsset[0] != '/' {
|
|
|
|
// it's relative path.
|
|
|
|
indexAsset = path.Join(r.RequestURI, indexAsset)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pusher.Push(indexAsset, nil); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// Still a directory? (we didn't find an index.html file)
|
|
|
|
if info.IsDir() {
|
|
|
|
if !options.ShowList {
|
|
|
|
plainStatusCode(ctx, http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if modified, err := ctx.CheckIfModifiedSince(info.ModTime()); !modified && err == nil {
|
|
|
|
ctx.WriteNotModified()
|
|
|
|
ctx.StatusCode(http.StatusNotModified)
|
|
|
|
ctx.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.SetLastModified(info.ModTime())
|
2020-07-10 22:21:09 +02:00
|
|
|
err = dirList(ctx, options, info.Name(), f)
|
2019-06-21 18:43:25 +02:00
|
|
|
if err != nil {
|
2020-07-06 15:06:48 +02:00
|
|
|
ctx.Application().Logger().Errorf("FileServer: dirList: %v", err)
|
2019-06-21 18:43:25 +02:00
|
|
|
plainStatusCode(ctx, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
ctx.Next()
|
|
|
|
return
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// index requested, send a moved permanently status
|
|
|
|
// and navigate back to the route without the index suffix.
|
|
|
|
if strings.HasSuffix(name, options.IndexName) {
|
|
|
|
localRedirect(ctx, "./")
|
|
|
|
return
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
if options.AssetValidator != nil {
|
|
|
|
if !options.AssetValidator(ctx, info.Name()) {
|
|
|
|
errCode := ctx.GetStatusCode()
|
|
|
|
if ctx.ResponseWriter().Written() <= context.StatusCodeWritten {
|
|
|
|
// if nothing written as body from the AssetValidator but 200 status code(which is the default),
|
|
|
|
// then we assume that the end-developer just returned false expecting this to be not found.
|
|
|
|
if errCode == http.StatusOK {
|
|
|
|
errCode = http.StatusNotFound
|
|
|
|
}
|
|
|
|
plainStatusCode(ctx, errCode)
|
|
|
|
}
|
|
|
|
return
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to find and send the correct content type based on the filename
|
|
|
|
// and the binary data inside "f".
|
|
|
|
detectOrWriteContentType(ctx, info.Name(), f)
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
// if not index file and attachments should be force-sent:
|
|
|
|
if !indexFound && options.Attachments.Enable {
|
2020-07-07 14:40:12 +02:00
|
|
|
destName := info.Name()
|
2020-07-10 22:21:09 +02:00
|
|
|
// diposition := "attachment"
|
2020-07-07 14:40:12 +02:00
|
|
|
if nameFunc := options.Attachments.NameFunc; nameFunc != nil {
|
|
|
|
destName = nameFunc(destName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.ResponseWriter().Header().Set(context.ContentDispositionHeaderKey, "attachment;filename="+destName)
|
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
ctx.Compress(options.Compress)
|
|
|
|
|
2020-07-07 14:40:12 +02:00
|
|
|
// If limit is 0 then same as ServeContent.
|
|
|
|
ctx.ServeContentWithRate(f, info.Name(), info.ModTime(), options.Attachments.Limit, options.Attachments.Burst)
|
2019-06-21 18:43:25 +02:00
|
|
|
if serveCode := ctx.GetStatusCode(); context.StatusCodeNotSuccessful(serveCode) {
|
|
|
|
plainStatusCode(ctx, serveCode)
|
|
|
|
return
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
2018-03-14 06:17:35 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
ctx.Next() // fire any middleware, if any.
|
|
|
|
}
|
2017-01-02 20:20:17 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
return h
|
2017-01-02 20:20:17 +01:00
|
|
|
}
|
2017-01-12 09:24:27 +01:00
|
|
|
|
|
|
|
// StripPrefix returns a handler that serves HTTP requests
|
|
|
|
// by removing the given prefix from the request URL's Path
|
|
|
|
// and invoking the handler h. StripPrefix handles a
|
|
|
|
// request for a path that doesn't begin with prefix by
|
|
|
|
// replying with an HTTP 404 not found error.
|
2017-03-14 01:58:56 +01:00
|
|
|
//
|
|
|
|
// Usage:
|
2019-06-21 18:43:25 +02:00
|
|
|
// fileserver := FileServer("./static_files", DirOptions {...})
|
|
|
|
// h := StripPrefix("/static", fileserver)
|
2019-11-05 21:12:26 +01:00
|
|
|
// app.Get("/static/{file:path}", h)
|
|
|
|
// app.Head("/static/{file:path}", h)
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
func StripPrefix(prefix string, h context.Handler) context.Handler {
|
2017-01-12 09:24:27 +01:00
|
|
|
if prefix == "" {
|
|
|
|
return h
|
|
|
|
}
|
2017-03-14 01:58:56 +01:00
|
|
|
// here we separate the path from the subdomain (if any), we care only for the path
|
|
|
|
// fixes a bug when serving static files via a subdomain
|
2019-06-21 18:43:25 +02:00
|
|
|
canonicalPrefix := prefix
|
|
|
|
if dotWSlashIdx := strings.Index(canonicalPrefix, SubdomainPrefix); dotWSlashIdx > 0 {
|
|
|
|
canonicalPrefix = canonicalPrefix[dotWSlashIdx+1:]
|
2017-03-14 01:58:56 +01:00
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
canonicalPrefix = toWebPath(canonicalPrefix)
|
2017-03-14 01:58:56 +01:00
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
return func(ctx *context.Context) {
|
2019-06-21 18:43:25 +02:00
|
|
|
if p := strings.TrimPrefix(ctx.Request().URL.Path, canonicalPrefix); len(p) < len(ctx.Request().URL.Path) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.Request().URL.Path = p
|
2017-01-12 09:24:27 +01:00
|
|
|
h(ctx)
|
|
|
|
} else {
|
|
|
|
ctx.NotFound()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 18:17:32 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
func toWebPath(systemPath string) string {
|
|
|
|
// winos slash to slash
|
|
|
|
webpath := strings.Replace(systemPath, "\\", "/", -1)
|
|
|
|
// double slashes to single
|
|
|
|
webpath = strings.Replace(webpath, "//", "/", -1)
|
|
|
|
return webpath
|
|
|
|
}
|
2017-03-13 00:40:57 +01:00
|
|
|
|
2019-06-21 18:43:25 +02:00
|
|
|
// Abs calls filepath.Abs but ignores the error and
|
|
|
|
// returns the original value if any error occurred.
|
|
|
|
func Abs(path string) string {
|
|
|
|
absPath, err := filepath.Abs(path)
|
2017-03-13 00:40:57 +01:00
|
|
|
if err != nil {
|
2019-06-21 18:43:25 +02:00
|
|
|
return path
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
2019-06-21 18:43:25 +02:00
|
|
|
return absPath
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The algorithm uses at most sniffLen bytes to make its decision.
|
|
|
|
const sniffLen = 512
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
func detectOrWriteContentType(ctx *context.Context, name string, content io.ReadSeeker) (string, error) {
|
2017-03-13 00:40:57 +01:00
|
|
|
// If Content-Type isn't set, use the file's extension to find it, but
|
|
|
|
// if the Content-Type is unset explicitly, do not sniff the type.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctypes, haveType := ctx.ResponseWriter().Header()["Content-Type"]
|
2017-03-13 00:40:57 +01:00
|
|
|
var ctype string
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
|
2017-03-13 00:40:57 +01:00
|
|
|
if !haveType {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctype = TypeByExtension(filepath.Ext(name))
|
2017-03-13 00:40:57 +01:00
|
|
|
if ctype == "" {
|
|
|
|
// read a chunk to decide between utf-8 text and binary
|
|
|
|
var buf [sniffLen]byte
|
|
|
|
n, _ := io.ReadFull(content, buf[:])
|
|
|
|
ctype = http.DetectContentType(buf[:n])
|
|
|
|
_, err := content.Seek(0, io.SeekStart) // rewind to output whole file
|
|
|
|
if err != nil {
|
2017-08-12 07:49:00 +02:00
|
|
|
return "", err
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
|
|
|
}
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
|
|
|
|
ctx.ContentType(ctype)
|
2017-03-13 00:40:57 +01:00
|
|
|
} else if len(ctypes) > 0 {
|
|
|
|
ctype = ctypes[0]
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:49:00 +02:00
|
|
|
return ctype, nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 00:40:57 +01:00
|
|
|
// localRedirect gives a Moved Permanently response.
|
|
|
|
// It does not convert relative paths to absolute paths like Redirect does.
|
2020-07-10 22:21:09 +02:00
|
|
|
func localRedirect(ctx *context.Context, newPath string) {
|
2017-11-10 15:15:47 +01:00
|
|
|
if q := ctx.Request().URL.RawQuery; q != "" {
|
2017-03-13 00:40:57 +01:00
|
|
|
newPath += "?" + q
|
|
|
|
}
|
2017-11-10 15:15:47 +01:00
|
|
|
|
|
|
|
ctx.Header("Location", newPath)
|
|
|
|
ctx.StatusCode(http.StatusMovedPermanently)
|
2017-03-13 00:40:57 +01:00
|
|
|
}
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
// DirectoryExists returns true if a directory(or file) exists, otherwise false
|
|
|
|
func DirectoryExists(dir string) bool {
|
2017-03-01 18:17:32 +01:00
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2020-07-05 22:27:32 +02:00
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
// DirListRichOptions the options for the `DirListRich` helper function.
|
|
|
|
type DirListRichOptions struct {
|
2020-07-07 05:11:44 +02:00
|
|
|
// If not nil then this template's "dirlist" is used to render the listing page.
|
2020-07-06 15:06:48 +02:00
|
|
|
Tmpl *template.Template
|
2020-07-07 05:11:44 +02:00
|
|
|
// If not empty then this view file is used to render the listing page.
|
|
|
|
// The view should be registered with `Application.RegisterView`.
|
|
|
|
// E.g. "dirlist.html"
|
|
|
|
TmplName string
|
2020-07-06 15:06:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 22:27:32 +02:00
|
|
|
// DirListRich is a `DirListFunc` which can be passed to `DirOptions.DirList` field
|
|
|
|
// to override the default file listing appearance.
|
|
|
|
// See `DirListRichTemplate` to modify the template, if necessary.
|
2020-07-06 15:06:48 +02:00
|
|
|
func DirListRich(opts ...DirListRichOptions) DirListFunc {
|
|
|
|
var options DirListRichOptions
|
|
|
|
if len(opts) > 0 {
|
|
|
|
options = opts[0]
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
2020-07-07 05:11:44 +02:00
|
|
|
if options.TmplName == "" && options.Tmpl == nil {
|
2020-07-06 15:06:48 +02:00
|
|
|
options.Tmpl = DirListRichTemplate
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
return func(ctx *context.Context, dirOptions DirOptions, dirName string, dir http.File) error {
|
2020-07-06 15:06:48 +02:00
|
|
|
dirs, err := dir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-05 22:27:32 +02:00
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
sortBy := ctx.URLParam("sort")
|
|
|
|
switch sortBy {
|
|
|
|
case "name":
|
|
|
|
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
|
|
|
|
case "size":
|
|
|
|
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Size() < dirs[j].Size() })
|
|
|
|
default:
|
|
|
|
sort.Slice(dirs, func(i, j int) bool { return dirs[i].ModTime().After(dirs[j].ModTime()) })
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
pageData := listPageData{
|
|
|
|
Title: fmt.Sprintf("List of %d files", len(dirs)),
|
|
|
|
Files: make([]fileInfoData, 0, len(dirs)),
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
for _, d := range dirs {
|
|
|
|
name := d.Name()
|
2020-07-05 22:27:32 +02:00
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
upath := ""
|
2020-07-16 10:42:45 +02:00
|
|
|
if !strings.HasSuffix(ctx.Path(), "/") && dirName != "" {
|
|
|
|
upath = "./" + path.Base(dirName) + "/" + name
|
2020-07-06 15:06:48 +02:00
|
|
|
} else {
|
2020-07-16 10:42:45 +02:00
|
|
|
upath = "./" + name
|
2020-07-06 15:06:48 +02:00
|
|
|
}
|
2020-07-05 22:27:32 +02:00
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
url := url.URL{Path: upath}
|
|
|
|
|
2020-07-16 10:42:45 +02:00
|
|
|
viewName := name
|
|
|
|
if d.IsDir() {
|
|
|
|
viewName += "/"
|
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
shouldDownload := dirOptions.Attachments.Enable && !d.IsDir()
|
2020-07-06 15:06:48 +02:00
|
|
|
pageData.Files = append(pageData.Files, fileInfoData{
|
2020-07-10 22:21:09 +02:00
|
|
|
Info: d,
|
|
|
|
ModTime: d.ModTime().UTC().Format(http.TimeFormat),
|
|
|
|
Path: url.String(),
|
|
|
|
RelPath: path.Join(ctx.Path(), name),
|
2020-07-16 10:42:45 +02:00
|
|
|
Name: html.EscapeString(viewName),
|
2020-07-10 22:21:09 +02:00
|
|
|
Download: shouldDownload,
|
2020-07-06 15:06:48 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-07 05:11:44 +02:00
|
|
|
if options.TmplName != "" {
|
|
|
|
return ctx.View(options.TmplName, pageData)
|
|
|
|
}
|
|
|
|
|
2020-07-06 15:06:48 +02:00
|
|
|
return options.Tmpl.ExecuteTemplate(ctx, "dirlist", pageData)
|
|
|
|
}
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
listPageData struct {
|
|
|
|
Title string // the document's title.
|
|
|
|
Files []fileInfoData
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfoData struct {
|
2020-07-10 22:21:09 +02:00
|
|
|
Info os.FileInfo
|
|
|
|
ModTime string // format-ed time.
|
|
|
|
Path string // the request path.
|
|
|
|
RelPath string // file path without the system directory itself (we are not exposing it to the user).
|
|
|
|
Name string // the html-escaped name.
|
|
|
|
Download bool // the file should be downloaded (attachment instead of inline view).
|
2020-07-05 22:27:32 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirListRichTemplate is the html template the `DirListRich` function is using to render
|
|
|
|
// the directories and files.
|
2020-07-06 15:06:48 +02:00
|
|
|
var DirListRichTemplate = template.Must(template.New("dirlist").
|
2020-07-05 22:27:32 +02:00
|
|
|
Funcs(template.FuncMap{
|
|
|
|
"formatBytes": func(b int64) string {
|
|
|
|
const unit = 1000
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.1f %cB",
|
|
|
|
float64(b)/float64(div), "kMGTPE"[exp])
|
|
|
|
},
|
|
|
|
}).Parse(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>{{.Title}}</title>
|
|
|
|
<style>
|
|
|
|
a {
|
|
|
|
padding: 8px 8px;
|
|
|
|
text-decoration:none;
|
|
|
|
cursor:pointer;
|
|
|
|
color: #10a2ff;
|
|
|
|
}
|
|
|
|
table {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
height: 100%;
|
|
|
|
width: 100%;
|
|
|
|
border-collapse: collapse;
|
|
|
|
border-spacing: 0;
|
|
|
|
empty-cells: show;
|
|
|
|
border: 1px solid #cbcbcb;
|
|
|
|
}
|
|
|
|
|
|
|
|
table caption {
|
|
|
|
color: #000;
|
|
|
|
font: italic 85%/1 arial, sans-serif;
|
|
|
|
padding: 1em 0;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
table td,
|
|
|
|
table th {
|
|
|
|
border-left: 1px solid #cbcbcb;
|
|
|
|
border-width: 0 0 0 1px;
|
|
|
|
font-size: inherit;
|
|
|
|
margin: 0;
|
|
|
|
overflow: visible;
|
|
|
|
padding: 0.5em 1em;
|
|
|
|
}
|
|
|
|
|
|
|
|
table thead {
|
|
|
|
background-color: #10a2ff;
|
|
|
|
color: #fff;
|
|
|
|
text-align: left;
|
|
|
|
vertical-align: bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
table td {
|
|
|
|
background-color: transparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-odd td {
|
|
|
|
background-color: #f2f2f2;
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-bordered td {
|
|
|
|
border-bottom: 1px solid #cbcbcb;
|
|
|
|
}
|
|
|
|
.table-bordered tbody > tr:last-child > td {
|
|
|
|
border-bottom-width: 0;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<table class="table-bordered table-odd">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>#</th>
|
|
|
|
<th>Name</th>
|
2020-07-06 15:06:48 +02:00
|
|
|
<th>Size</th>
|
2020-07-05 22:27:32 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{{ range $idx, $file := .Files }}
|
|
|
|
<tr>
|
|
|
|
<td>{{ $idx }}</td>
|
2020-07-10 22:21:09 +02:00
|
|
|
{{ if $file.Download }}
|
|
|
|
<td><a href="{{ $file.Path }}" title="{{ $file.ModTime }}" download>{{ $file.Name }}</a></td>
|
|
|
|
{{ else }}
|
|
|
|
<td><a href="{{ $file.Path }}" title="{{ $file.ModTime }}">{{ $file.Name }}</a></td>
|
|
|
|
{{ end }}
|
2020-07-06 15:06:48 +02:00
|
|
|
{{ if $file.Info.IsDir }}
|
|
|
|
<td>Dir</td>
|
|
|
|
{{ else }}
|
|
|
|
<td>{{ formatBytes $file.Info.Size }}</td>
|
|
|
|
{{ end }}
|
2020-07-05 22:27:32 +02:00
|
|
|
</tr>
|
|
|
|
{{ end }}
|
|
|
|
</tbody>
|
2020-07-06 15:06:48 +02:00
|
|
|
</table>
|
2020-07-05 22:27:32 +02:00
|
|
|
</body></html>
|
|
|
|
`))
|