/
file.go
86 lines (73 loc) · 2.11 KB
/
file.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package v2
import (
"net/http"
"strconv"
"github.com/IceWhaleTech/CasaOS/codegen"
"github.com/labstack/echo/v4"
)
// Path: route/v2/file.go
func (s *CasaOS) GetFileTest(ctx echo.Context) error {
//http.ServeFile(w, r, r.URL.Path[1:])
http.ServeFile(ctx.Response().Writer, ctx.Request(), "/DATA/test.img")
return ctx.String(200, "pong")
}
func (c *CasaOS) CheckUploadChunk(ctx echo.Context, params codegen.CheckUploadChunkParams) error {
identifier := ctx.QueryParam("identifier")
chunkNumber, err := strconv.ParseInt(ctx.QueryParam("chunkNumber"), 10, 64)
if err != nil {
return ctx.NoContent(http.StatusBadRequest)
}
err = c.fileUploadService.TestChunk(ctx, identifier, chunkNumber)
if err != nil {
return ctx.NoContent(http.StatusNoContent)
}
return ctx.NoContent(http.StatusOK)
}
func (c *CasaOS) PostUploadFile(ctx echo.Context) error {
path := ctx.FormValue("path")
// handle the request
chunkNumber, err := strconv.ParseInt(ctx.FormValue("chunkNumber"), 10, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
chunkSize, err := strconv.ParseInt(ctx.FormValue("chunkSize"), 10, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
currentChunkSize, err := strconv.ParseInt(ctx.FormValue("currentChunkSize"), 10, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
totalChunks, err := strconv.ParseInt(ctx.FormValue("totalChunks"), 10, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
totalSize, err := strconv.ParseInt(ctx.FormValue("totalSize"), 10, 64)
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
identifier := ctx.FormValue("identifier")
fileName := ctx.FormValue("filename")
relativePath := ctx.FormValue("relativePath")
bin, err := ctx.FormFile("file")
if err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}
err = c.fileUploadService.UploadFile(
ctx,
path,
chunkNumber,
chunkSize,
currentChunkSize,
totalChunks,
totalSize,
identifier,
relativePath,
fileName,
bin,
)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, err)
}
return ctx.NoContent(http.StatusOK)
}