mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 00:11:04 +00:00 
			
		
		
		
	- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			79 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2022 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package packages
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"strings"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"forgejo.org/models/db"
 | 
						|
	"forgejo.org/modules/timeutil"
 | 
						|
	"forgejo.org/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, err := util.CryptoRandomString(25)
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	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)
 | 
						|
}
 |