2022-06-15 14:49:31 +00:00
|
|
|
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"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MediaServer struct {
|
|
|
|
Server
|
|
|
|
|
2022-06-25 07:20:02 +00:00
|
|
|
db *sql.DB
|
|
|
|
downloader *ipfs.Downloader
|
|
|
|
multiaccountsDB *multiaccounts.Database
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-06-15 14:49:31 +00:00
|
|
|
err := generateTLSCert()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &MediaServer{
|
2022-10-21 12:15:39 +00:00
|
|
|
Server: NewServer(
|
|
|
|
globalCertificate,
|
2023-01-06 12:21:14 +00:00
|
|
|
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,
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
s.SetHandlers(HandlerPatternMap{
|
2022-09-20 09:55:59 +00:00
|
|
|
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),
|
2022-06-15 14:49:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:55:59 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
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()
|
|
|
|
}
|