diff options
| author | veradec <glitchmill.dyj3d@simplelogin.co> | 2026-11-12 10:13:24 +0530 |
|---|---|---|
| committer | veradec <glitchmill.dyj3d@simplelogin.co> | 2026-11-12 10:13:24 +0530 |
| commit | 7af53656c4b6a4e024a6618fc122fda06257d554 (patch) | |
| tree | fde9d05643925d27036c6b456192a294d58e7e82 | |
| parent | 282c26ce8411e17d75c3ca47fbd3876a9e1c9cb9 (diff) | |
add: creates a /random_dir/file_name just to have some de-duplicates
| -rw-r--r-- | main.go | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -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") } |
