2022-12-09 09:35:56 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package convert
import (
2023-07-10 12:31:19 +03:00
"strconv"
2022-12-09 09:35:56 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2023-07-10 12:31:19 +03:00
"code.gitea.io/gitea/modules/setting"
2022-12-09 09:35:56 +03:00
api "code.gitea.io/gitea/modules/structs"
)
2023-07-10 12:31:19 +03:00
func WebAssetDownloadURL ( repo * repo_model . Repository , attach * repo_model . Attachment ) string {
return attach . DownloadURL ( )
}
func APIAssetDownloadURL ( repo * repo_model . Repository , attach * repo_model . Attachment ) string {
if attach . CustomDownloadURL != "" {
return attach . CustomDownloadURL
}
// /repos/{owner}/{repo}/releases/{id}/assets/{attachment_id}
return setting . AppURL + "api/repos/" + repo . FullName ( ) + "/releases/" + strconv . FormatInt ( attach . ReleaseID , 10 ) + "/assets/" + strconv . FormatInt ( attach . ID , 10 )
}
// ToAttachment converts models.Attachment to api.Attachment for API usage
func ToAttachment ( repo * repo_model . Repository , a * repo_model . Attachment ) * api . Attachment {
return toAttachment ( repo , a , WebAssetDownloadURL )
}
// ToAPIAttachment converts models.Attachment to api.Attachment for API usage
func ToAPIAttachment ( repo * repo_model . Repository , a * repo_model . Attachment ) * api . Attachment {
return toAttachment ( repo , a , APIAssetDownloadURL )
}
// toAttachment converts models.Attachment to api.Attachment for API usage
func toAttachment ( repo * repo_model . Repository , a * repo_model . Attachment , getDownloadURL func ( repo * repo_model . Repository , attach * repo_model . Attachment ) string ) * api . Attachment {
2022-12-09 09:35:56 +03:00
return & api . Attachment {
ID : a . ID ,
Name : a . Name ,
Created : a . CreatedUnix . AsTime ( ) ,
DownloadCount : a . DownloadCount ,
Size : a . Size ,
UUID : a . UUID ,
2023-07-10 12:31:19 +03:00
DownloadURL : getDownloadURL ( repo , a ) , // for web request json and api request json, return different download urls
2022-12-09 09:35:56 +03:00
}
}
2023-07-10 12:31:19 +03:00
func ToAPIAttachments ( repo * repo_model . Repository , attachments [ ] * repo_model . Attachment ) [ ] * api . Attachment {
return toAttachments ( repo , attachments , APIAssetDownloadURL )
}
func toAttachments ( repo * repo_model . Repository , attachments [ ] * repo_model . Attachment , getDownloadURL func ( repo * repo_model . Repository , attach * repo_model . Attachment ) string ) [ ] * api . Attachment {
2022-12-09 09:35:56 +03:00
converted := make ( [ ] * api . Attachment , 0 , len ( attachments ) )
for _ , attachment := range attachments {
2023-07-10 12:31:19 +03:00
converted = append ( converted , toAttachment ( repo , attachment , getDownloadURL ) )
2022-12-09 09:35:56 +03:00
}
return converted
}