Rename mime types to media types, change endpoints accordingly

This commit is contained in:
Seednode 2023-10-09 09:47:17 -05:00
parent 9633239b69
commit c6941f1336
11 changed files with 28 additions and 28 deletions

View File

@ -48,7 +48,7 @@ If `--page-length` is also set, these can be viewed in paginated form by appendi
This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index. This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index.
The remaining four endpoints—`/available_extensions`, `/enabled_extensions`, `/available_mime_types` and `/enabled_mime_types`—return information about the registered file types. The remaining four endpoints—`/available/extensions`, `/enabled/extensions`, `/available/types` and `/enabled/types`—return information about the registered file types.
## Refresh ## Refresh
If the `--refresh` flag is passed and a positive-value `refresh=<integer><unit>` query parameter is provided, the page will reload after that interval. If the `--refresh` flag is passed and a positive-value `refresh=<integer><unit>` query parameter is provided, the page will reload after that interval.

View File

@ -255,19 +255,19 @@ func serveEnabledExtensions(formats types.Types, errorChannel chan<- error) http
} }
} }
func serveAvailableMimeTypes(errorChannel chan<- error) httprouter.Handle { func serveAvailableMediaTypes(errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
startTime := time.Now() startTime := time.Now()
written, err := w.Write([]byte(types.SupportedFormats.GetMimeTypes())) written, err := w.Write([]byte(types.SupportedFormats.GetMediaTypes()))
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
} }
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: Available MIME type list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Available media type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(written), humanReadableSize(written),
realIP(r), realIP(r),
@ -277,19 +277,19 @@ func serveAvailableMimeTypes(errorChannel chan<- error) httprouter.Handle {
} }
} }
func serveEnabledMimeTypes(formats types.Types, errorChannel chan<- error) httprouter.Handle { func serveEnabledMediaTypes(formats types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
startTime := time.Now() startTime := time.Now()
written, err := w.Write([]byte(formats.GetMimeTypes())) written, err := w.Write([]byte(formats.GetMediaTypes()))
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
} }
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: Registered MIME type list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Registered media type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(written), humanReadableSize(written),
realIP(r), realIP(r),
@ -312,8 +312,8 @@ func registerInfoHandlers(mux *httprouter.Router, args []string, index *fileInde
} }
} }
registerHandler(mux, Prefix+"/available_extensions", serveAvailableExtensions(errorChannel)) registerHandler(mux, Prefix+"/available/extensions", serveAvailableExtensions(errorChannel))
registerHandler(mux, Prefix+"/enabled_extensions", serveEnabledExtensions(formats, errorChannel)) registerHandler(mux, Prefix+"/enabled/extensions", serveEnabledExtensions(formats, errorChannel))
registerHandler(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes(errorChannel)) registerHandler(mux, Prefix+"/available/types", serveAvailableMediaTypes(errorChannel))
registerHandler(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats, errorChannel)) registerHandler(mux, Prefix+"/enabled/types", serveEnabledMediaTypes(formats, errorChannel))
} }

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "2.8.1" ReleaseVersion string = "2.9.0"
) )
var ( var (

View File

@ -297,7 +297,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
return return
} }
mimeType := format.MimeType(path) mediaType := format.MediaType(path)
fileUri := Prefix + generateFileUri(path) fileUri := Prefix + generateFileUri(path)
@ -314,7 +314,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
htmlBody.WriteString(faviconHtml) htmlBody.WriteString(faviconHtml)
htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, format.Css())) htmlBody.WriteString(fmt.Sprintf(`<style>%s</style>`, format.Css()))
title, err := format.Title(rootUrl, fileUri, path, fileName, Prefix, mimeType) title, err := format.Title(rootUrl, fileUri, path, fileName, Prefix, mediaType)
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err
@ -328,7 +328,7 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
htmlBody.WriteString(refreshFunction(rootUrl, refreshTimer)) htmlBody.WriteString(refreshFunction(rootUrl, refreshTimer))
} }
body, err := format.Body(rootUrl, fileUri, path, fileName, Prefix, mimeType) body, err := format.Body(rootUrl, fileUri, path, fileName, Prefix, mediaType)
if err != nil { if err != nil {
errorChannel <- err errorChannel <- err

View File

@ -44,7 +44,7 @@ func (t Format) Extensions() map[string]string {
} }
} }
func (t Format) MimeType(extension string) string { func (t Format) MediaType(extension string) string {
extensions := t.Extensions() extensions := t.Extensions()
value, exists := extensions[extension] value, exists := extensions[extension]

View File

@ -210,7 +210,7 @@ func (t Format) Extensions() map[string]string {
} }
} }
func (t Format) MimeType(extension string) string { func (t Format) MediaType(extension string) string {
extensions := t.Extensions() extensions := t.Extensions()
value, exists := extensions[extension] value, exists := extensions[extension]

View File

@ -43,7 +43,7 @@ func (t Format) Extensions() map[string]string {
} }
} }
func (t Format) MimeType(extension string) string { func (t Format) MediaType(extension string) string {
extensions := t.Extensions() extensions := t.Extensions()
value, exists := extensions[extension] value, exists := extensions[extension]

View File

@ -97,7 +97,7 @@ func (t Format) Validate(filePath string) bool {
return true return true
} }
func (t Format) MimeType(extension string) string { func (t Format) MediaType(extension string) string {
extensions := t.Extensions() extensions := t.Extensions()
value, exists := extensions[extension] value, exists := extensions[extension]

View File

@ -51,7 +51,7 @@ func (t Format) Extensions() map[string]string {
} }
} }
func (t Format) MimeType(extension string) string { func (t Format) MediaType(extension string) string {
extensions := t.Extensions() extensions := t.Extensions()
value, exists := extensions[extension] value, exists := extensions[extension]

View File

@ -31,7 +31,7 @@ type Type interface {
// Given a file extension, returns the corresponding MIME type, // Given a file extension, returns the corresponding MIME type,
// if one exists. Otherwise, returns an empty string. // if one exists. Otherwise, returns an empty string.
MimeType(extension string) string MediaType(extension string) string
// Optional function used to validate whether a given file matches this format. // Optional function used to validate whether a given file matches this format.
// If no validation checks are needed, this function should always return true. // If no validation checks are needed, this function should always return true.
@ -92,23 +92,23 @@ func (t Types) GetExtensions() string {
return output.String() return output.String()
} }
func (t Types) GetMimeTypes() string { func (t Types) GetMediaTypes() string {
var output strings.Builder var output strings.Builder
var mimeTypes []string var mediaTypes []string
for _, j := range t { for _, j := range t {
extensions := j.Extensions() extensions := j.Extensions()
for _, v := range extensions { for _, v := range extensions {
mimeTypes = append(mimeTypes, v) mediaTypes = append(mediaTypes, v)
} }
} }
mimeTypes = removeDuplicateStr(mimeTypes) mediaTypes = removeDuplicateStr(mediaTypes)
slices.Sort(mimeTypes) slices.Sort(mediaTypes)
for _, v := range mimeTypes { for _, v := range mediaTypes {
output.WriteString(v + "\n") output.WriteString(v + "\n")
} }

View File

@ -48,7 +48,7 @@ func (t Format) Extensions() map[string]string {
} }
} }
func (t Format) MimeType(path string) string { func (t Format) MediaType(path string) string {
extensions := t.Extensions() extensions := t.Extensions()
extension := filepath.Ext(path) extension := filepath.Ext(path)