-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
65 lines (53 loc) · 1.32 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package upload
import (
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
)
func init() {
err := os.MkdirAll("./uploads/chunks", os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
type FileController struct {
svc UploadService
}
func (c *FileController) NewUpload(ctx *gin.Context) {
upload := c.svc.CreateUpload()
ctx.JSON(http.StatusOK, upload)
}
func (c *FileController) UploadChunk(ctx *gin.Context) {
chunk, err := c.svc.UploadChunk(ctx.Request)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, chunk)
}
func (c *FileController) Reassemble(ctx *gin.Context) {
var req ReassembleChunksRequest
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
req.UploadId = ctx.Param("id")
file, err := c.svc.ReassembleChunk(req)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Return a success message and the file metadata
ctx.JSON(http.StatusOK, file)
}
func (c *FileController) Download(ctx *gin.Context) {
var req DownloadRequest
req.UploadId = ctx.Param("id")
err := c.svc.Download(req, ctx.Writer.Header(), ctx.Writer)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
}