diff --git a/khatru/blossom/eventstorewrapper.go b/khatru/blossom/eventstorewrapper.go index 5cfde0b..72a2836 100644 --- a/khatru/blossom/eventstorewrapper.go +++ b/khatru/blossom/eventstorewrapper.go @@ -7,6 +7,7 @@ import ( "fiatjaf.com/nostr" "fiatjaf.com/nostr/eventstore" + "fiatjaf.com/nostr/nipb0/blossom" ) // EventStoreBlobIndexWrapper uses fake events to keep track of what blobs we have stored and who owns them @@ -89,7 +90,7 @@ func (es EventStoreBlobIndexWrapper) Delete(ctx context.Context, sha256 string, func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) BlobDescriptor { hhash := evt.Tags[0][1] mimetype := evt.Tags[1][1] - ext := getExtension(mimetype) + ext := blossom.GetExtension(mimetype) size, _ := strconv.Atoi(evt.Tags[2][1]) return BlobDescriptor{ diff --git a/khatru/blossom/handlers.go b/khatru/blossom/handlers.go index 0784c37..8ec0b8c 100644 --- a/khatru/blossom/handlers.go +++ b/khatru/blossom/handlers.go @@ -12,6 +12,7 @@ import ( "time" "fiatjaf.com/nostr" + "fiatjaf.com/nostr/nipb0/blossom" "github.com/liamg/magic" ) @@ -87,11 +88,11 @@ func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) { } else { // if we can't find, use the filetype given by the upload header mimetype := r.Header.Get("Content-Type") - ext = getExtension(mimetype) + ext = blossom.GetExtension(mimetype) } // special case of android apk -- if we see a .zip but they say it's .apk we trust them - if ext == ".zip" && getExtension(r.Header.Get("Content-Type")) == ".apk" { + if ext == ".zip" && blossom.GetExtension(r.Header.Get("Content-Type")) == ".apk" { ext = ".apk" } @@ -197,7 +198,7 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) { ext = spl[1] } } else if bd != nil { - ext = getExtension(bd.Type) + ext = blossom.GetExtension(bd.Type) } if nil != bs.RejectGet { @@ -345,7 +346,7 @@ func (bs BlossomServer) handleDelete(w http.ResponseWriter, r *http.Request) { ext = spl[1] } } else if bd != nil { - ext = getExtension(bd.Type) + ext = blossom.GetExtension(bd.Type) } // should we accept this delete? @@ -454,7 +455,7 @@ func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) { var ext string contentType := resp.Header.Get("Content-Type") if contentType != "" { - ext = getExtension(contentType) + ext = blossom.GetExtension(contentType) } else if ft, _ := magic.Lookup(body); ft != nil { ext = "." + ft.Extension } else if idx := strings.LastIndex(req.URL, "."); idx != -1 { diff --git a/khatru/blossom/utils.go b/khatru/blossom/utils.go index 6de47b3..a76cc3d 100644 --- a/khatru/blossom/utils.go +++ b/khatru/blossom/utils.go @@ -1,7 +1,6 @@ package blossom import ( - "mime" "net/http" ) @@ -9,31 +8,3 @@ func blossomError(w http.ResponseWriter, msg string, code int) { w.Header().Add("X-Reason", msg) w.WriteHeader(code) } - -func getExtension(mimetype string) string { - if mimetype == "" { - return "" - } - - switch mimetype { - case "image/jpeg": - return ".jpg" - case "image/gif": - return ".gif" - case "image/png": - return ".png" - case "image/webp": - return ".webp" - case "video/mp4": - return ".mp4" - case "application/vnd.android.package-archive": - return ".apk" - } - - exts, _ := mime.ExtensionsByType(mimetype) - if len(exts) > 0 { - return exts[0] - } - - return "" -} diff --git a/nipb0/blossom/utils.go b/nipb0/blossom/utils.go new file mode 100644 index 0000000..800fb88 --- /dev/null +++ b/nipb0/blossom/utils.go @@ -0,0 +1,31 @@ +package blossom + +import "mime" + +func GetExtension(mimetype string) string { + if mimetype == "" { + return "" + } + + switch mimetype { + case "image/jpeg": + return ".jpg" + case "image/gif": + return ".gif" + case "image/png": + return ".png" + case "image/webp": + return ".webp" + case "video/mp4": + return ".mp4" + case "application/vnd.android.package-archive": + return ".apk" + } + + exts, _ := mime.ExtensionsByType(mimetype) + if len(exts) > 0 { + return exts[0] + } + + return "" +}