Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,6 +58,38 @@ func (m Message) ParentValid() bool {
return m.ParentID != "" && !m.ParentNotFound()
}

// GetFileInfos extracts typed FileInfo list from the message.
//
// This method is guaranteed not to fail. The inner type casting should never
// fail, but will simply produce a warning if that's the case.
func (m Message) GetFileInfos(log *logrus.Entry) *[]*FileInfo {
var fileInfos []*FileInfo

for _, file := range m.Extra["file"] {
fileInfo, ok := file.(FileInfo)
if !ok {
// This should never happen, unless a bridge receiving an external message
// produces an invalid Extra field where the File is not valid FileInfo.
// TODO: log more information about the message for debugging.
log.Warn(FileCastError())
continue
}

fileInfos = append(fileInfos, &fileInfo)
}

return &fileInfos
}

// FileInfo is an attachment contained in a message.
//
// When receiving an attachment (eg. an image), a bridge should populate the
// Data/Size fields.
//
// When the media server is enabled, for services that don't support file upload
// (such as IRC), the gateway router will upload the file to the media server
// and populate the URL/SHA fields. The Data/Size fields are not removed
// in this process. See handleFiles in gateway/handlers.go
type FileInfo struct {
Name string
Data *[]byte
Expand All @@ -67,6 +101,12 @@ type FileInfo struct {
NativeID string
}

var errFileCast = errors.New("failed to cast config.FileInfo")

func FileCastError() error {
return fmt.Errorf("%w", errFileCast)
}

type ChannelInfo struct {
Name string
Account string
Expand Down
24 changes: 22 additions & 2 deletions bridge/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"bytes"
"errors"
"fmt"
"image/png"
"io"
Expand All @@ -20,6 +21,12 @@ import (
"github.com/sirupsen/logrus"
)

var errHttpGetNotOk = errors.New("HTTP server responded non-OK code")

func HttpGetNotOkError(url string, code int) error {
return fmt.Errorf("%w: %s returned code %d", errHttpGetNotOk, url, code)
}

// DownloadFile downloads the given non-authenticated URL.
func DownloadFile(url string) (*[]byte, error) {
return DownloadFileAuth(url, "")
Expand All @@ -42,8 +49,21 @@ func DownloadFileAuth(url string, auth string) (*[]byte, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
io.Copy(&buf, resp.Body)

if resp.StatusCode != http.StatusOK {
return nil, HttpGetNotOkError(url, resp.StatusCode)
}

_, err = io.Copy(&buf, resp.Body)
if err != nil {
return nil, err
}

err = resp.Body.Close()
if err != nil {
return nil, err
}

data := buf.Bytes()
return &data, nil
}
Expand Down
Loading