|
|
|
@@ -18,6 +18,7 @@ import ( |
|
|
|
"code.gitea.io/gitea/modules/timeutil" |
|
|
|
|
|
|
|
gouuid "github.com/satori/go.uuid" |
|
|
|
"xorm.io/builder" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
@@ -55,6 +56,19 @@ type AttachmentUsername struct { |
|
|
|
Name string |
|
|
|
} |
|
|
|
|
|
|
|
type AttachmentInfo struct { |
|
|
|
Attachment `xorm:"extends"` |
|
|
|
Repo *Repository `xorm:"extends"` |
|
|
|
} |
|
|
|
|
|
|
|
type AttachmentsOptions struct { |
|
|
|
ListOptions |
|
|
|
DatasetID int8 |
|
|
|
DecompressState int |
|
|
|
Type int |
|
|
|
NeedRepoInfo bool |
|
|
|
} |
|
|
|
|
|
|
|
func (a *Attachment) AfterUpdate() { |
|
|
|
if a.DatasetID > 0 { |
|
|
|
datasetIsPublicCount, err := x.Where("dataset_id = ? AND is_private = ?", a.DatasetID, false).Count(new(Attachment)) |
|
|
|
@@ -504,3 +518,71 @@ func GetAttachmentSizeByDatasetID(datasetID int64) (int64, error) { |
|
|
|
func GetAllAttachmentSize() (int64, error) { |
|
|
|
return x.SumInt(&Attachment{}, "size") |
|
|
|
} |
|
|
|
|
|
|
|
func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { |
|
|
|
sess := x.NewSession() |
|
|
|
defer sess.Close() |
|
|
|
|
|
|
|
var cond = builder.NewCond() |
|
|
|
if opts.DatasetID > 0 { |
|
|
|
cond = cond.And( |
|
|
|
builder.Eq{"attachment.dataset_id": opts.DatasetID}, |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
if opts.DecompressState > 0 { |
|
|
|
cond = cond.And( |
|
|
|
builder.Eq{"attachment.decompress_state": opts.DecompressState}, |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.Type) >= 0 { |
|
|
|
cond = cond.And( |
|
|
|
builder.Eq{"cloudbrain.type": opts.Type}, |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
var count int64 |
|
|
|
var err error |
|
|
|
if opts.DatasetID > 0 { |
|
|
|
count, err = sess.Where(cond).Count(new(Attachment)) |
|
|
|
} |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return nil, 0, fmt.Errorf("Count: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
if opts.Page >= 0 && opts.PageSize > 0 { |
|
|
|
var start int |
|
|
|
if opts.Page == 0 { |
|
|
|
start = 0 |
|
|
|
} else { |
|
|
|
start = (opts.Page - 1) * opts.PageSize |
|
|
|
} |
|
|
|
sess.Limit(opts.PageSize, start) |
|
|
|
} |
|
|
|
|
|
|
|
sess.OrderBy("attachment.created_unix DESC") |
|
|
|
attachments := make([]*AttachmentInfo, 0, setting.UI.IssuePagingNum) |
|
|
|
if err := sess.Table(&Attachment{}).Where(cond). |
|
|
|
Find(&attachments); err != nil { |
|
|
|
return nil, 0, fmt.Errorf("Find: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
if opts.NeedRepoInfo { |
|
|
|
for _, attachment := range attachments { |
|
|
|
dataset, err := GetDatasetByID(attachment.DatasetID) |
|
|
|
if err != nil { |
|
|
|
return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) |
|
|
|
} |
|
|
|
repo, err := GetRepositoryByID(dataset.RepoID) |
|
|
|
if err == nil { |
|
|
|
attachment.Repo = repo |
|
|
|
} else { |
|
|
|
return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return attachments, count, nil |
|
|
|
} |