mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-12 05:20:26 +00:00
Get rid of the older mime type mechanism
This commit is contained in:
parent
756c4e8125
commit
dbafbb85d7
@ -2,6 +2,7 @@
|
||||
A wiki engine.
|
||||
|
||||
This is the development branch for version 0.10. Features planned for this version:
|
||||
* [ ] New file structure
|
||||
* [ ] Mycomarkup
|
||||
* [ ] CLI options
|
||||
* [ ] CSS improvements
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/gemtext"
|
||||
@ -75,7 +76,7 @@ func handlerText(w http.ResponseWriter, rq *http.Request) {
|
||||
hyphaName := HyphaNameFromRq(rq, "text")
|
||||
if data, ok := HyphaStorage[hyphaName]; ok {
|
||||
log.Println("Serving", data.textPath)
|
||||
w.Header().Set("Content-Type", data.textType.Mime())
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
http.ServeFile(w, rq, data.textPath)
|
||||
}
|
||||
}
|
||||
@ -86,7 +87,7 @@ func handlerBinary(w http.ResponseWriter, rq *http.Request) {
|
||||
hyphaName := HyphaNameFromRq(rq, "binary")
|
||||
if data, ok := HyphaStorage[hyphaName]; ok {
|
||||
log.Println("Serving", data.binaryPath)
|
||||
w.Header().Set("Content-Type", data.binaryType.Mime())
|
||||
w.Header().Set("Content-Type", ExtensionToMime(filepath.Ext(data.binaryPath)))
|
||||
http.ServeFile(w, rq, data.binaryPath)
|
||||
}
|
||||
}
|
||||
|
12
hypha.go
12
hypha.go
@ -36,9 +36,7 @@ func init() {
|
||||
// HyphaData represents a hypha's meta information: binary and text parts rooted paths and content types.
|
||||
type HyphaData struct {
|
||||
textPath string
|
||||
textType TextType
|
||||
binaryPath string
|
||||
binaryType BinaryType
|
||||
}
|
||||
|
||||
// uploadHelp is a helper function for UploadText and UploadBinary
|
||||
@ -167,14 +165,14 @@ func (hd *HyphaData) RenameHypha(hyphaName, newName string, recursive bool) *his
|
||||
}
|
||||
|
||||
// binaryHtmlBlock creates an html block for binary part of the hypha.
|
||||
func binaryHtmlBlock(hyphaName string, d *HyphaData) string {
|
||||
switch d.binaryType {
|
||||
case BinaryJpeg, BinaryGif, BinaryPng, BinaryWebp, BinarySvg, BinaryIco:
|
||||
func binaryHtmlBlock(hyphaName string, hd *HyphaData) string {
|
||||
switch filepath.Ext(hd.binaryPath) {
|
||||
case ".jpg", ".gif", ".png", ".webp", ".svg", ".ico":
|
||||
return fmt.Sprintf(`
|
||||
<div class="binary-container binary-container_with-img">
|
||||
<img src="/binary/%s"/>
|
||||
</div>`, hyphaName)
|
||||
case BinaryOgg, BinaryWebm, BinaryMp4:
|
||||
case ".ogg", ".webm", ".mp4":
|
||||
return fmt.Sprintf(`
|
||||
<div class="binary-container binary-container_with-video">
|
||||
<video>
|
||||
@ -182,7 +180,7 @@ func binaryHtmlBlock(hyphaName string, d *HyphaData) string {
|
||||
<p>Your browser does not support video. See video's <a href="/binary/%[1]s">direct url</a></p>
|
||||
</video>
|
||||
`, hyphaName)
|
||||
case BinaryMp3:
|
||||
case ".mp3":
|
||||
return fmt.Sprintf(`
|
||||
<div class="binary-container binary-container_with-audio">
|
||||
<audio>
|
||||
|
2
main.go
2
main.go
@ -52,7 +52,7 @@ func handlerList(w http.ResponseWriter, rq *http.Request) {
|
||||
pageCount = len(HyphaStorage)
|
||||
)
|
||||
for hyphaName, data := range HyphaStorage {
|
||||
tbody += templates.HyphaListRowHTML(hyphaName, data.binaryType.Mime(), data.binaryPath != "")
|
||||
tbody += templates.HyphaListRowHTML(hyphaName, ExtensionToMime(filepath.Ext(data.binaryPath)), data.binaryPath != "")
|
||||
}
|
||||
util.HTTP200Page(w, base("List of pages", templates.HyphaListHTML(tbody, pageCount)))
|
||||
}
|
||||
|
96
mime.go
96
mime.go
@ -5,62 +5,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TextType is content type of text part of a hypha.
|
||||
type TextType int
|
||||
|
||||
const (
|
||||
// TextPlain is default text content type.
|
||||
TextPlain TextType = iota
|
||||
// TextGemini is content type for MycorrhizaWiki's dialect of gemtext.
|
||||
TextGemini
|
||||
)
|
||||
|
||||
// Mime returns mime type representation of `t`.
|
||||
func (t TextType) Mime() string {
|
||||
return [...]string{"text/plain", "text/gemini"}[t]
|
||||
}
|
||||
|
||||
// Extension returns extension (with dot) to be used for files with content type `t`.
|
||||
func (t TextType) Extension() string {
|
||||
return [...]string{".txt", ".gmi"}[t]
|
||||
}
|
||||
|
||||
// BinaryType is content type of binary part of a hypha.
|
||||
type BinaryType int
|
||||
|
||||
// Supported binary content types
|
||||
const (
|
||||
// BinaryOctet is default binary content type.
|
||||
BinaryOctet BinaryType = iota
|
||||
BinaryJpeg
|
||||
BinaryGif
|
||||
BinaryPng
|
||||
BinaryWebp
|
||||
BinarySvg
|
||||
BinaryIco
|
||||
BinaryOgg
|
||||
BinaryWebm
|
||||
BinaryMp3
|
||||
BinaryMp4
|
||||
)
|
||||
|
||||
var binaryMimes = [...]string{}
|
||||
|
||||
// Mime returns mime type representation of `t`.
|
||||
func (t BinaryType) Mime() string {
|
||||
return binaryMimes[t]
|
||||
}
|
||||
|
||||
var binaryExtensions = [...]string{
|
||||
".bin", ".jpg", ".gif", ".png", ".webp", ".svg", ".ico",
|
||||
".ogg", ".webm", ".mp3", ".mp4",
|
||||
}
|
||||
|
||||
// Extension returns extension (with dot) to be used for files with content type `t`.
|
||||
func (t BinaryType) Extension() string {
|
||||
return binaryExtensions[t]
|
||||
}
|
||||
|
||||
func MimeToExtension(mime string) string {
|
||||
mm := map[string]string{
|
||||
"application/octet-stream": "bin",
|
||||
@ -81,14 +25,24 @@ func MimeToExtension(mime string) string {
|
||||
return ".bin"
|
||||
}
|
||||
|
||||
// MimeToBinaryType converts mime type to BinaryType. If the mime type is not supported, BinaryOctet is returned as a fallback type.
|
||||
func MimeToBinaryType(mime string) BinaryType {
|
||||
for i, binaryMime := range binaryMimes {
|
||||
if binaryMime == mime {
|
||||
return BinaryType(i)
|
||||
}
|
||||
func ExtensionToMime(ext string) string {
|
||||
mm := map[string]string{
|
||||
"bin": "application/octet-stream",
|
||||
"jpg": "image/jpeg",
|
||||
"gif": "image/gif",
|
||||
"png": "image/png",
|
||||
"webp": "image/webp",
|
||||
"svg": "image/svg+xml",
|
||||
"ico": "image/x-icon",
|
||||
"ogg": "application/ogg",
|
||||
"webm": "video/webm",
|
||||
"mp3": "audio/mp3",
|
||||
"mp4": "video/mp4",
|
||||
}
|
||||
return BinaryOctet
|
||||
if mime, ok := mm[ext]; ok {
|
||||
return mime
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
// DataFromFilename fetches all meta information from hypha content file with path `fullPath`. If it is not a content file, `skip` is true, and you are expected to ignore this file when indexing hyphae. `name` is name of the hypha to which this file relates. `isText` is true when the content file is text, false when is binary. `mimeId` is an integer representation of content type. Cast it to TextType if `isText == true`, cast it to BinaryType if `isText == false`.
|
||||
@ -105,19 +59,3 @@ func DataFromFilename(fullPath string) (name string, isText bool, skip bool) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// mimeData determines what content type file has judging by its `ext`ension. `itText` and `mimeId` are the same as in DataFromFilename.
|
||||
func mimeData(ext string) (isText bool, mimeId int) {
|
||||
switch ext {
|
||||
case ".txt":
|
||||
return true, int(TextPlain)
|
||||
case ".gmi":
|
||||
return true, int(TextGemini)
|
||||
}
|
||||
for i, binExt := range binaryExtensions {
|
||||
if ext == binExt {
|
||||
return false, i
|
||||
}
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user