blossom: GetExtension()

This commit is contained in:
fiatjaf
2026-01-12 15:45:53 -03:00
parent 5efcbbfddb
commit 14acd4b740
4 changed files with 39 additions and 35 deletions

View File

@@ -7,6 +7,7 @@ import (
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore" "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 // 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 { func (es EventStoreBlobIndexWrapper) parseEvent(evt nostr.Event) BlobDescriptor {
hhash := evt.Tags[0][1] hhash := evt.Tags[0][1]
mimetype := evt.Tags[1][1] mimetype := evt.Tags[1][1]
ext := getExtension(mimetype) ext := blossom.GetExtension(mimetype)
size, _ := strconv.Atoi(evt.Tags[2][1]) size, _ := strconv.Atoi(evt.Tags[2][1])
return BlobDescriptor{ return BlobDescriptor{

View File

@@ -12,6 +12,7 @@ import (
"time" "time"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/nipb0/blossom"
"github.com/liamg/magic" "github.com/liamg/magic"
) )
@@ -87,11 +88,11 @@ func (bs BlossomServer) handleUpload(w http.ResponseWriter, r *http.Request) {
} else { } else {
// if we can't find, use the filetype given by the upload header // if we can't find, use the filetype given by the upload header
mimetype := r.Header.Get("Content-Type") 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 // 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" ext = ".apk"
} }
@@ -197,7 +198,7 @@ func (bs BlossomServer) handleGetBlob(w http.ResponseWriter, r *http.Request) {
ext = spl[1] ext = spl[1]
} }
} else if bd != nil { } else if bd != nil {
ext = getExtension(bd.Type) ext = blossom.GetExtension(bd.Type)
} }
if nil != bs.RejectGet { if nil != bs.RejectGet {
@@ -345,7 +346,7 @@ func (bs BlossomServer) handleDelete(w http.ResponseWriter, r *http.Request) {
ext = spl[1] ext = spl[1]
} }
} else if bd != nil { } else if bd != nil {
ext = getExtension(bd.Type) ext = blossom.GetExtension(bd.Type)
} }
// should we accept this delete? // should we accept this delete?
@@ -454,7 +455,7 @@ func (bs BlossomServer) handleMirror(w http.ResponseWriter, r *http.Request) {
var ext string var ext string
contentType := resp.Header.Get("Content-Type") contentType := resp.Header.Get("Content-Type")
if contentType != "" { if contentType != "" {
ext = getExtension(contentType) ext = blossom.GetExtension(contentType)
} else if ft, _ := magic.Lookup(body); ft != nil { } else if ft, _ := magic.Lookup(body); ft != nil {
ext = "." + ft.Extension ext = "." + ft.Extension
} else if idx := strings.LastIndex(req.URL, "."); idx != -1 { } else if idx := strings.LastIndex(req.URL, "."); idx != -1 {

View File

@@ -1,7 +1,6 @@
package blossom package blossom
import ( import (
"mime"
"net/http" "net/http"
) )
@@ -9,31 +8,3 @@ func blossomError(w http.ResponseWriter, msg string, code int) {
w.Header().Add("X-Reason", msg) w.Header().Add("X-Reason", msg)
w.WriteHeader(code) 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 ""
}

31
nipb0/blossom/utils.go Normal file
View File

@@ -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 ""
}