add upload folder
This commit is contained in:
parent
7c782522f5
commit
f63932ddaf
26
main.go
26
main.go
@ -16,6 +16,8 @@ import (
|
|||||||
|
|
||||||
var dbpool *pgxpool.Pool
|
var dbpool *pgxpool.Pool
|
||||||
|
|
||||||
|
var uploadFolder string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres")
|
dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.19.0.2:5432/postgres")
|
||||||
@ -48,6 +50,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentDir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("main: %v\n", err)
|
||||||
|
}
|
||||||
|
uploadFolder = fmt.Sprintf("%s/videos", currentDir)
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.SetTrustedProxies(nil)
|
router.SetTrustedProxies(nil)
|
||||||
|
|
||||||
@ -70,12 +78,6 @@ func initUpload(c *gin.Context) {
|
|||||||
return
|
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")
|
rows, _ := dbpool.Query(context.Background(), "select filepath from videos")
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var filepath string
|
var filepath string
|
||||||
@ -85,7 +87,7 @@ func initUpload(c *gin.Context) {
|
|||||||
log.Fatalf("initUpload: %v\n", err)
|
log.Fatalf("initUpload: %v\n", err)
|
||||||
}
|
}
|
||||||
fmt.Println(filepath)
|
fmt.Println(filepath)
|
||||||
if filepath == fmt.Sprintf("%s/%s", currentDir, fileName) {
|
if filepath == fmt.Sprintf("%s/%s", uploadFolder, fileName) {
|
||||||
c.JSON(http.StatusForbidden, "File already exists")
|
c.JSON(http.StatusForbidden, "File already exists")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -106,7 +108,7 @@ func ReceiveChunk(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(c.GetHeader("file-name"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile(fmt.Sprintf("%s/%s", uploadFolder, c.GetHeader("file-name")), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, "")
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -124,19 +126,13 @@ func ReceiveChunk(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func finishUpload(c *gin.Context) {
|
func finishUpload(c *gin.Context) {
|
||||||
currentDir, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
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.JSON(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", uploadFolder, fileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, "")
|
c.JSON(http.StatusInternalServerError, "")
|
||||||
log.Fatalf("finishUpload: %v\n", err)
|
log.Fatalf("finishUpload: %v\n", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user