|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package conda |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/db" |
| 11 | + "code.gitea.io/gitea/models/packages" |
| 12 | + conda_module "code.gitea.io/gitea/modules/packages/conda" |
| 13 | + |
| 14 | + "xorm.io/builder" |
| 15 | +) |
| 16 | + |
| 17 | +type FileSearchOptions struct { |
| 18 | + OwnerID int64 |
| 19 | + Channel string |
| 20 | + Subdir string |
| 21 | + Filename string |
| 22 | +} |
| 23 | + |
| 24 | +// SearchFiles gets all files matching the search options |
| 25 | +func SearchFiles(ctx context.Context, opts *FileSearchOptions) ([]*packages.PackageFile, error) { |
| 26 | + var cond builder.Cond = builder.Eq{ |
| 27 | + "package.type": packages.TypeConda, |
| 28 | + "package.owner_id": opts.OwnerID, |
| 29 | + "package_version.is_internal": false, |
| 30 | + } |
| 31 | + |
| 32 | + if opts.Filename != "" { |
| 33 | + cond = cond.And(builder.Eq{ |
| 34 | + "package_file.lower_name": strings.ToLower(opts.Filename), |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + var versionPropsCond builder.Cond = builder.Eq{ |
| 39 | + "package_property.ref_type": packages.PropertyTypePackage, |
| 40 | + "package_property.name": conda_module.PropertyChannel, |
| 41 | + "package_property.value": opts.Channel, |
| 42 | + } |
| 43 | + |
| 44 | + cond = cond.And(builder.In("package.id", builder.Select("package_property.ref_id").Where(versionPropsCond).From("package_property"))) |
| 45 | + |
| 46 | + var filePropsCond builder.Cond = builder.Eq{ |
| 47 | + "package_property.ref_type": packages.PropertyTypeFile, |
| 48 | + "package_property.name": conda_module.PropertySubdir, |
| 49 | + "package_property.value": opts.Subdir, |
| 50 | + } |
| 51 | + |
| 52 | + cond = cond.And(builder.In("package_file.id", builder.Select("package_property.ref_id").Where(filePropsCond).From("package_property"))) |
| 53 | + |
| 54 | + sess := db.GetEngine(ctx). |
| 55 | + Select("package_file.*"). |
| 56 | + Table("package_file"). |
| 57 | + Join("INNER", "package_version", "package_version.id = package_file.version_id"). |
| 58 | + Join("INNER", "package", "package.id = package_version.package_id"). |
| 59 | + Where(cond) |
| 60 | + |
| 61 | + pfs := make([]*packages.PackageFile, 0, 10) |
| 62 | + return pfs, sess.Find(&pfs) |
| 63 | +} |
0 commit comments