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/profiling"
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/typeddata"
"github.com/status-im/status-go/signal"
@ -729,7 +729,7 @@ func ConvertToKeycardAccount(keyStoreDir, accountData, settingsJSON, password, n
}
func ImageServerTLSCert() string {
cert, err := images.PublicTLSCert()
cert, err := server.PublicTLSCert()
if err != nil {
return makeJSONResponse(err)

View File

@ -133,6 +133,8 @@ type Message struct {
AudioPath string `json:"audioPath,omitempty"`
// ImageLocalURL is the local url of the image
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 string `json:"communityId,omitempty"`
@ -160,9 +162,10 @@ type Message struct {
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.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) {
@ -226,7 +229,7 @@ func (m *Message) MarshalJSON() ([]byte, error) {
New: m.New,
EnsName: m.EnsName,
Image: m.ImageLocalURL,
Audio: m.Base64Audio,
Audio: m.AudioLocalURL,
CommunityID: m.CommunityID,
Timestamp: m.Timestamp,
ContentType: m.ContentType,

View File

@ -76,7 +76,6 @@ func (db sqlitePersistence) tableUserMessagesAllFieldsJoin() string {
m1.sticker_hash,
m1.image_payload,
COALESCE(m1.audio_duration_ms,0),
m1.audio_base64,
m1.community_id,
m1.mentions,
m1.links,
@ -101,7 +100,6 @@ func (db sqlitePersistence) tableUserMessagesAllFieldsJoin() string {
m2.text,
m2.parsed_text,
m2.audio_duration_ms,
m2.audio_base64,
m2.community_id,
c.alias,
c.identicon`
@ -119,7 +117,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
var quotedText sql.NullString
var quotedParsedText []byte
var quotedFrom sql.NullString
var quotedAudio sql.NullString
var quotedAudioDuration sql.NullInt64
var quotedCommunityID sql.NullString
var serializedMentions []byte
@ -156,7 +153,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
&sticker.Hash,
&image.Payload,
&audio.DurationMs,
&message.Base64Audio,
&communityID,
&serializedMentions,
&serializedLinks,
@ -181,7 +177,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
&quotedText,
&quotedParsedText,
&quotedAudioDuration,
&quotedAudio,
&quotedCommunityID,
&alias,
&identicon,
@ -205,7 +200,6 @@ func (db sqlitePersistence) tableUserMessagesScanAllFields(row scanner, message
Text: quotedText.String,
ParsedText: quotedParsedText,
AudioDurationMs: uint64(quotedAudioDuration.Int64),
Base64Audio: quotedAudio.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/transport"
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"
mailserversDB "github.com/status-im/status-go/services/mailservers"
@ -124,7 +125,7 @@ type Messenger struct {
account *multiaccounts.Account
mailserversDatabase *mailserversDB.Database
browserDatabase *browsers.Database
imageServer *images.Server
httpServer *server.Server
quit chan struct{}
requestedCommunities map[string]*transport.Filter
connectionState connection.State
@ -386,7 +387,7 @@ func NewMessenger(
settings := accounts.NewDB(database)
mailservers := mailserversDB.NewDB(database)
imageServer, err := images.NewServer(database, logger)
httpServer, err := server.NewServer(database, logger)
if err != nil {
return nil, err
@ -428,14 +429,14 @@ func NewMessenger(
quit: make(chan struct{}),
requestedCommunities: make(map[string]*transport.Filter),
browserDatabase: c.browserDatabase,
imageServer: imageServer,
httpServer: httpServer,
shutdownTasks: []func() error{
ensVerifier.Stop,
pushNotificationClient.Stop,
communitiesManager.Stop,
encryptionProtocol.Stop,
transp.ResetFilters,
imageServer.Stop,
httpServer.Stop,
transp.Stop,
func() error { sender.Stop(); return nil },
// 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() {
if m.imageServer != nil {
m.imageServer.ToForeground()
if m.httpServer != nil {
m.httpServer.ToForeground()
}
}
func (m *Messenger) ToBackground() {
if m.imageServer != nil {
m.imageServer.ToBackground()
if m.httpServer != nil {
m.httpServer.ToBackground()
}
}
@ -657,7 +658,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
}
}
err = m.imageServer.Start()
err = m.httpServer.Start()
if err != nil {
return nil, err
}
@ -3811,7 +3812,7 @@ func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*common
}
for idx := range msgs {
msgs[idx].PrepareImageURL(m.imageServer.Port)
msgs[idx].PrepareServerURLs(m.httpServer.Port)
}
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) {
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"
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 (
"context"
@ -19,6 +19,7 @@ import (
"go.uber.org/zap"
"github.com/status-im/status-go/protocol/identity/identicon"
"github.com/status-im/status-go/protocol/images"
)
var globalCertificate *tls.Certificate = nil
@ -92,7 +93,12 @@ func PublicTLSCert() (string, error) {
return globalPem, nil
}
type messageHandler struct {
type imageHandler struct {
db *sql.DB
logger *zap.Logger
}
type audioHandler struct {
db *sql.DB
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"]
if !ok || len(messageIDs) == 0 {
@ -141,7 +147,7 @@ func (s *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logger.Error("empty image")
return
}
mime, err := ImageMime(image)
mime, err := images.ImageMime(image)
if err != nil {
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 {
Port int
run bool
@ -208,7 +242,8 @@ func (s *Server) listenAndServe() {
func (s *Server) Start() error {
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})
s.server = &http.Server{Handler: handler}