Uploader/main.go

227 lines
5.3 KiB
Go
Raw Normal View History

package main
import (
2024-05-18 14:36:44 +02:00
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
2024-05-18 14:36:44 +02:00
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jaswdr/faker"
)
2024-05-19 00:19:47 +02:00
var dbpool *pgxpool.Pool
2024-06-03 19:44:53 +02:00
var uploadFolder string
func main() {
2024-05-19 00:19:47 +02:00
var err error
2024-06-03 21:04:50 +02:00
dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.3:5432/postgres")
2024-05-18 14:36:44 +02:00
if err != nil {
log.Fatal(err)
}
2024-05-18 14:36:44 +02:00
defer dbpool.Close()
sqlStmt := `
2024-06-01 13:58:16 +02:00
DROP TABLE IF EXISTS videos;
2024-05-18 14:36:44 +02:00
2024-06-01 13:58:16 +02:00
CREATE TABLE IF NOT EXISTS videos
2024-05-18 14:36:44 +02:00
(
2024-05-19 00:19:47 +02:00
id serial NOT NULL,
2024-05-18 14:36:44 +02:00
filepath text,
CONSTRAINT videos_pkey PRIMARY KEY (id)
)
`
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil {
2024-06-01 13:58:16 +02:00
log.Fatalf("main: create table: %v\n", err)
2024-05-18 14:36:44 +02:00
}
faker := faker.New()
for i := 0; i < 10; i++ {
2024-06-01 13:58:16 +02:00
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", faker.File().AbsoluteFilePathForUnix(2))
2024-05-18 14:36:44 +02:00
if err != nil {
2024-06-01 13:58:16 +02:00
log.Fatalf("main: fill with fake data: %v\n", err)
2024-05-18 14:36:44 +02:00
}
}
2024-05-18 14:36:44 +02:00
2024-06-03 19:44:53 +02:00
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)
2024-06-03 19:36:41 +02:00
router.POST("/video/init", initUpload)
2024-06-03 21:04:50 +02:00
router.POST("/video/chunk", receiveChunk)
2024-06-03 19:36:41 +02:00
router.POST("/video/completed", finishUpload)
2024-05-19 00:19:47 +02:00
router.GET("/videos", listVideos)
router.GET("/videos/:id", getVideo)
2024-06-01 13:58:16 +02:00
router.DELETE("/videos/:id", deleteVideo)
router.Run("localhost:8080")
}
2024-06-03 19:36:41 +02:00
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
}
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)
}
2024-06-03 19:44:53 +02:00
if filepath == fmt.Sprintf("%s/%s", uploadFolder, fileName) {
2024-06-03 19:36:41 +02:00
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")
}
2024-06-03 21:04:50 +02:00
func receiveChunk(c *gin.Context) {
2024-05-19 00:19:47 +02:00
chunk, err := io.ReadAll(c.Request.Body)
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusBadRequest, "Couldn't read html request body")
2024-05-18 14:36:44 +02:00
return
}
2024-06-03 19:44:53 +02:00
f, err := os.OpenFile(fmt.Sprintf("%s/%s", uploadFolder, c.GetHeader("file-name")), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2024-05-18 14:36:44 +02:00
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-05-18 14:36:44 +02:00
log.Fatal(err)
}
2024-05-19 00:19:47 +02:00
if _, err := f.Write(chunk); err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-05-18 14:36:44 +02:00
log.Fatal(err)
}
2024-05-18 14:36:44 +02:00
if err := f.Close(); err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-05-18 14:36:44 +02:00
log.Fatal(err)
}
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusOK, "Received chunk")
}
2024-06-03 19:36:41 +02:00
func finishUpload(c *gin.Context) {
2024-05-19 00:19:47 +02:00
fileName, err := io.ReadAll(c.Request.Body)
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusBadRequest, "Couldn't read html request body")
return
}
2024-06-03 19:44:53 +02:00
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", uploadFolder, fileName))
2024-05-19 00:19:47 +02:00
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("finishUpload: %v\n", err)
2024-02-23 23:23:06 +01:00
}
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusOK, gin.H{}) // return list of videos
}
2024-05-19 00:19:47 +02:00
func listVideos(c *gin.Context) {
var err error
rows, _ := dbpool.Query(context.Background(), "select * from videos")
for rows.Next() {
var id int
var filepath string
err = rows.Scan(&id, &filepath)
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-06-01 13:58:16 +02:00
log.Fatalf("listVideos: %v\n", err)
}
2024-05-19 00:19:47 +02:00
fmt.Println(id, filepath)
}
2024-05-19 00:19:47 +02:00
err = rows.Err()
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-06-01 13:58:16 +02:00
log.Fatalf("listVideos: %v\n", err)
}
2024-06-01 13:58:16 +02:00
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusOK, gin.H{})
}
2024-05-19 00:19:47 +02:00
func getVideo(c *gin.Context) {
var err error
inputId, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Fatal(err)
}
2024-06-01 13:58:16 +02:00
rows, _ := dbpool.Query(context.Background(), "select * from videos where id = $1", inputId)
rows.Next()
var id int
var filepath string
err = rows.Scan(&id, &filepath)
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-06-01 13:58:16 +02:00
log.Fatalf("getVideo: %v\n", err)
}
fmt.Println(id, filepath)
err = rows.Err()
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
2024-06-01 13:58:16 +02:00
log.Fatalf("getVideo: %v\n", err)
}
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusOK, gin.H{})
2024-06-01 13:58:16 +02:00
}
func deleteVideo(c *gin.Context) {
inputId, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Fatal(err)
}
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
rows.Next()
var filepath string
err = rows.Scan(&filepath)
if err != nil {
log.Printf("deleteVideo: %v\n", err)
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusBadRequest, "Video does not exist")
2024-06-01 13:58:16 +02:00
return
2024-05-19 00:19:47 +02:00
}
2024-06-01 13:58:16 +02:00
fmt.Println(filepath)
2024-05-19 00:19:47 +02:00
err = rows.Err()
if err != nil {
2024-06-01 13:58:16 +02:00
log.Printf("deleteVideo: %v\n", err)
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "Scanning the row didn't work")
2024-06-01 13:58:16 +02:00
return
}
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
if err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("deleteVideo: %v\n", err)
}
2024-06-01 13:58:16 +02:00
if err = os.Remove(filepath); err != nil {
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("deleteVideo: %v\n", err)
2024-06-01 13:58:16 +02:00
}
2024-06-03 19:36:41 +02:00
c.JSON(http.StatusOK, gin.H{}) // return list of videos
}