Added endpoints to view registered file formats

This commit is contained in:
Seednode 2023-09-12 22:35:15 -05:00
parent c77a151a24
commit cbf7218453
5 changed files with 96 additions and 13 deletions

View File

@ -78,9 +78,13 @@ The cache can be regenerated at any time by accessing the `/clear_cache` endpoin
If `--cache-file` is set, the cache will be loaded from the specified file on start, and written to the file whenever it is re-generated.
If the `-i|--index` flag is passed, two additional endpoints—`/html` and `/json`are registered.
If the `-i|--index` flag is passed, four additional endpoints are registered.
When accessed, these endpoints return the contents of the index, in HTML and JSON formats respectively. This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index.
The first of these—`/html` and `/json`—return the contents of the index, in HTML and JSON formats respectively.
This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index.
The other two endpoints—`/extensions` and `/mime_types`—return the registered file types.
## Statistics

View File

@ -11,6 +11,7 @@ import (
"io"
"net/http"
"os"
"slices"
"sort"
"strconv"
"strings"
@ -316,3 +317,79 @@ func serveIndexJson(args []string, index *FileIndex) httprouter.Handle {
}
}
}
func serveExtensions(formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/plain")
startTime := time.Now()
var output strings.Builder
extensions := make([]string, len(formats.Extensions))
i := 0
for k := range formats.Extensions {
extensions[i] = k
i++
}
slices.Sort(extensions)
for _, v := range extensions {
output.WriteString(v + "\n")
}
response := []byte(output.String())
w.Write(response)
if Verbose {
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
)
}
}
}
func serveMimeTypes(formats *types.Types) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Content-Type", "text/plain")
startTime := time.Now()
var output strings.Builder
mimeTypes := make([]string, len(formats.MimeTypes))
i := 0
for k := range formats.MimeTypes {
mimeTypes[i] = k
i++
}
slices.Sort(mimeTypes)
for _, v := range mimeTypes {
output.WriteString(v + "\n")
}
response := []byte(output.String())
w.Write(response)
if Verbose {
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
startTime.Format(LogDate),
humanReadableSize(len(response)),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
)
}
}
}

View File

@ -12,7 +12,7 @@ import (
)
const (
ReleaseVersion string = "0.74.0"
ReleaseVersion string = "0.75.0"
)
var (
@ -46,12 +46,6 @@ var (
Short: "Serves random media from the specified directories.",
Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
// enable image support if no other flags are passed, to retain backwards compatibility
// to be replaced with MarkFlagsOneRequired on next spf13/cobra update
if !(All || Audio || Flash || Images || Text || Videos) {
Images = true
}
if Index {
cmd.MarkFlagRequired("cache")
}

View File

@ -324,10 +324,6 @@ func ServePage(args []string) error {
formats.Add(types.Flash{})
}
if Images || All {
formats.Add(types.Images{})
}
if Text || All {
formats.Add(types.Text{})
}
@ -336,6 +332,12 @@ func ServePage(args []string) error {
formats.Add(types.Video{})
}
// enable image support if no other flags are passed, to retain backwards compatibility
// to be replaced with rootCmd.MarkFlagsOneRequired on next spf13/cobra update
if Images || All || len(formats.Extensions) == 0 {
formats.Add(types.Images{})
}
paths, err := normalizePaths(args, formats)
if err != nil {
return err
@ -416,6 +418,10 @@ func ServePage(args []string) error {
if PageLength != 0 {
mux.GET("/json/:page", serveIndexJson(args, index))
}
mux.GET("/extensions", serveExtensions(formats))
mux.GET("/mime_types", serveMimeTypes(formats))
}
if Profile {

View File

@ -44,6 +44,7 @@ func (t Text) Extensions() map[string]string {
return map[string]string{
`.css`: `text/css`,
`.csv`: `text/csv`,
`.htm`: `text/html`,
`.html`: `text/html`,
`.js`: `text/javascript`,
`.json`: `application/json`,
@ -59,6 +60,7 @@ func (t Text) MimeTypes() []string {
`application/xml`,
`text/css`,
`text/csv`,
`text/html`,
`text/javascript`,
`text/plain`,
`text/plain; charset=utf-8`,