load audio from http server

This commit is contained in:
andrey 2022-02-23 15:34:16 +01:00 committed by flexsurfer
parent 38a16b3554
commit 598b83757c
7 changed files with 61 additions and 28 deletions

View File

@ -1 +1 @@
0.94.9 0.94.10

View File

@ -23,7 +23,7 @@ import (
"github.com/status-im/status-go/params" "github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling" "github.com/status-im/status-go/profiling"
protocol "github.com/status-im/status-go/protocol" protocol "github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/protocol/images" "github.com/status-im/status-go/server"
"github.com/status-im/status-go/services/personal" "github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/services/typeddata" "github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/signal" "github.com/status-im/status-go/signal"
@ -729,7 +729,7 @@ func ConvertToKeycardAccount(keyStoreDir, accountData, settingsJSON, password, n
} }
func ImageServerTLSCert() string { func ImageServerTLSCert() string {
cert, err := images.PublicTLSCert() cert, err := server.PublicTLSCert()
if err != nil { if err != nil {
return makeJSONResponse(err) return makeJSONResponse(err)

View File

@ -133,6 +133,8 @@ type Message struct {
AudioPath string `json:"audioPath,omitempty"` AudioPath string `json:"audioPath,omitempty"`
// ImageLocalURL is the local url of the image // ImageLocalURL is the local url of the image
ImageLocalURL string `json:"imageLocalUrl,omitempty"` ImageLocalURL string `json:"imageLocalUrl,omitempty"`
// AudioLocalURL is the local url of the audio
AudioLocalURL string `json:"audioLocalUrl,omitempty"`
// CommunityID is the id of the community to advertise // CommunityID is the id of the community to advertise
CommunityID string `json:"communityId,omitempty"` CommunityID string `json:"communityId,omitempty"`
@ -160,9 +162,10 @@ type Message struct {
Deleted bool `json:"deleted"` Deleted bool `json:"deleted"`
} }
func (m *Message) PrepareImageURL(port int) { func (m *Message) PrepareServerURLs(port int) {
m.ImageLocalURL = fmt.Sprintf("https://localhost:%d/messages/images?messageId=%s", port, m.ID) m.ImageLocalURL = fmt.Sprintf("https://localhost:%d/messages/images?messageId=%s", port, m.ID)
m.Identicon = fmt.Sprintf("https://localhost:%d/messages/identicons?publicKey=%s", port, m.From) m.Identicon = fmt.Sprintf("https://localhost:%d/messages/identicons?publicKey=%s", port, m.From)
m.AudioLocalURL = fmt.Sprintf("https://localhost:%d/messages/audio?messageId=%s", port, m.ID)
} }
func (m *Message) MarshalJSON() ([]byte, error) { func (m *Message) MarshalJSON() ([]byte, error) {
@ -226,7 +229,7 @@ func (m *Message) MarshalJSON() ([]byte, error) {
New: m.New, New: m.New,
EnsName: m.EnsName, EnsName: m.EnsName,
Image: m.ImageLocalURL, Image: m.ImageLocalURL,
Audio: m.Base64Audio, Audio: m.AudioLocalURL,
CommunityID: m.CommunityID, CommunityID: m.CommunityID,
Timestamp: m.Timestamp, Timestamp: m.Timestamp,
ContentType: m.ContentType, ContentType: m.ContentType,

View File

@ -76,7 +76,6 @@ func (db sqlitePersistence) tableUserMessagesAllFieldsJoin() string {
m1.sticker_hash, m1.sticker_hash,
m1.image_payload, m1.image_payload,
COALESCE(m1.audio_duration_ms,0), COALESCE(m1.audio_duration_ms,0),
m1.audio_base64,
m1.community_id, m1.community_id,
m1.mentions, m1.mentions,
m1.links, m1.links,
@ -101,7 +100,6 @@ func (db sqlitePersistence) tableUserMessagesAllFieldsJoin() string {
m2.text, m2.text,
m2.parsed_text, m2.parsed_text,
m2.audio_duration_ms, m2.audio_duration_ms,
m2.audio_base64,
m2.community_id, m2.community_id,
c.alias, c.alias,
c.identicon` c.identicon`
@ -119,7 +117,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
var quotedText sql.NullString var quotedText sql.NullString
var quotedParsedText []byte var quotedParsedText []byte
var quotedFrom sql.NullString var quotedFrom sql.NullString
var quotedAudio sql.NullString
var quotedAudioDuration sql.NullInt64 var quotedAudioDuration sql.NullInt64
var quotedCommunityID sql.NullString var quotedCommunityID sql.NullString
var serializedMentions []byte var serializedMentions []byte
@ -156,7 +153,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
&sticker.Hash, &sticker.Hash,
&image.Payload, &image.Payload,
&audio.DurationMs, &audio.DurationMs,
&message.Base64Audio,
&communityID, &communityID,
&serializedMentions, &serializedMentions,
&serializedLinks, &serializedLinks,
@ -181,7 +177,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
&quotedText, &quotedText,
&quotedParsedText, &quotedParsedText,
&quotedAudioDuration, &quotedAudioDuration,
&quotedAudio,
&quotedCommunityID, &quotedCommunityID,
&alias, &alias,
&identicon, &identicon,
@ -205,7 +200,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
Text: quotedText.String, Text: quotedText.String,
ParsedText: quotedParsedText, ParsedText: quotedParsedText,
AudioDurationMs: uint64(quotedAudioDuration.Int64), AudioDurationMs: uint64(quotedAudioDuration.Int64),
Base64Audio: quotedAudio.String,
CommunityID: quotedCommunityID.String, CommunityID: quotedCommunityID.String,
} }
} }

View File

@ -55,6 +55,7 @@ import (
"github.com/status-im/status-go/protocol/sqlite" "github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/transport" "github.com/status-im/status-go/protocol/transport"
v1protocol "github.com/status-im/status-go/protocol/v1" v1protocol "github.com/status-im/status-go/protocol/v1"
"github.com/status-im/status-go/server"
"github.com/status-im/status-go/services/ext/mailservers" "github.com/status-im/status-go/services/ext/mailservers"
mailserversDB "github.com/status-im/status-go/services/mailservers" mailserversDB "github.com/status-im/status-go/services/mailservers"
@ -124,7 +125,7 @@ type Messenger struct {
account *multiaccounts.Account account *multiaccounts.Account
mailserversDatabase *mailserversDB.Database mailserversDatabase *mailserversDB.Database
browserDatabase *browsers.Database browserDatabase *browsers.Database
imageServer *images.Server httpServer *server.Server
quit chan struct{} quit chan struct{}
requestedCommunities map[string]*transport.Filter requestedCommunities map[string]*transport.Filter
connectionState connection.State connectionState connection.State
@ -386,7 +387,7 @@ func NewMessenger(
settings := accounts.NewDB(database) settings := accounts.NewDB(database)
mailservers := mailserversDB.NewDB(database) mailservers := mailserversDB.NewDB(database)
imageServer, err := images.NewServer(database, logger) httpServer, err := server.NewServer(database, logger)
if err != nil { if err != nil {
return nil, err return nil, err
@ -428,14 +429,14 @@ func NewMessenger(
quit: make(chan struct{}), quit: make(chan struct{}),
requestedCommunities: make(map[string]*transport.Filter), requestedCommunities: make(map[string]*transport.Filter),
browserDatabase: c.browserDatabase, browserDatabase: c.browserDatabase,
imageServer: imageServer, httpServer: httpServer,
shutdownTasks: []func() error{ shutdownTasks: []func() error{
ensVerifier.Stop, ensVerifier.Stop,
pushNotificationClient.Stop, pushNotificationClient.Stop,
communitiesManager.Stop, communitiesManager.Stop,
encryptionProtocol.Stop, encryptionProtocol.Stop,
transp.ResetFilters, transp.ResetFilters,
imageServer.Stop, httpServer.Stop,
transp.Stop, transp.Stop,
func() error { sender.Stop(); return nil }, func() error { sender.Stop(); return nil },
// Currently this often fails, seems like it's safe to ignore them // Currently this often fails, seems like it's safe to ignore them
@ -561,14 +562,14 @@ func (m *Messenger) resendExpiredMessages() error {
} }
func (m *Messenger) ToForeground() { func (m *Messenger) ToForeground() {
if m.imageServer != nil { if m.httpServer != nil {
m.imageServer.ToForeground() m.httpServer.ToForeground()
} }
} }
func (m *Messenger) ToBackground() { func (m *Messenger) ToBackground() {
if m.imageServer != nil { if m.httpServer != nil {
m.imageServer.ToBackground() m.httpServer.ToBackground()
} }
} }
@ -657,7 +658,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
} }
} }
err = m.imageServer.Start() err = m.httpServer.Start()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -3811,7 +3812,7 @@ func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*common
} }
for idx := range msgs { for idx := range msgs {
msgs[idx].PrepareImageURL(m.imageServer.Port) msgs[idx].PrepareServerURLs(m.httpServer.Port)
} }
return msgs, nextCursor, nil return msgs, nextCursor, nil
@ -3819,7 +3820,7 @@ func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*common
func (m *Messenger) prepareMessages(messages map[string]*common.Message) { func (m *Messenger) prepareMessages(messages map[string]*common.Message) {
for idx := range messages { for idx := range messages {
messages[idx].PrepareImageURL(m.imageServer.Port) messages[idx].PrepareServerURLs(m.httpServer.Port)
} }
} }

View File

@ -3,5 +3,5 @@ package protocol
import "fmt" import "fmt"
func (m *Messenger) ImageServerURL() string { func (m *Messenger) ImageServerURL() string {
return fmt.Sprintf("https://localhost:%d/messages/", m.imageServer.Port) return fmt.Sprintf("https://localhost:%d/messages/", m.httpServer.Port)
} }

View File

@ -1,4 +1,4 @@
package images package server
import ( import (
"context" "context"
@ -19,6 +19,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/protocol/identity/identicon" "github.com/status-im/status-go/protocol/identity/identicon"
"github.com/status-im/status-go/protocol/images"
) )
var globalCertificate *tls.Certificate = nil var globalCertificate *tls.Certificate = nil
@ -92,7 +93,12 @@ func PublicTLSCert() (string, error) {
return globalPem, nil return globalPem, nil
} }
type messageHandler struct { type imageHandler struct {
db *sql.DB
logger *zap.Logger
}
type audioHandler struct {
db *sql.DB db *sql.DB
logger *zap.Logger logger *zap.Logger
} }
@ -123,7 +129,7 @@ func (s *identiconHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func (s *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *imageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
messageIDs, ok := r.URL.Query()["messageId"] messageIDs, ok := r.URL.Query()["messageId"]
if !ok || len(messageIDs) == 0 { if !ok || len(messageIDs) == 0 {
@ -141,7 +147,7 @@ func (s *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logger.Error("empty image") s.logger.Error("empty image")
return return
} }
mime, err := ImageMime(image) mime, err := images.ImageMime(image)
if err != nil { if err != nil {
s.logger.Error("failed to get mime", zap.Error(err)) s.logger.Error("failed to get mime", zap.Error(err))
} }
@ -155,6 +161,34 @@ func (s *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
func (s *audioHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
messageIDs, ok := r.URL.Query()["messageId"]
if !ok || len(messageIDs) == 0 {
s.logger.Error("no messageID")
return
}
messageID := messageIDs[0]
var audio []byte
err := s.db.QueryRow(`SELECT audio_payload FROM user_messages WHERE id = ?`, messageID).Scan(&audio)
if err != nil {
s.logger.Error("failed to find image", zap.Error(err))
return
}
if len(audio) == 0 {
s.logger.Error("empty audio")
return
}
w.Header().Set("Content-Type", "audio/aac")
w.Header().Set("Cache-Control", "no-store")
_, err = w.Write(audio)
if err != nil {
s.logger.Error("failed to write audio", zap.Error(err))
}
}
type Server struct { type Server struct {
Port int Port int
run bool run bool
@ -208,7 +242,8 @@ func (s *Server) listenAndServe() {
func (s *Server) Start() error { func (s *Server) Start() error {
handler := http.NewServeMux() handler := http.NewServeMux()
handler.Handle("/messages/images", &messageHandler{db: s.db, logger: s.logger}) handler.Handle("/messages/images", &imageHandler{db: s.db, logger: s.logger})
handler.Handle("/messages/audio", &audioHandler{db: s.db, logger: s.logger})
handler.Handle("/messages/identicons", &identiconHandler{logger: s.logger}) handler.Handle("/messages/identicons", &identiconHandler{logger: s.logger})
s.server = &http.Server{Handler: handler} s.server = &http.Server{Handler: handler}