From 68231e7a9b26cc4f89c86ddb6b7f67338856b269 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 8 Dec 2023 17:26:18 +0000 Subject: [PATCH] Support embedding PDF documents --- handlers_file.go | 9 ++------- markdown/embeds.go | 19 ++++++++++++++++--- resources/static/style.css | 5 +++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/handlers_file.go b/handlers_file.go index bf36533..ea4560b 100644 --- a/handlers_file.go +++ b/handlers_file.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/mdbot/wiki/markdown" "io" "log" "mime" @@ -78,12 +79,6 @@ type FileProvider interface { } func FileHandler(provider FileProvider) http.HandlerFunc { - canEmbed := func(mimeType string) bool { - return strings.HasPrefix(mimeType, "image/") || - strings.HasPrefix(mimeType, "video/") || - strings.HasPrefix(mimeType, "audio/") - } - return func(writer http.ResponseWriter, request *http.Request) { name := strings.TrimPrefix(request.URL.Path, "/files/view/") reader, err := provider.GetFile(name) @@ -100,7 +95,7 @@ func FileHandler(provider FileProvider) http.HandlerFunc { writer.Header().Add("Content-Type", mimeType) writer.Header().Add("X-Content-Type-Options", "nosniff") - if !canEmbed(mimeType) { + if !markdown.CanEmbed(mimeType) { writer.Header().Add("Content-Disposition", "attachment") } _, _ = io.Copy(writer, reader) diff --git a/markdown/embeds.go b/markdown/embeds.go index 0327917..679877b 100644 --- a/markdown/embeds.go +++ b/markdown/embeds.go @@ -59,12 +59,23 @@ const ( image mediaType = iota video audio + pdf ) var mimePrefixes = map[string]mediaType{ - "image/": image, - "video/": video, - "audio/": audio, + "image/": image, + "video/": video, + "audio/": audio, + "application/pdf": pdf, +} + +func CanEmbed(mimeType string) bool { + for m, _ := range mimePrefixes { + if strings.HasPrefix(mimeType, m) { + return true + } + } + return false } type mediaEmbed struct { @@ -117,6 +128,8 @@ func (m mediaRenderer) render(w util.BufWriter, source []byte, n ast.Node, enter _, _ = w.WriteString(fmt.Sprintf(`