2024-02-14 00:56:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-05-18 14:36:44 +02:00
|
|
|
"context"
|
2024-03-04 16:26:34 +01:00
|
|
|
"fmt"
|
2024-05-17 00:59:46 +02:00
|
|
|
"io"
|
|
|
|
"log"
|
2024-02-14 00:56:11 +01:00
|
|
|
"net/http"
|
2024-05-17 00:59:46 +02:00
|
|
|
"os"
|
2024-03-04 16:26:34 +01:00
|
|
|
"strconv"
|
2024-02-14 00:56:11 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-05-18 14:36:44 +02:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2024-03-04 16:26:34 +01:00
|
|
|
"github.com/jaswdr/faker"
|
2024-02-14 00:56:11 +01:00
|
|
|
)
|
|
|
|
|
2024-05-19 00:19:47 +02:00
|
|
|
var dbpool *pgxpool.Pool
|
2024-06-03 19:44:53 +02:00
|
|
|
var uploadFolder string
|
|
|
|
|
2024-02-14 00:56:11 +01:00
|
|
|
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-17 00:59:46 +02:00
|
|
|
}
|
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)
|
2024-05-17 00:59:46 +02:00
|
|
|
if err != nil {
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("main: %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-03 21:19:36 +02:00
|
|
|
log.Panicf("main: %v\n", err)
|
2024-05-18 14:36:44 +02:00
|
|
|
}
|
2024-05-17 00:59:46 +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 {
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("main: %v\n", err)
|
2024-06-03 19:44:53 +02:00
|
|
|
}
|
|
|
|
uploadFolder = fmt.Sprintf("%s/videos", currentDir)
|
|
|
|
|
2024-02-14 00:56:11 +01:00
|
|
|
router := gin.Default()
|
2024-02-23 23:57:41 +01:00
|
|
|
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)
|
2024-02-14 00:56:11 +01:00
|
|
|
|
|
|
|
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, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("initUpload: %v\n", err)
|
2024-06-03 19:36:41 +02:00
|
|
|
}
|
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, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("initUpload: %v\n", err)
|
2024-06-03 19:36:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-05-17 00:59:46 +02:00
|
|
|
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-05-17 00:59:46 +02:00
|
|
|
|
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-06-03 22:15:43 +02:00
|
|
|
log.Panicf("receiveChunk: %v\n", err)
|
2024-05-18 14:36:44 +02:00
|
|
|
}
|
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-06-03 22:15:43 +02:00
|
|
|
log.Panicf("receiveChunk: %v\n", err)
|
2024-05-17 00:59:46 +02:00
|
|
|
}
|
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-06-03 22:15:43 +02:00
|
|
|
log.Panicf("receiveChunk: %v\n", err)
|
2024-05-18 14:36:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 19:36:41 +02:00
|
|
|
c.JSON(http.StatusOK, "Received chunk")
|
2024-04-14 20:52:06 +02:00
|
|
|
}
|
|
|
|
|
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")
|
2024-02-14 00:56:11 +01:00
|
|
|
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, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("finishUpload: %v\n", err)
|
2024-02-23 23:23:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-03 22:15:43 +02:00
|
|
|
c.JSON(http.StatusOK, "File uploaded successfully")
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
|
|
|
|
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-03 21:19:36 +02:00
|
|
|
log.Panicf("listVideos: %v\n", err)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
2024-05-19 00:19:47 +02:00
|
|
|
fmt.Println(id, filepath)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
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-03 21:19:36 +02:00
|
|
|
log.Panicf("listVideos: %v\n", err)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
2024-06-01 13:58:16 +02:00
|
|
|
|
2024-06-03 21:19:36 +02:00
|
|
|
allVideos := gin.H{}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, allVideos)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("getVideo: %v\n", err)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
|
|
|
|
2024-06-03 21:19:36 +02:00
|
|
|
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
|
2024-06-01 13:58:16 +02:00
|
|
|
rows.Next()
|
|
|
|
var filepath string
|
2024-06-03 21:19:36 +02:00
|
|
|
err = rows.Scan(&filepath)
|
2024-06-01 13:58:16 +02:00
|
|
|
if err != nil {
|
2024-06-03 19:36:41 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("getVideo: %v\n", err)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
2024-06-03 19:36:41 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("getVideo: %v\n", err)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 22:15:43 +02:00
|
|
|
c.JSON(http.StatusOK, filepath)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteVideo(c *gin.Context) {
|
|
|
|
inputId, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("deleteVideo: %v\n", err)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-06-03 19:36:41 +02:00
|
|
|
c.JSON(http.StatusBadRequest, "Video does not exist")
|
2024-06-03 22:15:43 +02:00
|
|
|
log.Panicf("deleteVideo: %v\n", err)
|
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-03 22:15:43 +02:00
|
|
|
c.JSON(http.StatusInternalServerError, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("deleteVideo: %v\n", err)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, 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, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("deleteVideo: %v\n", err)
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|
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, "")
|
2024-06-03 21:19:36 +02:00
|
|
|
log.Panicf("deleteVideo: %v\n", err)
|
2024-06-01 13:58:16 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 22:15:43 +02:00
|
|
|
c.JSON(http.StatusOK, "File deleted successfully")
|
2024-02-14 00:56:11 +01:00
|
|
|
}
|