Skip to content

Commit

Permalink
Fix container download counter (#19287)
Browse files Browse the repository at this point in the history
* Increment counter on manifest download.

* Refactor GetPackageFileStream method.
  • Loading branch information
KN4CK3R authored Mar 31, 2022
1 parent 242d710 commit 08d1992
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
4 changes: 2 additions & 2 deletions routers/api/packages/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func GetBlob(ctx *context.Context) {
return
}

s, err := packages_module.NewContentStore().Get(packages_module.BlobHash256Key(blob.Blob.HashSHA256))
s, _, err := packages_service.GetPackageFileStream(ctx, blob.File)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
Expand Down Expand Up @@ -506,7 +506,7 @@ func GetManifest(ctx *context.Context) {
return
}

s, err := packages_module.NewContentStore().Get(packages_module.BlobHash256Key(manifest.Blob.HashSHA256))
s, _, err := packages_service.GetPackageFileStream(ctx, manifest.File)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
Expand Down
8 changes: 1 addition & 7 deletions routers/api/packages/nuget/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,7 @@ func DownloadSymbolFile(ctx *context.Context) {
return
}

pv, err := packages_model.GetVersionByID(ctx, pfs[0].VersionID)
if err != nil {
apiError(ctx, http.StatusInternalServerError, err)
return
}

s, _, err := packages_service.GetPackageFileStream(ctx, pv, pfs[0])
s, _, err := packages_service.GetPackageFileStream(ctx, pfs[0])
if err != nil {
if err == packages_model.ErrPackageNotExist || err == packages_model.ErrPackageFileNotExist {
apiError(ctx, http.StatusNotFound, err)
Expand Down
1 change: 0 additions & 1 deletion routers/web/user/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ func DownloadPackageFile(ctx *context.Context) {

s, _, err := packages_service.GetPackageFileStream(
ctx,
ctx.Package.Descriptor.Version,
pf,
)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions services/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func GetFileStreamByPackageVersionAndFileID(ctx context.Context, owner *user_mod
return nil, nil, err
}

return GetPackageFileStream(ctx, pv, pf)
return GetPackageFileStream(ctx, pf)
}

// GetFileStreamByPackageVersion returns the content of the specific package file
Expand All @@ -436,11 +436,11 @@ func GetFileStreamByPackageVersion(ctx context.Context, pv *packages_model.Packa
return nil, nil, err
}

return GetPackageFileStream(ctx, pv, pf)
return GetPackageFileStream(ctx, pf)
}

// GetPackageFileStream returns the content of the specific package file
func GetPackageFileStream(ctx context.Context, pv *packages_model.PackageVersion, pf *packages_model.PackageFile) (io.ReadCloser, *packages_model.PackageFile, error) {
func GetPackageFileStream(ctx context.Context, pf *packages_model.PackageFile) (io.ReadCloser, *packages_model.PackageFile, error) {
pb, err := packages_model.GetBlobByID(ctx, pf.BlobID)
if err != nil {
return nil, nil, err
Expand All @@ -449,7 +449,7 @@ func GetPackageFileStream(ctx context.Context, pv *packages_model.PackageVersion
s, err := packages_module.NewContentStore().Get(packages_module.BlobHash256Key(pb.HashSHA256))
if err == nil {
if pf.IsLead {
if err := packages_model.IncrementDownloadCounter(ctx, pv.ID); err != nil {
if err := packages_model.IncrementDownloadCounter(ctx, pf.VersionID); err != nil {
log.Error("Error incrementing download counter: %v", err)
}
}
Expand Down

0 comments on commit 08d1992

Please sign in to comment.