code.gitea.io/gitea@v1.21.7/services/convert/attachment.go (about)

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