diff --git a/main.go b/main.go index bc5005f..9cff8f4 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "fmt" "io" @@ -118,14 +119,20 @@ func main() { }, } }), - micropub.WithMediaEndpoint(s.profileURL+"/micropub/media"), + micropub.WithMediaEndpoint(s.profileURL+"micropub/media"), ) r.Get("/", mpHandler.ServeHTTP) r.Post("/", mpHandler.ServeHTTP) r.Post("/media", micropub.NewMediaHandler( mp.HandleMediaUpload, - mp.HasScope, + func(r *http.Request, scope string) bool { + // IndieKit checks for a `media` scope, not commonly requested + hasMediaScope := mp.HasScope(r, scope) + hasCreateScope := mp.HasScope(r, "create") + + return hasMediaScope || hasCreateScope + }, ).ServeHTTP) }) @@ -170,7 +177,7 @@ func (s *server) servePostTemplate(w http.ResponseWriter, r *http.Request) { func (s *server) serveMedia(w http.ResponseWriter, r *http.Request) { key := strings.TrimPrefix(r.URL.Path, "/") - res, err := s.db.Media.GetObject(r.Context(), &s3.GetObjectInput{ + res, err := s.db.Media.GetObject(context.TODO(), &s3.GetObjectInput{ Bucket: aws.String(os.Getenv("AWS_S3_BUCKET_NAME")), Key: &key, }) diff --git a/micropub.go b/micropub.go index b3a2dc6..6104406 100644 --- a/micropub.go +++ b/micropub.go @@ -74,6 +74,10 @@ func (s *micropubImplementation) HandleMediaUpload(file multipart.File, header * defer file.Close() kind, err := filetype.MatchReader(file) + if _, err := file.Seek(0, 0); err != nil { + return "", fmt.Errorf("%w: %w", errors.New("failed to reset cursor"), err) + } + if err != nil { return "", fmt.Errorf("%w: %w", errors.New("failed to upload"), err) }