chore: extract controller and service layer

This commit is contained in:
CorrectRoadH 2023-12-27 14:28:42 +08:00
parent cd7a46f10c
commit 9bfdc54545
2 changed files with 91 additions and 59 deletions

View file

@ -2,6 +2,7 @@ package v2
import (
"net/http"
"strconv"
"github.com/IceWhaleTech/CasaOS/codegen"
"github.com/labstack/echo/v4"
@ -18,9 +19,66 @@ func (s *CasaOS) GetFileTest(ctx echo.Context) error {
}
func (c *CasaOS) CheckUploadChunk(ctx echo.Context, params codegen.CheckUploadChunkParams) error {
return c.fileUploadService.TestChunk(ctx)
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 {
return c.fileUploadService.UploadFile(ctx)
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")
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,
fileName,
bin,
)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, err)
}
return ctx.NoContent(http.StatusOK)
}

View file

@ -3,9 +3,8 @@ package service
import (
"fmt"
"io"
"net/http"
"mime/multipart"
"os"
"strconv"
"sync"
"github.com/labstack/echo/v4"
@ -29,68 +28,44 @@ func NewFileUploadService() *FileUploadService {
}
}
func (s *FileUploadService) TestChunk(c echo.Context) error {
// s.lock.RLock()
// defer s.lock.RUnlock()
identifier := c.QueryParam("identifier")
chunkNumber, err := strconv.ParseInt(c.QueryParam("chunkNumber"), 10, 64)
if err != nil {
return err
}
func (s *FileUploadService) TestChunk(
c echo.Context,
identifier string,
chunkNumber int64,
) error {
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
if !ok {
return c.NoContent(http.StatusNoContent)
return fmt.Errorf("file not found")
}
fileInfo := fileInfoTemp.(*FileInfo)
if !fileInfo.init {
return c.NoContent(http.StatusNoContent)
return fmt.Errorf("file not init")
}
// 这里返回的应该得是 permanentErrors不是 404. 不然前端会上传失败而不是重传块。
// 梁哥应该得改一下。
// return StatusNoContent instead of 404
// the is require by frontend
if !fileInfo.uploaded[chunkNumber-1] {
return c.NoContent(http.StatusNoContent)
return fmt.Errorf("file not found")
}
return c.NoContent(http.StatusOK)
return nil
}
func (s *FileUploadService) UploadFile(c echo.Context) error {
path := c.FormValue("path")
// handle the request
chunkNumber, err := strconv.ParseInt(c.FormValue("chunkNumber"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
chunkSize, err := strconv.ParseInt(c.FormValue("chunkSize"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
currentChunkSize, err := strconv.ParseInt(c.FormValue("currentChunkSize"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
totalChunks, err := strconv.ParseInt(c.FormValue("totalChunks"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
totalSize, err := strconv.ParseInt(c.FormValue("totalSize"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
identifier := c.FormValue("identifier")
fileName := c.FormValue("filename")
bin, err := c.FormFile("file")
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
func (s *FileUploadService) UploadFile(
c echo.Context,
path string,
chunkNumber int64,
chunkSize int64,
currentChunkSize int64,
totalChunks int64,
totalSize int64,
identifier string,
fileName string,
bin *multipart.FileHeader,
) error {
s.lock.Lock()
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
var fileInfo *FileInfo
@ -98,22 +73,21 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
file, err := os.OpenFile(path+"/"+fileName+".tmp", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
s.lock.Unlock()
return c.JSON(http.StatusInternalServerError, err)
return err
}
if !ok {
// file, err := os.Create(path + "/" + fileName + ".tmp")
if err != nil {
s.lock.Unlock()
return c.JSON(http.StatusInternalServerError, err)
return err
}
// pre allocate file size
fmt.Println("truncate", totalSize)
if err != nil {
s.lock.Unlock()
return c.JSON(http.StatusInternalServerError, err)
return err
}
// file info init
@ -131,12 +105,12 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
_, err = file.Seek((chunkNumber-1)*chunkSize, io.SeekStart)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
return err
}
src, err := bin.Open()
if err != nil {
return c.JSON(http.StatusInternalServerError, err)
return err
}
defer src.Close()
@ -145,7 +119,7 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
if err != nil {
fmt.Println(err)
return c.JSON(http.StatusInternalServerError, err)
return err
}
s.lock.Lock()
@ -166,5 +140,5 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
}
s.lock.Unlock()
return c.NoContent(http.StatusOK)
return nil
}