diff --git a/main.go b/main.go index cf8abcf..91eb0f7 100644 --- a/main.go +++ b/main.go @@ -51,8 +51,9 @@ func main() { router := gin.Default() router.SetTrustedProxies(nil) - router.POST("/video", ReceiveChunk) - router.POST("/video/completed", ReceiveFile) + router.POST("/video/init", initUpload) + router.POST("/video/chunk", ReceiveChunk) + router.POST("/video/completed", finishUpload) router.GET("/videos", listVideos) router.GET("/videos/:id", getVideo) router.DELETE("/videos/:id", deleteVideo) @@ -60,45 +61,88 @@ func main() { router.Run("localhost:8080") } +func initUpload(c *gin.Context) { + var err error + + fileName, err := io.ReadAll(c.Request.Body) + if err != nil { + c.JSON(http.StatusBadRequest, "Couldn't read html request body") + 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 + err = rows.Scan(&filepath) + if err != nil { + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("initUpload: %v\n", err) + } + fmt.Println(filepath) + if filepath == fmt.Sprintf("%s/%s", currentDir, fileName) { + c.JSON(http.StatusForbidden, "File already exists") + return + } + } + err = rows.Err() + if err != nil { + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("initUpload: %v\n", err) + } + + c.JSON(http.StatusOK, "Upload can proceed") +} + func ReceiveChunk(c *gin.Context) { chunk, err := io.ReadAll(c.Request.Body) if err != nil { - c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body") + c.JSON(http.StatusBadRequest, "Couldn't read html request body") return } f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatal(err) } if _, err := f.Write(chunk); err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatal(err) } if err := f.Close(); err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatal(err) } - c.IndentedJSON(http.StatusOK, "Received chunk") + c.JSON(http.StatusOK, "Received chunk") } -func ReceiveFile(c *gin.Context) { +func finishUpload(c *gin.Context) { currentDir, err := os.Getwd() if err != nil { - log.Fatalf("ReceiveFile: %v\n", err) + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("finishUpload: %v\n", err) } fileName, err := io.ReadAll(c.Request.Body) if err != nil { - c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body") + 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)) if err != nil { - log.Fatalf("ReceiveFile: %v\n", err) + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("finishUpload: %v\n", err) } - c.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos + c.JSON(http.StatusOK, gin.H{}) // return list of videos } func listVideos(c *gin.Context) { @@ -109,16 +153,18 @@ func listVideos(c *gin.Context) { var filepath string err = rows.Scan(&id, &filepath) if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatalf("listVideos: %v\n", err) } fmt.Println(id, filepath) } err = rows.Err() if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatalf("listVideos: %v\n", err) } - c.IndentedJSON(http.StatusOK, gin.H{}) + c.JSON(http.StatusOK, gin.H{}) } func getVideo(c *gin.Context) { @@ -134,15 +180,17 @@ func getVideo(c *gin.Context) { var filepath string err = rows.Scan(&id, &filepath) if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatalf("getVideo: %v\n", err) } fmt.Println(id, filepath) err = rows.Err() if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Fatalf("getVideo: %v\n", err) } - c.IndentedJSON(http.StatusOK, gin.H{}) + c.JSON(http.StatusOK, gin.H{}) } func deleteVideo(c *gin.Context) { @@ -157,29 +205,27 @@ func deleteVideo(c *gin.Context) { err = rows.Scan(&filepath) if err != nil { log.Printf("deleteVideo: %v\n", err) - c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Video does not exist"}) + c.JSON(http.StatusBadRequest, "Video does not exist") return } fmt.Println(filepath) err = rows.Err() if err != nil { log.Printf("deleteVideo: %v\n", err) - c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Scanning the row didn't work"}) + c.JSON(http.StatusInternalServerError, "Scanning the row didn't work") return } _, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId) if err != nil { - log.Printf("deleteVideo: %v\n", err) - c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete entry from DB"}) - return + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("deleteVideo: %v\n", err) } if err = os.Remove(filepath); err != nil { - log.Printf("deleteVideo: %v\n", err) - c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete video from server"}) - return + c.JSON(http.StatusInternalServerError, "") + log.Fatalf("deleteVideo: %v\n", err) } - c.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos + c.JSON(http.StatusOK, gin.H{}) // return list of videos }