Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Greyh4t committed Nov 30, 2018
0 parents commit 1b488d5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# fileserver

简易文件服务器
支持文件上传及下载
90 changes: 90 additions & 0 deletions fileserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"flag"
"fmt"
"net/url"
"path"
"strings"

"github.com/gin-gonic/gin"
)

var (
port string
username string
password string
dir string
head = `<html>
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="files" multiple="multiple" />
<input type="submit" value="上传" />
</form>
`
errFmt = `<font color="red">%s</font><br />
<input type="button" value="返回" onclick="history.back()">`
)

func init() {
flag.StringVar(&port, "port", "80", "http port")
flag.StringVar(&username, "user", "", "username")
flag.StringVar(&password, "pass", "", "password")
flag.StringVar(&dir, "dir", "./", "file dir")
flag.Parse()
}

func uploadFile(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, fmt.Sprintf(errFmt, "Error: "+err.Error()))
}

var p = "/"
u, err := url.ParseRequestURI(c.GetHeader("Referer"))
if err == nil {
// remove ../
p = path.Clean("/"+u.Path) + "/"
}

files := form.File["files"]
for _, file := range files {
err = c.SaveUploadedFile(file, path.Join(dir, p, path.Clean("/"+file.Filename)))
if err != nil {
c.Header("Content-Type", "text/html; charset=utf-8")
c.String(200, fmt.Sprintf(errFmt, file.Filename+" 上传失败<br />Error: "+err.Error()))
break
}
}
c.Redirect(302, p)
}

func writeHead(c *gin.Context) {
if strings.HasSuffix(c.Request.URL.Path, "/") {
c.Writer.WriteString(head)
}
}

func main() {
gin.SetMode(gin.ReleaseMode)
r := gin.Default()

var authGroup *gin.RouterGroup
if username != "" {
authGroup = r.Group("/", gin.BasicAuth(gin.Accounts{
username: password,
}))
} else {
authGroup = r.Group("/")
}

getGroup := authGroup.Group("/", writeHead)

getGroup.StaticFS("/", gin.Dir(dir, true))
authGroup.POST("/", uploadFile)

err := r.Run(":" + port)
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 1b488d5

Please sign in to comment.