Uploader/main.go

186 lines
4.2 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
func main() {
2024-05-19 00:19:47 +02:00
var err error
dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2: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
router := gin.Default()
router.SetTrustedProxies(nil)
2024-05-19 00:19:47 +02:00
router.POST("/video", ReceiveChunk)
router.POST("/video/completed", ReceiveFile)
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-05-19 00:19:47 +02:00
func ReceiveChunk(c *gin.Context) {
chunk, err := io.ReadAll(c.Request.Body)
if err != nil {
2024-06-01 13:58:16 +02:00
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
2024-05-18 14:36:44 +02:00
return
}
2024-06-01 13:58:16 +02:00
f, err := os.OpenFile(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 {
log.Fatal(err)
}
2024-05-19 00:19:47 +02:00
if _, err := f.Write(chunk); err != nil {
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 {
log.Fatal(err)
}
c.IndentedJSON(http.StatusOK, "Received chunk")
}
2024-05-19 00:19:47 +02:00
func ReceiveFile(c *gin.Context) {
currentDir, err := os.Getwd()
if err != nil {
2024-06-01 13:58:16 +02:00
log.Fatalf("ReceiveFile: %v\n", err)
2024-05-19 00:19:47 +02:00
}
2024-05-19 00:19:47 +02:00
fileName, err := io.ReadAll(c.Request.Body)
if err != nil {
2024-06-01 13:58:16 +02:00
c.IndentedJSON(http.StatusBadRequest, "Couldn't read html request body")
return
}
2024-06-01 13:58:16 +02:00
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", currentDir, fileName))
2024-05-19 00:19:47 +02:00
if err != nil {
2024-06-01 13:58:16 +02:00
log.Fatalf("ReceiveFile: %v\n", err)
2024-02-23 23:23:06 +01:00
}
2024-06-01 13:58:16 +02:00
c.IndentedJSON(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-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-01 13:58:16 +02:00
log.Fatalf("listVideos: %v\n", err)
}
2024-06-01 13:58:16 +02:00
c.IndentedJSON(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 {
log.Fatalf("getVideo: %v\n", err)
}
fmt.Println(id, filepath)
err = rows.Err()
if err != nil {
log.Fatalf("getVideo: %v\n", err)
}
c.IndentedJSON(http.StatusOK, gin.H{})
}
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)
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Video does not exist"})
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)
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "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
}
2024-06-01 13:58:16 +02:00
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.IndentedJSON(http.StatusOK, gin.H{}) // return list of videos
}