Skip to content

Commit

Permalink
Get source mimetype from sourceuri
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed May 1, 2024
1 parent b754845 commit 4c82f03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func MessageHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

// Read the Alpaca message payload
message, err := api.DecodeAlpacaMessage(r)
auth := ""
if config.ForwardAuth {
auth = r.Header.Get("Authorization")
}
message, err := api.DecodeAlpacaMessage(r, auth)
if err != nil {
slog.Error("Error decoding Pub/Sub message", "err", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
Expand All @@ -60,7 +64,7 @@ func MessageHandler(w http.ResponseWriter, r *http.Request) {
return
}
if config.ForwardAuth {
req.Header.Set("Authorization", r.Header.Get("Authorization"))
req.Header.Set("Authorization", auth)
}
sourceResp, err := http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -76,7 +80,7 @@ func MessageHandler(w http.ResponseWriter, r *http.Request) {
}

// build a command to run that we will pipe the stdin stream into
cmd, err := scyllaridae.BuildExecCommand(message.Attachment.Content.MimeType, message.Attachment.Content.Args, config)
cmd, err := scyllaridae.BuildExecCommand(message.Attachment.Content.SourceMimeType, message.Attachment.Content.Args, config)
if err != nil {
slog.Error("Error building command", "err", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
Expand Down
8 changes: 5 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ cmdByMimeType:
destinationServer := createMockDestinationServer(tt.returnedBody)
defer destinationServer.Close()

sourceServer := createMockSourceServer(t, tt.authHeader, destinationServer.URL)
sourceServer := createMockSourceServer(t, tt.mimetype, tt.authHeader, destinationServer.URL)
defer sourceServer.Close()

os.Setenv("SCYLLARIDAE_YML", tt.yml)
Expand All @@ -168,7 +168,8 @@ cmdByMimeType:
t.Fatal(err)
}
req.Header.Set("X-Islandora-Args", destinationServer.URL)
req.Header.Set("Accept", tt.mimetype)
// set the mimetype to send to the destination server in the Accept header
req.Header.Set("Accept", "application/xml")
req.Header.Set("Authorization", tt.requestAuth)
req.Header.Set("Apix-Ldp-Resource", sourceServer.URL)

Expand All @@ -192,13 +193,14 @@ func createMockDestinationServer(content string) *httptest.Server {
}))
}

func createMockSourceServer(t *testing.T, auth, content string) *httptest.Server {
func createMockSourceServer(t *testing.T, mimetype, auth, content string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if config.ForwardAuth && r.Header.Get("Authorization") != auth {
w.WriteHeader(http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", mimetype)
if _, err := w.Write([]byte(content)); err != nil {
t.Fatal("Failed to write response in mock server")
}
Expand Down
53 changes: 43 additions & 10 deletions pkg/api/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"fmt"
"log/slog"
"net/http"
)

Expand Down Expand Up @@ -55,24 +57,55 @@ type Attachment struct {
//
// swagger:model Content
type Content struct {
MimeType string `json:"mimetype" description:"MIME type of the content"`
Args string `json:"args" description:"Arguments used or applicable to the content"`
SourceURI string `json:"sourceUri" description:"Source URI from which the content is fetched"`
DestinationURI string `json:"destinationUri" description:"Destination URI to where the content is delivered"`
FileUploadURI string `json:"fileUploadUri" description:"File upload URI for uploading the content"`
SourceMimeType string `json:"source_mimetype,omitempty" description:"MIME type of the source URI"`
DestinationMimeType string `json:"mimetype" description:"MIME type of the derivative being created"`
Args string `json:"args" description:"Arguments used or applicable to the content"`
SourceURI string `json:"sourceUri" description:"Source URI from which the content is fetched"`
DestinationURI string `json:"destinationUri" description:"Destination URI to where the content is delivered"`
FileUploadURI string `json:"fileUploadUri" description:"File upload URI for uploading the content"`
}

func DecodeAlpacaMessage(r *http.Request) (Payload, error) {
func DecodeAlpacaMessage(r *http.Request, auth string) (Payload, error) {
p := Payload{}

// set the payload based on the headers alpaca sends to this service
p.Attachment.Content.Args = r.Header.Get("X-Islandora-Args")
p.Attachment.Content.SourceURI = r.Header.Get("Apix-Ldp-Resource")
mimetype := r.Header.Get("Accept")
if mimetype == "" {
mimetype = "text/plain"

p.Attachment.Content.DestinationMimeType = r.Header.Get("Accept")
if p.Attachment.Content.DestinationMimeType == "" {
p.Attachment.Content.DestinationMimeType = "text/plain"
}

err := p.getSourceUri(auth)
if err != nil {
return p, err
}
p.Attachment.Content.MimeType = mimetype

return p, nil
}

func (p *Payload) getSourceUri(auth string) error {
client := &http.Client{}

req, err := http.NewRequest("HEAD", p.Attachment.Content.SourceURI, nil)
if err != nil {
slog.Error("Unable to create source URI request", "uri", p.Attachment.Content.SourceURI, "err", err)
return fmt.Errorf("error creating request for %s", p.Attachment.Content.SourceURI)
}

if auth != "" {
req.Header.Set("Authorization", auth)
}

resp, err := client.Do(req)
if err != nil {
slog.Error("Unable to get source URI", "uri", p.Attachment.Content.SourceURI, "err", err)
return fmt.Errorf("error issuing HEAD request on %s", p.Attachment.Content.SourceURI)
}
defer resp.Body.Close()

p.Attachment.Content.SourceMimeType = resp.Header.Get("Content-Type")
slog.Info("Got mimetype", "mime", p.Attachment.Content.SourceMimeType)
return nil
}

0 comments on commit 4c82f03

Please sign in to comment.