finish initUpload
This commit is contained in:
parent
bc278868da
commit
7c782522f5
86
main.go
86
main.go
@ -51,8 +51,9 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.SetTrustedProxies(nil)
|
router.SetTrustedProxies(nil)
|
||||||
|
|
||||||
router.POST("/video", ReceiveChunk)
|
router.POST("/video/init", initUpload)
|
||||||
router.POST("/video/completed", ReceiveFile)
|
router.POST("/video/chunk", ReceiveChunk)
|
||||||
|
router.POST("/video/completed", finishUpload)
|
||||||
router.GET("/videos", listVideos)
|
router.GET("/videos", listVideos)
|
||||||
router.GET("/videos/:id", getVideo)
|
router.GET("/videos/:id", getVideo)
|
||||||
router.DELETE("/videos/:id", deleteVideo)
|
router.DELETE("/videos/:id", deleteVideo)
|
||||||
@ -60,45 +61,88 @@ func main() {
|
|||||||
router.Run("localhost:8080")
|
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) {
|
func ReceiveChunk(c *gin.Context) {
|
||||||
chunk, err := io.ReadAll(c.Request.Body)
|
chunk, err := io.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
|
c.JSON(http.StatusBadRequest, "Couldn't read html request body")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if _, err := f.Write(chunk); err != nil {
|
if _, err := f.Write(chunk); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatal(err)
|
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()
|
currentDir, err := os.Getwd()
|
||||||
if err != nil {
|
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)
|
fileName, err := io.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
|
c.JSON(http.StatusBadRequest, "Couldn't read html request body")
|
||||||
return
|
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", currentDir, fileName))
|
||||||
if err != nil {
|
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) {
|
func listVideos(c *gin.Context) {
|
||||||
@ -109,16 +153,18 @@ func listVideos(c *gin.Context) {
|
|||||||
var filepath string
|
var filepath string
|
||||||
err = rows.Scan(&id, &filepath)
|
err = rows.Scan(&id, &filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatalf("listVideos: %v\n", err)
|
log.Fatalf("listVideos: %v\n", err)
|
||||||
}
|
}
|
||||||
fmt.Println(id, filepath)
|
fmt.Println(id, filepath)
|
||||||
}
|
}
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatalf("listVideos: %v\n", err)
|
log.Fatalf("listVideos: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{})
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVideo(c *gin.Context) {
|
func getVideo(c *gin.Context) {
|
||||||
@ -134,15 +180,17 @@ func getVideo(c *gin.Context) {
|
|||||||
var filepath string
|
var filepath string
|
||||||
err = rows.Scan(&id, &filepath)
|
err = rows.Scan(&id, &filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatalf("getVideo: %v\n", err)
|
log.Fatalf("getVideo: %v\n", err)
|
||||||
}
|
}
|
||||||
fmt.Println(id, filepath)
|
fmt.Println(id, filepath)
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatalf("getVideo: %v\n", err)
|
log.Fatalf("getVideo: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{})
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteVideo(c *gin.Context) {
|
func deleteVideo(c *gin.Context) {
|
||||||
@ -157,29 +205,27 @@ func deleteVideo(c *gin.Context) {
|
|||||||
err = rows.Scan(&filepath)
|
err = rows.Scan(&filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("deleteVideo: %v\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(filepath)
|
fmt.Println(filepath)
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("deleteVideo: %v\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
|
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("deleteVideo: %v\n", err)
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete entry from DB"})
|
log.Fatalf("deleteVideo: %v\n", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Remove(filepath); err != nil {
|
if err = os.Remove(filepath); err != nil {
|
||||||
log.Printf("deleteVideo: %v\n", err)
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Couldn't delete video from server"})
|
log.Fatalf("deleteVideo: %v\n", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos
|
c.JSON(http.StatusOK, gin.H{}) // return list of videos
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user