Skip to content

Commit

Permalink
uploader to os-system cache
Browse files Browse the repository at this point in the history
  • Loading branch information
lovehunter9 committed Aug 16, 2024
1 parent 37712b5 commit 013e7ac
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cmd/upload/app/handler4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
"time"
)

const (
CachePathPrefix = "/appdata"
)

func getPVC(c *fiber.Ctx) (string, string, string, string, error) {
bflName := c.Get("X-Bfl-User")
klog.Info("BFL_NAME: ", bflName)
Expand Down Expand Up @@ -47,7 +51,7 @@ func getPVC(c *fiber.Ctx) (string, string, string, string, error) {

// UploadLink 处理上传链接的 GET 请求
func (a *appController) UploadLink(c *fiber.Ctx) error {
_, userPvc, _, uploadsDir, err := getPVC(c)
_, userPvc, cachePvc, uploadsDir, err := getPVC(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(
models.NewResponse(1, "bfl header missing or invalid", nil))
Expand All @@ -60,7 +64,11 @@ func (a *appController) UploadLink(c *fiber.Ctx) error {
models.NewResponse(1, "missing path query parameter", nil))
}

path = rewriteUrl(path, userPvc, "")
if strings.HasPrefix(path, CachePathPrefix) {
path = rewriteUrl(path, cachePvc, CachePathPrefix)
} else {
path = rewriteUrl(path, userPvc, "")
}

// 检查上传目录是否存在,如果不存在则创建
if !utils.PathExists(uploadsDir) {
Expand Down Expand Up @@ -94,7 +102,7 @@ func (a *appController) UploadLink(c *fiber.Ctx) error {
}

func (a *appController) UploadedBytes(c *fiber.Ctx) error {
_, userPvc, _, uploadsDir, err := getPVC(c)
_, userPvc, cachePvc, uploadsDir, err := getPVC(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(
models.NewResponse(1, "bfl header missing or invalid", nil))
Expand All @@ -106,7 +114,11 @@ func (a *appController) UploadedBytes(c *fiber.Ctx) error {
models.NewResponse(1, "missing parent_dir query parameter", nil))
}

parentDir = rewriteUrl(parentDir, userPvc, "")
if strings.HasPrefix(parentDir, CachePathPrefix) {
parentDir = rewriteUrl(parentDir, cachePvc, CachePathPrefix)
} else {
parentDir = rewriteUrl(parentDir, userPvc, "")
}

if !utils.CheckDirExist(parentDir) {
klog.Warningf("Storage path %s is not exist or is not a dir", parentDir)
Expand Down Expand Up @@ -165,7 +177,7 @@ func (a *appController) UploadedBytes(c *fiber.Ctx) error {
}

func (a *appController) UploadChunks(c *fiber.Ctx) error {
_, userPvc, _, uploadsDir, err := getPVC(c)
_, userPvc, cachePvc, uploadsDir, err := getPVC(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(
models.NewResponse(1, "bfl header missing or invalid", nil))
Expand Down Expand Up @@ -194,7 +206,12 @@ func (a *appController) UploadChunks(c *fiber.Ctx) error {
models.NewResponse(1, "param invalid", nil))
}

parentDir := rewriteUrl(resumableInfo.ParentDir, userPvc, "")
parentDir := resumableInfo.ParentDir
if strings.HasPrefix(parentDir, CachePathPrefix) {
parentDir = rewriteUrl(parentDir, cachePvc, CachePathPrefix)
} else {
parentDir = rewriteUrl(parentDir, userPvc, "")
}
if uploadID != uid.MakeUid(parentDir) {
return c.Status(fiber.StatusBadRequest).JSON(
models.NewResponse(1, "invalid upload link", nil))
Expand Down

0 comments on commit 013e7ac

Please sign in to comment.