databag/net/server/internal/api_getChannelTopicAsset.go

49 lines
1.3 KiB
Go
Raw Normal View History

2022-03-02 07:52:37 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
2022-03-02 07:52:37 +00:00
)
2022-07-27 05:48:50 +00:00
//GetChannelTopicAsset retrieves asset added to specified topic
2022-03-02 07:52:37 +00:00
func GetChannelTopicAsset(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
topicID := params["topicID"]
assetID := params["assetID"]
2022-03-02 07:52:37 +00:00
2022-07-27 05:43:39 +00:00
channelSlot, _, code, err := getChannelSlot(r, true)
2022-07-22 19:28:14 +00:00
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
2022-03-02 07:52:37 +00:00
2022-07-22 19:28:14 +00:00
// load asset
var asset store.Asset
if err = store.DB.Preload("Topic.TopicSlot").Where("channel_id = ? AND asset_id = ?", channelSlot.Channel.ID, assetID).First(&asset).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if asset.Topic.TopicSlot.TopicSlotID != topicID {
ErrResponse(w, http.StatusNotFound, errors.New("invalid topic asset"))
return
}
if asset.Transform == "" {
ErrResponse(w, http.StatusUnauthorized, errors.New("transform source files not accessible"))
return
}
2022-03-02 07:52:37 +00:00
2022-07-22 19:28:14 +00:00
// construct file path
path := getStrConfigValue(CNFAssetPath, APPDefaultPath) + "/" + act.GUID + "/" + asset.AssetID
http.ServeFile(w, r, path)
2022-03-02 07:52:37 +00:00
}