From 7af53656c4b6a4e024a6618fc122fda06257d554 Mon Sep 17 00:00:00 2001 From: veradec Date: Thu, 12 Nov 2026 10:13:24 +0530 Subject: add: creates a /random_dir/file_name just to have some de-duplicates --- main.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 9af9e21..3a72a89 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,36 @@ package main import ( + "math/rand" "net/http" "fmt" "log" "path/filepath" "github.com/gin-gonic/gin" + "flag" + "time" + "os" ) +func randomString(length int) string { + rand.Seed(time.Now().UnixNano()) + + const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + result := make([]byte, length) + + for i := range result { + result[i] = charset[rand.Intn(len(charset))] + } + + return string(result) +} + func main(){ router := gin.Default() + + domain := flag.String("domain", "Domain", "Website Link") + + flag.Parse() router.POST("/", func(c *gin.Context) { // single file @@ -19,10 +40,10 @@ func main(){ return } log.Println(file.Filename) - - dst := filepath.Join("./files/", filepath.Base(file.Filename)) + random_string := randomString(6) + dst := filepath.Join("./files/", random_string, filepath.Base(file.Filename)) c.SaveUploadedFile(file, dst) - c.String(http.StatusOK, fmt.Sprintf("%s/%s\n", domain , file.Filename)) + c.String(http.StatusOK, fmt.Sprintf("%s/%s/%s\n", *domain, random_string ,file.Filename)) }) router.LoadHTMLFiles("index.html") @@ -31,10 +52,9 @@ func main(){ c.HTML(http.StatusOK, "index.html", gin.H{}) }) - var safeFS http.FileSystem = http.Dir("./files") - router.GET("/:name", func(c *gin.Context) { - c.FileFromFS(c.Param("name"), safeFS) - }) + files := http.FileServer(http.FS(os.DirFS("./files"))) + + router.GET("/:random_string/:name", gin.WrapH(files)) router.Run(":8080") } -- cgit v1.2.3