diff --git a/.gitignore b/.gitignore index 0607035..577faab 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ uploads/ # Files .env gin.log -main.exe \ No newline at end of file +main.exe +config.example.yml \ No newline at end of file diff --git a/README.md b/README.md index 9b085bd..c8791aa 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ I made this as part of learning Go, so it's probably bad code. I don't recommend 3. Either run the server with ``go run main.go`` or build with ``go build main.go`` and run the binary ## License -[MIT](https://github.com/davidjcralph/gosharex/blob/master/LICENSE) +[MIT](https://github.com/davidjcralph/gosharex/blob/main/LICENSE) diff --git a/go.mod b/go.mod index a9896f2..378dabb 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ -module main +module davidjcralph.co.uk/gosharex go 1.14 require ( - github.com/bramvdbogaerde/go-randomstring v0.0.0-20160712175629-9e00e68359ca github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2 github.com/gin-gonic/gin v1.5.0 github.com/joho/godotenv v1.3.0 - github.com/knq/baseconv v0.0.0-20180401001559-5ac6a1b7584c // indirect github.com/zcong1993/gin-ratelimit v0.0.0-20201103092833-69c87ffc6f97 // direct ) + +replace davidjcralph.co.uk/gosharex/util => ./util/ \ No newline at end of file diff --git a/main.go b/main.go index 31c9b72..c81e5bf 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/bramvdbogaerde/go-randomstring" + "davidjcralph.co.uk/gosharex/util" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" ratelimit "github.com/zcong1993/gin-ratelimit" @@ -52,8 +52,8 @@ func setupRouter() *gin.Engine { return } - filename := randomstring.New() + "." + strings.Split(file.Filename, ".")[1] // Create random filename... - c.SaveUploadedFile(file, os.Getenv("FOLDER")+filename) // ...and move the file to the uploads folder + filename := util.RandomString(4) + "." + strings.Split(file.Filename, ".")[1] // Create random filename... + c.SaveUploadedFile(file, os.Getenv("FOLDER")+filename) // ...and move the file to the uploads folder c.JSON(200, gin.H{"file": filename}) }) @@ -82,18 +82,20 @@ func setupRouter() *gin.Engine { c.JSON(200, gin.H{"fileCount": len(files), "uptime": time.Since(startTime)}) }) - router.Use(static.Serve("/", static.LocalFile(os.Getenv("FOLDER"), true))) // Static files in ./uploads/ + router.Use(static.Serve("/web", static.LocalFile(os.Getenv("FOLDER"), true))) // Static files in ./uploads/ if os.Getenv("WEB") == "true" { - router.LoadHTMLGlob("templates/*") + router.LoadHTMLGlob("web/templates/*") + + router.Use(static.Serve("/", static.LocalFile("/web/*/*", true))) // Static files in ./uploads/ router.GET("/web", func(c *gin.Context) { - c.HTML(200, "index.tmpl", gin.H{}) + c.HTML(200, "index.njk", gin.H{}) }) router.GET("/web/stats", func(c *gin.Context) { files, _ := ioutil.ReadDir(os.Getenv("FOLDER")) - c.HTML(200, "stats.tmpl", gin.H{"fileCount": len(files), "uptime": time.Since(startTime)}) + c.HTML(200, "stats.njk", gin.H{"fileCount": len(files), "uptime": time.Since(startTime)}) }) } @@ -103,7 +105,7 @@ func setupRouter() *gin.Engine { func main() { // Create ./uploads if it doesn't already exist if _, err := os.Stat(os.Getenv("FOLDER")); os.IsNotExist(err) { - os.Mkdir(os.Getenv("FOLDER"), 777) + os.Mkdir(os.Getenv("FOLDER"), 0777) } // Start webserver diff --git a/templates/stats.tmpl b/templates/stats.tmpl deleted file mode 100644 index f182f8a..0000000 --- a/templates/stats.tmpl +++ /dev/null @@ -1,9 +0,0 @@ - - - -
-

Files: {{ .fileCount }}
- Uptime: {{ .uptime }}

-
- - \ No newline at end of file diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..2c90eb3 --- /dev/null +++ b/util/util.go @@ -0,0 +1,12 @@ +package util + +import ( + "crypto/rand" + "fmt" +) + +func RandomString(length int) string { + b := make([]byte, length) + rand.Read(b) + return fmt.Sprintf("%x", b)[:length] +} diff --git a/web/assets/.gitkeep b/web/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/templates/index.tmpl b/web/templates/index.njk similarity index 68% rename from templates/index.tmpl rename to web/templates/index.njk index e068de9..37442e7 100644 --- a/templates/index.tmpl +++ b/web/templates/index.njk @@ -1,5 +1,8 @@ + + gosharex +

Hello World

diff --git a/web/templates/stats.njk b/web/templates/stats.njk new file mode 100644 index 0000000..8cbdbf9 --- /dev/null +++ b/web/templates/stats.njk @@ -0,0 +1,12 @@ + + + + gosharex + + +
+

Files: {{ .fileCount }}
+ Uptime: {{ .uptime }}

+
+ + \ No newline at end of file