Files
forge/models/packages/package_blob_upload.go
zeekayandHanzo Dev aa87442fdd Record upstream authorship centrally in NOTICE instead of in every file header
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the
Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in
NOTICE, alongside LICENSE. The header lint accepts any copyright line above the
SPDX line, so it no longer demands an upstream one.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:50:35 -07:00

77 lines
2.1 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package packages
import (
"context"
"strings"
"time"
"github.com/hanzoai/git/models/db"
"github.com/hanzoai/git/modules/timeutil"
"github.com/hanzoai/git/modules/util"
)
// ErrPackageBlobUploadNotExist indicates a package blob upload not exist error
var ErrPackageBlobUploadNotExist = util.NewNotExistErrorf("package blob upload does not exist")
func init() {
db.RegisterModel(new(PackageBlobUpload))
}
// PackageBlobUpload represents a package blob upload
type PackageBlobUpload struct {
ID string `xorm:"pk"`
BytesReceived int64 `xorm:"NOT NULL DEFAULT 0"`
HashStateBytes []byte `xorm:"BLOB"`
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
UpdatedUnix timeutil.TimeStamp `xorm:"updated INDEX NOT NULL"`
}
// CreateBlobUpload inserts a blob upload
func CreateBlobUpload(ctx context.Context) (*PackageBlobUpload, error) {
id := util.CryptoRandomString(25)
pbu := &PackageBlobUpload{
ID: strings.ToLower(id),
}
_, err := db.GetEngine(ctx).Insert(pbu)
return pbu, err
}
// GetBlobUploadByID gets a blob upload by id
func GetBlobUploadByID(ctx context.Context, id string) (*PackageBlobUpload, error) {
pbu := &PackageBlobUpload{}
has, err := db.GetEngine(ctx).ID(id).Get(pbu)
if err != nil {
return nil, err
}
if !has {
return nil, ErrPackageBlobUploadNotExist
}
return pbu, nil
}
// UpdateBlobUpload updates the blob upload
func UpdateBlobUpload(ctx context.Context, pbu *PackageBlobUpload) error {
_, err := db.GetEngine(ctx).ID(pbu.ID).Update(pbu)
return err
}
// DeleteBlobUploadByID deletes the blob upload
func DeleteBlobUploadByID(ctx context.Context, id string) error {
_, err := db.GetEngine(ctx).ID(id).Delete(&PackageBlobUpload{})
return err
}
// FindExpiredBlobUploads gets all expired blob uploads
func FindExpiredBlobUploads(ctx context.Context, olderThan time.Duration) ([]*PackageBlobUpload, error) {
pbus := make([]*PackageBlobUpload, 0, 10)
return pbus, db.GetEngine(ctx).
Where("updated_unix < ?", time.Now().Add(-olderThan).Unix()).
Find(&pbus)
}