mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-11-06 22:59:44 +00:00
chore: extract controller and service layer
This commit is contained in:
parent
cd7a46f10c
commit
9bfdc54545
@ -2,6 +2,7 @@ package v2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/IceWhaleTech/CasaOS/codegen"
|
"github.com/IceWhaleTech/CasaOS/codegen"
|
||||||
"github.com/labstack/echo/v4"
|
"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 {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,9 +3,8 @@ package service
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
@ -29,68 +28,44 @@ func NewFileUploadService() *FileUploadService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *FileUploadService) TestChunk(c echo.Context) error {
|
func (s *FileUploadService) TestChunk(
|
||||||
// s.lock.RLock()
|
c echo.Context,
|
||||||
// defer s.lock.RUnlock()
|
identifier string,
|
||||||
|
chunkNumber int64,
|
||||||
identifier := c.QueryParam("identifier")
|
) error {
|
||||||
chunkNumber, err := strconv.ParseInt(c.QueryParam("chunkNumber"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return c.NoContent(http.StatusNoContent)
|
return fmt.Errorf("file not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
fileInfo := fileInfoTemp.(*FileInfo)
|
fileInfo := fileInfoTemp.(*FileInfo)
|
||||||
|
|
||||||
if !fileInfo.init {
|
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] {
|
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 {
|
func (s *FileUploadService) UploadFile(
|
||||||
path := c.FormValue("path")
|
c echo.Context,
|
||||||
|
path string,
|
||||||
// handle the request
|
chunkNumber int64,
|
||||||
chunkNumber, err := strconv.ParseInt(c.FormValue("chunkNumber"), 10, 64)
|
chunkSize int64,
|
||||||
if err != nil {
|
currentChunkSize int64,
|
||||||
return c.JSON(http.StatusBadRequest, err)
|
totalChunks int64,
|
||||||
}
|
totalSize int64,
|
||||||
chunkSize, err := strconv.ParseInt(c.FormValue("chunkSize"), 10, 64)
|
identifier string,
|
||||||
if err != nil {
|
fileName string,
|
||||||
return c.JSON(http.StatusBadRequest, err)
|
bin *multipart.FileHeader,
|
||||||
}
|
) error {
|
||||||
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)
|
|
||||||
}
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
fileInfoTemp, ok := s.uploadStatus.Load(identifier)
|
||||||
var fileInfo *FileInfo
|
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)
|
file, err := os.OpenFile(path+"/"+fileName+".tmp", os.O_WRONLY|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
// file, err := os.Create(path + "/" + fileName + ".tmp")
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// pre allocate file size
|
// pre allocate file size
|
||||||
fmt.Println("truncate", totalSize)
|
fmt.Println("truncate", totalSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// file info init
|
// file info init
|
||||||
@ -131,12 +105,12 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
|
|||||||
|
|
||||||
_, err = file.Seek((chunkNumber-1)*chunkSize, io.SeekStart)
|
_, err = file.Seek((chunkNumber-1)*chunkSize, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
src, err := bin.Open()
|
src, err := bin.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
@ -145,7 +119,7 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return c.JSON(http.StatusInternalServerError, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
@ -166,5 +140,5 @@ func (s *FileUploadService) UploadFile(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
|
|
||||||
return c.NoContent(http.StatusOK)
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user