Use int returned from w.Write() instead of calculating length of []byte

This commit is contained in:
Seednode 2023-10-04 15:47:43 -05:00
parent 84c25310da
commit 9ae0aa60d3
5 changed files with 55 additions and 52 deletions

View File

@ -79,14 +79,14 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats types.Ty
} }
if sortOrder == "asc" || sortOrder == "desc" { if sortOrder == "asc" || sortOrder == "desc" {
splitPath, length, err := split(path, regexes) splitPath, err := split(path, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
splitPath.number = fmt.Sprintf("%0*d", length, 1) splitPath.number = fmt.Sprintf("%0*d", len(splitPath.number), 1)
path, err = tryExtensions(splitPath, formats) path, err = tryExtensions(splitPath, formats)
if err != nil { if err != nil {
@ -119,7 +119,7 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats types.Ty
} }
func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) { func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
splitPath, _, err := split(filePath, regexes) splitPath, err := split(filePath, regexes)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -104,7 +104,10 @@ func (index *fileIndex) Export(path string) error {
enc := gob.NewEncoder(z) enc := gob.NewEncoder(z)
index.mutex.RLock() index.mutex.RLock()
enc.Encode(&index.list) err = enc.Encode(&index.list)
if err != nil {
return err
}
length := len(index.list) length := len(index.list)
index.mutex.RUnlock() index.mutex.RUnlock()
@ -139,12 +142,11 @@ func (index *fileIndex) Import(path string) error {
index.mutex.Lock() index.mutex.Lock()
err = dec.Decode(&index.list) err = dec.Decode(&index.list)
length := len(index.list)
index.mutex.Unlock()
if err != nil { if err != nil {
return err return err
} }
length := len(index.list)
index.mutex.Unlock()
if Verbose { if Verbose {
fmt.Printf("%s | INDEX: Imported %d entries from %s in %s\n", fmt.Printf("%s | INDEX: Imported %d entries from %s in %s\n",

View File

@ -137,7 +137,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
htmlBody.WriteString(`</table></body></html>`) htmlBody.WriteString(`</table></body></html>`)
b, err := io.WriteString(w, gohtml.Format(htmlBody.String())) length, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil { if err != nil {
return return
} }
@ -145,7 +145,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n", fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(b), humanReadableSize(length),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -195,12 +195,15 @@ func serveIndexJson(args []string, index *fileIndex, errorChannel chan<- error)
return return
} }
w.Write(response) written, err := w.Write(response)
if err != nil {
errorChannel <- err
}
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n", fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -208,20 +211,21 @@ func serveIndexJson(args []string, index *fileIndex, errorChannel chan<- error)
} }
} }
func serveAvailableExtensions() httprouter.Handle { func serveAvailableExtensions(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()
response := []byte(types.SupportedFormats.GetExtensions()) written, err := w.Write([]byte(types.SupportedFormats.GetExtensions()))
if err != nil {
w.Write(response) errorChannel <- err
}
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: Available extension list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Available extension list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -229,20 +233,21 @@ func serveAvailableExtensions() httprouter.Handle {
} }
} }
func serveEnabledExtensions(formats types.Types) httprouter.Handle { func serveEnabledExtensions(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()
response := []byte(formats.GetExtensions()) written, err := w.Write([]byte(formats.GetExtensions()))
if err != nil {
w.Write(response) errorChannel <- err
}
if Verbose { if Verbose {
fmt.Printf("%s | SERVE: Registered extension list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Registered extension list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -250,20 +255,21 @@ func serveEnabledExtensions(formats types.Types) httprouter.Handle {
} }
} }
func serveAvailableMimeTypes() httprouter.Handle { func serveAvailableMimeTypes(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()
response := []byte(types.SupportedFormats.GetMimeTypes()) written, err := w.Write([]byte(types.SupportedFormats.GetMimeTypes()))
if err != nil {
w.Write(response) 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 MIME type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -271,20 +277,21 @@ func serveAvailableMimeTypes() httprouter.Handle {
} }
} }
func serveEnabledMimeTypes(formats types.Types) httprouter.Handle { func serveEnabledMimeTypes(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()
response := []byte(formats.GetMimeTypes()) written, err := w.Write([]byte(formats.GetMimeTypes()))
if err != nil {
w.Write(response) 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 MIME type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(written),
realIP(r), realIP(r),
time.Since(startTime).Round(time.Microsecond), time.Since(startTime).Round(time.Microsecond),
) )
@ -305,8 +312,8 @@ func registerInfoHandlers(mux *httprouter.Router, args []string, index *fileInde
} }
} }
registerHandler(mux, Prefix+"/available_extensions", serveAvailableExtensions()) registerHandler(mux, Prefix+"/available_extensions", serveAvailableExtensions(errorChannel))
registerHandler(mux, Prefix+"/enabled_extensions", serveEnabledExtensions(formats)) registerHandler(mux, Prefix+"/enabled_extensions", serveEnabledExtensions(formats, errorChannel))
registerHandler(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes()) registerHandler(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes(errorChannel))
registerHandler(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats)) registerHandler(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats, errorChannel))
} }

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
ReleaseVersion string = "2.5.0" ReleaseVersion string = "2.5.1"
) )
var ( var (

View File

@ -17,41 +17,35 @@ type splitPath struct {
} }
func (splitPath *splitPath) increment() { func (splitPath *splitPath) increment() {
length := len(splitPath.number)
asInt, err := strconv.Atoi(splitPath.number) asInt, err := strconv.Atoi(splitPath.number)
if err != nil { if err != nil {
return return
} }
splitPath.number = fmt.Sprintf("%0*d", length, asInt+1) splitPath.number = fmt.Sprintf("%0*d", len(splitPath.number), asInt+1)
} }
func (splitPath *splitPath) decrement() { func (splitPath *splitPath) decrement() {
length := len(splitPath.number)
asInt, err := strconv.Atoi(splitPath.number) asInt, err := strconv.Atoi(splitPath.number)
if err != nil { if err != nil {
return return
} }
splitPath.number = fmt.Sprintf("%0*d", length, asInt-1) splitPath.number = fmt.Sprintf("%0*d", len(splitPath.number), asInt-1)
} }
func split(path string, regexes *regexes) (*splitPath, int, error) { func split(path string, regexes *regexes) (*splitPath, error) {
p := splitPath{}
split := regexes.filename.FindAllStringSubmatch(path, -1) split := regexes.filename.FindAllStringSubmatch(path, -1)
if len(split) < 1 || len(split[0]) < 3 { if len(split) < 1 || len(split[0]) < 3 {
return &splitPath{}, 0, nil return &splitPath{}, nil
} }
p.base = split[0][1] p := &splitPath{
base: split[0][1],
number: split[0][2],
extension: split[0][3],
}
p.number = split[0][2] return p, nil
p.extension = split[0][3]
return &p, len(p.number), nil
} }