Adds attachment fetching/uploading
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user