status-go/server/server_media.go

106 lines
2.6 KiB
Go
Raw Normal View History

package server
import (
"database/sql"
"net/url"
"github.com/status-im/status-go/ipfs"
2022-10-21 12:15:39 +00:00
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/signal"
)
type MediaServer struct {
Server
2022-06-25 07:20:02 +00:00
db *sql.DB
downloader *ipfs.Downloader
multiaccountsDB *multiaccounts.Database
}
// NewMediaServer returns a *MediaServer
2022-06-25 07:20:02 +00:00
func NewMediaServer(db *sql.DB, downloader *ipfs.Downloader, multiaccountsDB *multiaccounts.Database) (*MediaServer, error) {
err := generateTLSCert()
if err != nil {
return nil, err
}
s := &MediaServer{
2022-10-21 12:15:39 +00:00
Server: NewServer(
globalCertificate,
Localhost,
2022-10-21 12:15:39 +00:00
signal.SendMediaServerStarted,
logutils.ZapLogger().Named("MediaServer"),
),
2022-06-25 07:20:02 +00:00
db: db,
downloader: downloader,
multiaccountsDB: multiaccountsDB,
}
s.SetHandlers(HandlerPatternMap{
imagesPath: handleImage(s.db, s.logger),
audioPath: handleAudio(s.db, s.logger),
identiconsPath: handleIdenticon(s.logger),
ipfsPath: handleIPFS(s.downloader, s.logger),
accountImagesPath: handleAccountImages(s.multiaccountsDB, s.logger),
contactImagesPath: handleContactImages(s.db, s.logger),
discordAuthorsPath: handleDiscordAuthorAvatar(s.db, s.logger),
discordAttachmentsPath: handleDiscordAttachment(s.db, s.logger),
})
return s, nil
}
func (s *MediaServer) MakeImageServerURL() string {
u := s.MakeBaseURL()
u.Path = basePath + "/"
return u.String()
}
func (s *MediaServer) MakeIdenticonURL(from string) string {
u := s.MakeBaseURL()
u.Path = identiconsPath
u.RawQuery = url.Values{"publicKey": {from}}.Encode()
return u.String()
}
func (s *MediaServer) MakeImageURL(id string) string {
u := s.MakeBaseURL()
u.Path = imagesPath
u.RawQuery = url.Values{"messageId": {id}}.Encode()
return u.String()
}
func (s *MediaServer) MakeDiscordAuthorAvatarURL(authorID string) string {
u := s.MakeBaseURL()
u.Path = discordAuthorsPath
u.RawQuery = url.Values{"authorId": {authorID}}.Encode()
return u.String()
}
func (s *MediaServer) MakeDiscordAttachmentURL(messageID string, id string) string {
u := s.MakeBaseURL()
u.Path = discordAttachmentsPath
u.RawQuery = url.Values{"messageId": {messageID}, "attachmentId": {id}}.Encode()
return u.String()
}
func (s *MediaServer) MakeAudioURL(id string) string {
u := s.MakeBaseURL()
u.Path = audioPath
u.RawQuery = url.Values{"messageId": {id}}.Encode()
return u.String()
}
func (s *MediaServer) MakeStickerURL(stickerHash string) string {
u := s.MakeBaseURL()
u.Path = ipfsPath
u.RawQuery = url.Values{"hash": {stickerHash}}.Encode()
return u.String()
}