diff --git a/main.go b/main.go index 91eb0f7..f8c7828 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,8 @@ import ( var dbpool *pgxpool.Pool +var uploadFolder string + func main() { var err error dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres") @@ -48,6 +50,12 @@ func main() { } } + currentDir, err := os.Getwd() + if err != nil { + log.Fatalf("main: %v\n", err) + } + uploadFolder = fmt.Sprintf("%s/videos", currentDir) + router := gin.Default() router.SetTrustedProxies(nil) @@ -70,12 +78,6 @@ func initUpload(c *gin.Context) { return } - currentDir, err := os.Getwd() - if err != nil { - c.JSON(http.StatusInternalServerError, "") - log.Fatalf("initUpload: %v\n", err) - } - rows, _ := dbpool.Query(context.Background(), "select filepath from videos") for rows.Next() { var filepath string @@ -85,7 +87,7 @@ func initUpload(c *gin.Context) { log.Fatalf("initUpload: %v\n", err) } fmt.Println(filepath) - if filepath == fmt.Sprintf("%s/%s", currentDir, fileName) { + if filepath == fmt.Sprintf("%s/%s", uploadFolder, fileName) { c.JSON(http.StatusForbidden, "File already exists") return } @@ -106,7 +108,7 @@ func ReceiveChunk(c *gin.Context) { return } - f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + f, err := os.OpenFile(fmt.Sprintf("%s/%s", uploadFolder, c.GetHeader("file-name")), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { c.JSON(http.StatusInternalServerError, "") log.Fatal(err) @@ -124,19 +126,13 @@ func ReceiveChunk(c *gin.Context) { } func finishUpload(c *gin.Context) { - currentDir, err := os.Getwd() - if err != nil { - c.JSON(http.StatusInternalServerError, "") - log.Fatalf("finishUpload: %v\n", err) - } - fileName, err := io.ReadAll(c.Request.Body) if err != nil { c.JSON(http.StatusBadRequest, "Couldn't read html request body") return } - _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", currentDir, fileName)) + _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", uploadFolder, fileName)) if err != nil { c.JSON(http.StatusInternalServerError, "") log.Fatalf("finishUpload: %v\n", err)