Private
Public Access
1
0

Adds attachment fetching/uploading

This commit is contained in:
2024-04-07 20:22:38 -07:00
parent 63876104aa
commit fa76c7eac1
5 changed files with 202 additions and 14 deletions

View File

@@ -1 +1,5 @@
package web
type UploadAttachmentResponse struct {
TransferGUID string `json:"fileTransferGUID"`
}

View File

@@ -1,6 +1,7 @@
package web
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
@@ -330,6 +331,67 @@ func (m *MockHTTPServer) handleMarkConversation(w http.ResponseWriter, r *http.R
w.WriteHeader(http.StatusOK)
}
func (m *MockHTTPServer) handleFetchAttachment(w http.ResponseWriter, r *http.Request) {
if !m.requireAuthentication(w, r) {
return
}
guid := r.URL.Query().Get("guid")
if guid == "" {
log.Error().Msg("fetchAttachment: Missing 'guid' parameter")
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
return
}
reader, err := m.Server.FetchAttachment(guid)
if err != nil {
log.Error().Msgf("fetchAttachment: Could not load attachment from store: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dw := bufio.NewWriter(w)
_, err = dw.ReadFrom(reader)
if err != nil {
log.Error().Msgf("fetchAttachment: Error reading attachment data: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (m *MockHTTPServer) handleUploadAttachment(w http.ResponseWriter, r *http.Request) {
if !m.requireAuthentication(w, r) {
return
}
filename := r.URL.Query().Get("filename")
if filename == "" {
log.Error().Msgf("uploadAttachment: filename not provided")
http.Error(w, "filename not provided", http.StatusBadRequest)
return
}
guid, err := m.Server.UploadAttachment(filename, r.Body)
if err != nil {
log.Error().Msgf("uploadAttachment: error storing attachment: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response := UploadAttachmentResponse{
TransferGUID: *guid,
}
jsonData, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
func (m *MockHTTPServer) handleUpdates(w http.ResponseWriter, r *http.Request) {
if !m.requireAuthentication(w, r) {
return
@@ -380,6 +442,8 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
this.mux.Handle("/markConversation", http.HandlerFunc(this.handleMarkConversation))
this.mux.Handle("/fetchAttachment", http.HandlerFunc(this.handleFetchAttachment))
this.mux.Handle("/uploadAttachment", http.HandlerFunc(this.handleUploadAttachment))
this.mux.Handle("/updates", http.HandlerFunc(this.handleUpdates))
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))

View File

@@ -602,3 +602,47 @@ func TestSendMessage(t *testing.T) {
t.Fatalf("Message not found in conversation")
}
}
func TestAttachments(t *testing.T) {
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: false})
httpServer := httptest.NewServer(s)
// Send upload request
testData := "hello world!"
attachmentDataReader := strings.NewReader(testData)
resp, err := http.Post(httpServer.URL+"/uploadAttachment?filename=test.txt", "application/data", attachmentDataReader)
if err != nil {
t.Fatalf("Error uploading attachment: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error decoding body: %s", body)
}
var response web.UploadAttachmentResponse
err = json.Unmarshal(body, &response)
if err != nil {
t.Fatalf("Error decoding response: %s", err)
}
guid := response.TransferGUID
// Cleanup after ourselves
defer s.Server.DeleteAttachment(guid)
// Fetch it back
resp, err = http.Get(httpServer.URL + fmt.Sprintf("/fetchAttachment?guid=%s", guid))
if err != nil {
t.Fatalf("Error fetching attachment: %s", err)
}
responseData, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error reading attachment data: %s", err)
}
if string(responseData) != testData {
t.Fatalf("Didn't get expected response data: %s (got %s)", testData, responseData)
}
}