You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.go 1.8KB

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