mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
file-server: fix ShowList on root dir
Former-commit-id: 6795382235d76942bcfd31ecc0b4ab02ecb85a8a
This commit is contained in:
66
_examples/file-server/file-server/main.go
Normal file
66
_examples/file-server/file-server/main.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user