Skip to content

Commit

Permalink
uploader4: debug
Browse files Browse the repository at this point in the history
  • Loading branch information
lovehunter9 committed Aug 14, 2024
1 parent 9fa190b commit c9b9577
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/upload/app/handler4.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,28 @@ func (a *appController) UploadChunks(c *fiber.Ctx) error {
models.NewResponse(1, "Unsupported file size", nil))
}

// 获取请求头中的Content-Range字段
ranges := c.Get("Content-Range")
// 如果存在Content-Range,则解析并设置fsm的相关字段
var offset int64
var parsed bool
if ranges != "" {
offset, parsed = fileutils.ParseContentRange(ranges)
if !parsed {
return c.Status(fiber.StatusBadRequest).JSON(
models.NewResponse(1, "Invalid content range", nil))
}
}

// Write the file contents to the file at the specified path
fileSize, err := fileutils.SaveFile(fileHeader, fileutils.GetTempFilePathById(innerIdentifier))
writtenSize, err := fileutils.SaveFile4(fileHeader, fileutils.GetTempFilePathById(innerIdentifier), offset)
if err != nil {
klog.Warningf("innerIdentifier:%s, info:%+v, err:%v", innerIdentifier, info, err)
return c.Status(fiber.StatusInternalServerError).JSON(
models.NewResponse(1, err.Error(), info))
}

info.Offset = fileSize
info.Offset += writtenSize
a.server.fileInfoMgr.UpdateInfo(innerIdentifier, info)

// Update file information for debug
Expand All @@ -325,7 +338,7 @@ func (a *appController) UploadChunks(c *fiber.Ctx) error {
}

// Check if the file has been written
if fileSize == info.FileSize {
if info.Offset == info.FileSize {
// Move the file to the specified upload path
err = fileutils.MoveFileByInfo(info)
if err != nil {
Expand Down
67 changes: 67 additions & 0 deletions pkg/upload/fileutils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,73 @@ func SaveFile(fileHeader *multipart.FileHeader, filePath string) (int64, error)
return fileSize, nil
}

func ParseContentRange(ranges string) (int64, bool) {
start := strings.Index(ranges, "bytes")
end := strings.Index(ranges, "-")
slash := strings.Index(ranges, "/")

if start < 0 || end < 0 || slash < 0 {
return -1, false
}

startStr := strings.TrimLeft(ranges[start+len("bytes"):end], " ")
firstByte, err := strconv.ParseInt(startStr, 10, 64)
if err != nil {
return -1, false
}

lastByte, err := strconv.ParseInt(ranges[end+1:slash], 10, 64)
if err != nil {
return -1, false
}

fileSize, err := strconv.ParseInt(ranges[slash+1:], 10, 64)
if err != nil {
return -1, false
}

if firstByte > lastByte || lastByte >= fileSize {
return -1, false
}

//fsm.rstart = firstByte
//fsm.rend = lastByte
//fsm.fsize = fileSize

return firstByte, true
}

func SaveFile4(fileHeader *multipart.FileHeader, filePath string, offset int64) (int64, error) {
// 打开源文件
sourceFile, err := fileHeader.Open()
if err != nil {
return 0, err
}
defer sourceFile.Close()

// 打开目标文件准备写入
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return 0, err
}
defer dstFile.Close()

// 移动文件指针到指定的偏移量位置
_, err = dstFile.Seek(offset, io.SeekStart)
if err != nil {
return 0, err
}

// 写入源文件内容到目标文件的指定位置
n, err := io.Copy(dstFile, sourceFile)
if err != nil {
return 0, err
}

// 返回写入的字节数和nil错误(如果没有发生错误)
return n, nil
}

func UpdateFileInfo(fileInfo models.FileInfo) error {
// Construct file information path
infoPath := filepath.Join(UploadsDir, fileInfo.ID+".info")
Expand Down

0 comments on commit c9b9577

Please sign in to comment.