Skip to content

Commit

Permalink
feat: create subfolder for uploaded files (close #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Apr 14, 2023
1 parent 9d2bb2d commit 7bf5187
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 15 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"log"
"net"
"os"
"os/exec"
"runtime"
"strconv"
Expand Down Expand Up @@ -123,3 +124,17 @@ func IntMax(a int, b int) int {
func IsMobileUserAgent(userAgent string) bool {
return strings.Contains(userAgent, "Mobile") || strings.Contains(userAgent, "Android") || strings.Contains(userAgent, "iPhone") || strings.Contains(userAgent, "iPad")
}

func MakeDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
} else {
return err
}
}
return nil
}
25 changes: 19 additions & 6 deletions controller/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"go-file/common"
"go-file/model"
"log"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -66,14 +67,21 @@ func UploadFile(c *gin.Context) {
}
files = append(files, file)
}
t := time.Now()
subfolder := t.Format("2006-01")
err = common.MakeDirIfNotExist(filepath.Join(uploadPath, subfolder))
if err != nil {
log.Println(err)
c.Status(http.StatusInternalServerError)
return
}
for _, file := range files {
// In case someone wants to upload to other folders.
filename := filepath.Base(file.Filename)
link := filename
savePath := filepath.Join(uploadPath, filename)
link := fmt.Sprintf("%s/%s", subfolder, filename)
savePath := filepath.Join(uploadPath, subfolder, filename)
if _, err := os.Stat(savePath); err == nil {
// File already existed.
t := time.Now()
timestamp := t.Format("_2006-01-02_15-04-05")
ext := filepath.Ext(filename)
if ext == "" {
Expand Down Expand Up @@ -155,8 +163,13 @@ func DeleteFile(c *gin.Context) {
}

func DownloadFile(c *gin.Context) {
path := c.Param("file")
fullPath := filepath.Join(common.UploadPath, path)
path := c.Param("filepath")
subfolder, filename := filepath.Split(path)
link := filename // Keep compatibility with old version
if subfolder != "/" {
link = fmt.Sprintf("%s/%s", subfolder, filename)
}
fullPath := filepath.Join(common.UploadPath, subfolder, filename)
if !strings.HasPrefix(fullPath, common.UploadPath) {
// We may being attacked!
c.Status(403)
Expand All @@ -176,6 +189,6 @@ func DownloadFile(c *gin.Context) {
}
// Update download counter
go func() {
model.UpdateDownloadCounter(path)
model.UpdateDownloadCounter(link)
}()
}
2 changes: 1 addition & 1 deletion router/web-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func setWebRouter(router *gin.Engine) {
fileDownloadAuth := router.Group("/")
fileDownloadAuth.Use(middleware.DownloadRateLimit(), middleware.FileDownloadPermissionCheck())
{
fileDownloadAuth.GET("/upload/:file", controller.DownloadFile)
fileDownloadAuth.GET("/upload/*filepath", controller.DownloadFile)
fileDownloadAuth.GET("/explorer", controller.GetExplorerPageOrFile)
}

Expand Down

0 comments on commit 7bf5187

Please sign in to comment.