1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-22 12:27:02 +00:00

file-server: fix ShowList on root dir

Former-commit-id: 6795382235d76942bcfd31ecc0b4ab02ecb85a8a
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-05 05:39:48 +03:00
parent 0a1b500c8b
commit 57dc64625d
12 changed files with 142 additions and 131 deletions

View File

@@ -78,10 +78,6 @@ func TestEmbeddingFilesIntoApp(t *testing.T) {
t.Fatalf("expected a route to serve embedded files")
}
if len(route.StaticSites()) > 0 {
t.Fatalf("not expected a static site, the ./assets directory or its subdirectories do not contain any index.html")
}
if runtime.GOOS != "windows" {
// remove the embedded static favicon for !windows,
// it should be built for unix-specific in order to be work

View File

@@ -0,0 +1,66 @@
package main
import (
"crypto/md5"
"fmt"
"io"
"mime/multipart"
"os"
"strconv"
"strings"
"time"
"github.com/kataras/iris/v12"
)
func init() {
os.Mkdir("./uploads", 0700)
}
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
app.Get("/", index)
app.Get("/upload", uploadView)
app.Post("/upload", upload)
app.HandleDir("/files", "./uploads", iris.DirOptions{
Gzip: true,
ShowList: true,
})
app.Listen(":8080")
}
func index(ctx iris.Context) {
ctx.Redirect("/upload")
}
func uploadView(ctx iris.Context) {
now := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
ctx.View("upload.html", token)
}
func upload(ctx iris.Context) {
_, err := ctx.UploadFormFiles("./uploads", beforeSave)
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Redirect("/files")
}
func beforeSave(ctx iris.Context, file *multipart.FileHeader) {
ip := ctx.RemoteAddr()
ip = strings.ReplaceAll(ip, ".", "_")
ip = strings.ReplaceAll(ip, ":", "_")
file.Filename = ip + "-" + file.Filename
}

View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Files</title>
</head>
<body>
<form enctype="multipart/form-data"
action="/upload" method="POST">
<input type="file" name="uploadfile" multiple/> <input type="hidden"
name="token" value="{{.}}" />
<input type="submit" value="upload" />
</form>
</body>
</html>