mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
supporting asset download
This commit is contained in:
parent
e839397733
commit
06957fdcd7
@ -18,11 +18,6 @@ func AddChannelTopicTag(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetChannelAsset(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetChannelAssets(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -1,6 +1,8 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"net/http"
|
||||
@ -71,7 +73,7 @@ func GetArticleSubjectField(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", http.DetectContentType(binData))
|
||||
w.Write(binData)
|
||||
// response with content
|
||||
http.ServeContent(w, r, field, time.Unix(slot.Article.Updated, 0), bytes.NewReader(binData))
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"time"
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"net/http"
|
||||
@ -71,7 +73,7 @@ func GetChannelSubjectField(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", http.DetectContentType(binData))
|
||||
w.Write(binData)
|
||||
// response with content
|
||||
http.ServeContent(w, r, field, time.Unix(slot.Channel.Updated, 0), bytes.NewReader(binData))
|
||||
}
|
||||
|
||||
|
50
net/server/internal/api_getChannelTopicAsset.go
Normal file
50
net/server/internal/api_getChannelTopicAsset.go
Normal file
@ -0,0 +1,50 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func GetChannelTopicAsset(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
PrintMsg(r)
|
||||
|
||||
// scan parameters
|
||||
params := mux.Vars(r)
|
||||
topicId := params["topicId"]
|
||||
assetId := params["assetId"]
|
||||
|
||||
channelSlot, _, err, code := getChannelSlot(r, true)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
act := &channelSlot.Account
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// construct file path
|
||||
path := getStrConfigValue(CONFIG_ASSETPATH, ".") + "/" + act.Guid + "/" + asset.AssetId
|
||||
http.ServeFile(w, r, path)
|
||||
}
|
||||
|
@ -531,10 +531,10 @@ var routes = Routes{
|
||||
},
|
||||
|
||||
Route{
|
||||
"GetChannelAsset",
|
||||
"GetChannelTopicAsset",
|
||||
strings.ToUpper("Get"),
|
||||
"/content/channels/{channelId}/topics/{topicId}/assets/{assetId}",
|
||||
GetChannelAsset,
|
||||
GetChannelTopicAsset,
|
||||
},
|
||||
|
||||
Route{
|
||||
|
@ -63,6 +63,8 @@ func ApiTestData(
|
||||
body interface{},
|
||||
tokenType string,
|
||||
token string,
|
||||
start int64,
|
||||
end int64,
|
||||
) (data []byte, hdr map[string][]string, err error) {
|
||||
|
||||
var r *http.Request
|
||||
@ -78,10 +80,14 @@ func ApiTestData(
|
||||
r.Header.Add("TokenType", tokenType)
|
||||
SetBearerAuth(r, token)
|
||||
}
|
||||
if start != 0 || end != 0 {
|
||||
byteRange := "bytes=" + strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10)
|
||||
r.Header.Add("Range", byteRange)
|
||||
}
|
||||
endpoint(w, r)
|
||||
|
||||
resp := w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 206 {
|
||||
err = errors.New("response failed");
|
||||
return
|
||||
}
|
||||
|
@ -56,17 +56,17 @@ func TestTopicShare(t *testing.T) {
|
||||
assert.NotNil(t, channel.Data.ChannelDetail);
|
||||
params["field"] = "nested.image"
|
||||
data, header, err = ApiTestData(GetChannelSubjectField, "GET", "/content/channels/{channelId}/subject/{field}",
|
||||
¶ms, nil, APP_TOKENAPP, set.A.Token)
|
||||
¶ms, nil, APP_TOKENAPP, set.A.Token, 0, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "image/png", header["Content-Type"][0])
|
||||
assert.Zero(t, bytes.Compare(img, data))
|
||||
data, header, err = ApiTestData(GetChannelSubjectField, "GET", "/content/channels/{channelId}/subject/{field}",
|
||||
¶ms, nil, APP_TOKENCONTACT, set.B.A.Token)
|
||||
¶ms, nil, APP_TOKENCONTACT, set.B.A.Token, 0, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "image/png", header["Content-Type"][0])
|
||||
assert.Zero(t, bytes.Compare(img, data))
|
||||
data, header, err = ApiTestData(GetChannelSubjectField, "GET", "/content/channels/{channelId}/subject/{field}",
|
||||
¶ms, nil, APP_TOKENCONTACT, set.C.A.Token)
|
||||
¶ms, nil, APP_TOKENCONTACT, set.C.A.Token, 0, 0)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "image/png", header["Content-Type"][0])
|
||||
assert.Zero(t, bytes.Compare(img, data))
|
||||
@ -96,7 +96,6 @@ func TestTopicShare(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, ApiTestUpload(AddChannelTopicAsset, "POST", "/content/channels/{channelId}/topics/{topicId}/assets?transforms=" + url.QueryEscape(string(transforms)),
|
||||
¶ms, img, APP_TOKENCONTACT, set.C.A.Token, assets, nil))
|
||||
PrintMsg(assets)
|
||||
|
||||
// view topics
|
||||
topics := &[]Topic{}
|
||||
@ -104,5 +103,14 @@ func TestTopicShare(t *testing.T) {
|
||||
¶ms, nil, APP_TOKENAPP, set.A.Token, topics, nil))
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// download file
|
||||
params["assetId"] = (*assets)[1].AssetId
|
||||
data, header, err = ApiTestData(GetChannelTopicAsset, "GET", "/content/channels/{channelId}/topics/{topicId}/assets/{assetId}",
|
||||
¶ms, nil, APP_TOKENAPP, set.A.Token, 1, 2)
|
||||
|
||||
PrintMsg(header)
|
||||
PrintMsg(err)
|
||||
PrintMsg(len(data))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user