databag/net/server/internal/api_getAccountListingImage.go

45 lines
1.1 KiB
Go
Raw Normal View History

2022-03-09 19:27:50 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"bytes"
"databag/internal/store"
"encoding/base64"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
"time"
2022-03-09 19:27:50 +00:00
)
2022-07-25 19:19:24 +00:00
//GetAccountListingImage retrieve profile image of publicly accessible account
2022-03-09 19:27:50 +00:00
func GetAccountListingImage(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// get referenced account guid
params := mux.Vars(r)
guid := params["guid"]
var account store.Account
if err := store.DB.Preload("AccountDetail").Where("guid = ? AND searchable = ? AND disabled = ?", guid, true, false).First(&account).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if account.AccountDetail.Image == "" {
ErrResponse(w, http.StatusNotFound, errors.New("image not set"))
return
}
data, err := base64.StdEncoding.DecodeString(account.AccountDetail.Image)
if err != nil {
ErrResponse(w, http.StatusNotFound, errors.New("image not valid"))
return
}
// response with content
http.ServeContent(w, r, "image", time.Unix(account.Updated, 0), bytes.NewReader(data))
2022-03-09 19:27:50 +00:00
}