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.

attachment.go 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package convert
  4. import (
  5. repo_model "code.gitea.io/gitea/models/repo"
  6. api "code.gitea.io/gitea/modules/structs"
  7. )
  8. func WebAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
  9. return attach.DownloadURL()
  10. }
  11. func APIAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
  12. return attach.DownloadURL()
  13. }
  14. // ToAttachment converts models.Attachment to api.Attachment for API usage
  15. func ToAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
  16. return toAttachment(repo, a, WebAssetDownloadURL)
  17. }
  18. // ToAPIAttachment converts models.Attachment to api.Attachment for API usage
  19. func ToAPIAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
  20. return toAttachment(repo, a, APIAssetDownloadURL)
  21. }
  22. // toAttachment converts models.Attachment to api.Attachment for API usage
  23. func toAttachment(repo *repo_model.Repository, a *repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Attachment {
  24. return &api.Attachment{
  25. ID: a.ID,
  26. Name: a.Name,
  27. Created: a.CreatedUnix.AsTime(),
  28. DownloadCount: a.DownloadCount,
  29. Size: a.Size,
  30. UUID: a.UUID,
  31. DownloadURL: getDownloadURL(repo, a), // for web request json and api request json, return different download urls
  32. }
  33. }
  34. func ToAPIAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
  35. return toAttachments(repo, attachments, APIAssetDownloadURL)
  36. }
  37. func toAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) []*api.Attachment {
  38. converted := make([]*api.Attachment, 0, len(attachments))
  39. for _, attachment := range attachments {
  40. converted = append(converted, toAttachment(repo, attachment, getDownloadURL))
  41. }
  42. return converted
  43. }