Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherr pick fix bug of hang up to download empty file #157

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions api/handler/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,17 @@ func (h *RepoHandler) HeadSDKDownload(ctx *gin.Context) {

func (h *RepoHandler) handleDownload(ctx *gin.Context, isResolve bool) {
currentUser := httpbase.GetCurrentUser(ctx)
var branch string
namespace, name, err := common.GetNamespaceAndNameFromContext(ctx)
var (
namespace string
name string
branch string
reader io.ReadCloser
size int64
url string
contentLength int64
err error
)
namespace, name, err = common.GetNamespaceAndNameFromContext(ctx)
if err != nil {
slog.Error("Bad request format", "error", err)
httpbase.BadRequest(ctx, err.Error())
Expand Down Expand Up @@ -856,7 +865,7 @@ func (h *RepoHandler) handleDownload(ctx *gin.Context, isResolve bool) {
RepoType: common.RepoTypeFromContext(ctx),
}
// TODO:move the check into SDKDownloadFile, and can return the file content as we get all the content before check lfs pointer
lfs, err := h.c.IsLfs(ctx, req)
lfs, contentLength, err := h.c.IsLfs(ctx, req)
if err != nil {
if errors.Is(err, component.ErrNotFound) {
slog.Error("repo not found", slog.String("repo_type", string(common.RepoTypeFromContext(ctx))), slog.Any("path", fmt.Sprintf("%s/%s", namespace, name)))
Expand All @@ -869,34 +878,40 @@ func (h *RepoHandler) handleDownload(ctx *gin.Context, isResolve bool) {
return
}
req.Lfs = lfs
reader, size, url, err := h.c.SDKDownloadFile(ctx, req, currentUser)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
slog.Error("permission denied when accessing repo", slog.String("repo_type", string(req.RepoType)), slog.Any("path", fmt.Sprintf("%s/%s", namespace, name)))
httpbase.UnauthorizedError(ctx, err)
return
}

if errors.Is(err, component.ErrNotFound) {
slog.Error("repo not found", slog.String("repo_type", string(common.RepoTypeFromContext(ctx))), slog.Any("path", fmt.Sprintf("%s/%s", namespace, name)))
httpbase.NotFoundError(ctx, err)
if contentLength > 0 {
// file content is not empty, download it directly
reader, size, url, err = h.c.SDKDownloadFile(ctx, req, currentUser)
if err != nil {
if errors.Is(err, component.ErrUnauthorized) {
slog.Error("permission denied when accessing repo", slog.String("repo_type", string(req.RepoType)), slog.Any("path", fmt.Sprintf("%s/%s", namespace, name)))
httpbase.UnauthorizedError(ctx, err)
return
}

if errors.Is(err, component.ErrNotFound) {
slog.Error("repo not found", slog.String("repo_type", string(common.RepoTypeFromContext(ctx))), slog.Any("path", fmt.Sprintf("%s/%s", namespace, name)))
httpbase.NotFoundError(ctx, err)
return
}

slog.Error("Failed to download repo file", slog.String("repo_type", string(req.RepoType)), slog.Any("error", err))
httpbase.ServerError(ctx, err)
return
}

slog.Error("Failed to download repo file", slog.String("repo_type", string(req.RepoType)), slog.Any("error", err))
httpbase.ServerError(ctx, err)
return
}

if req.Lfs {
ctx.Redirect(http.StatusMovedPermanently, url)
} else {
slog.Info("Download repo file succeed", slog.String("repo_type", string(req.RepoType)), slog.String("name", name), slog.String("path", req.Path), slog.String("ref", req.Ref))
slog.Info("Download repo file succeed", slog.String("repo_type", string(req.RepoType)), slog.String("name", name), slog.String("path", req.Path), slog.String("ref", req.Ref), slog.Any("Content-Length", size))
fileName := path.Base(req.Path)
ctx.Header("Content-Type", "application/octet-stream")
ctx.Header("Content-Disposition", `attachment; filename="`+fileName+`"`)
ctx.Header("Content-Length", strconv.FormatInt(size, 10))
_, err = io.Copy(ctx.Writer, reader)
if contentLength > 0 {
_, err = io.Copy(ctx.Writer, reader)
}
if err != nil {
slog.Error("Failed to download repo file", slog.String("repo_type", string(req.RepoType)), slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down
8 changes: 4 additions & 4 deletions component/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ func (c *RepoComponent) SDKListFiles(ctx context.Context, repoType types.Reposit
}, nil
}

func (c *RepoComponent) IsLfs(ctx context.Context, req *types.GetFileReq) (bool, error) {
func (c *RepoComponent) IsLfs(ctx context.Context, req *types.GetFileReq) (bool, int64, error) {
getFileRawReq := gitserver.GetRepoInfoByPathReq{
Namespace: req.Namespace,
Name: req.Name,
Expand All @@ -1042,13 +1042,13 @@ func (c *RepoComponent) IsLfs(ctx context.Context, req *types.GetFileReq) (bool,
content, err := c.git.GetRepoFileRaw(ctx, getFileRawReq)
if err != nil {
if err.Error() == ErrNotFoundMessage {
return false, ErrNotFound
return false, -1, ErrNotFound
}
slog.Error("failed to get %s file raw", string(req.RepoType), slog.String("namespace", req.Namespace), slog.String("name", req.Name), slog.String("path", req.Path))
return false, err
return false, -1, err
}

return strings.HasPrefix(content, LFSPrefix), nil
return strings.HasPrefix(content, LFSPrefix), int64(len(content)), nil
}

func (c *RepoComponent) HeadDownloadFile(ctx context.Context, req *types.GetFileReq, userName string) (*types.File, error) {
Expand Down