Use int returned from w.Write() instead of calculating length of []byte
This commit is contained in:
parent
84c25310da
commit
9ae0aa60d3
|
@ -79,14 +79,14 @@ func newFile(list []string, sortOrder string, regexes *regexes, formats types.Ty
|
|||
}
|
||||
|
||||
if sortOrder == "asc" || sortOrder == "desc" {
|
||||
splitPath, length, err := split(path, regexes)
|
||||
splitPath, err := split(path, regexes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
switch {
|
||||
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)
|
||||
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) {
|
||||
splitPath, _, err := split(filePath, regexes)
|
||||
splitPath, err := split(filePath, regexes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
10
cmd/index.go
10
cmd/index.go
|
@ -104,7 +104,10 @@ func (index *fileIndex) Export(path string) error {
|
|||
enc := gob.NewEncoder(z)
|
||||
|
||||
index.mutex.RLock()
|
||||
enc.Encode(&index.list)
|
||||
err = enc.Encode(&index.list)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := len(index.list)
|
||||
index.mutex.RUnlock()
|
||||
|
||||
|
@ -139,12 +142,11 @@ func (index *fileIndex) Import(path string) error {
|
|||
|
||||
index.mutex.Lock()
|
||||
err = dec.Decode(&index.list)
|
||||
length := len(index.list)
|
||||
index.mutex.Unlock()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := len(index.list)
|
||||
index.mutex.Unlock()
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | INDEX: Imported %d entries from %s in %s\n",
|
||||
|
|
63
cmd/info.go
63
cmd/info.go
|
@ -137,7 +137,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
|
|||
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
|
|||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(b),
|
||||
humanReadableSize(length),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
|
@ -195,12 +195,15 @@ func serveIndexJson(args []string, index *fileIndex, errorChannel chan<- error)
|
|||
return
|
||||
}
|
||||
|
||||
w.Write(response)
|
||||
written, err := w.Write(response)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(len(response)),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
response := []byte(types.SupportedFormats.GetExtensions())
|
||||
|
||||
w.Write(response)
|
||||
written, err := w.Write([]byte(types.SupportedFormats.GetExtensions()))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Available extension list (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(len(response)),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
response := []byte(formats.GetExtensions())
|
||||
|
||||
w.Write(response)
|
||||
written, err := w.Write([]byte(formats.GetExtensions()))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Registered extension list (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(len(response)),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
response := []byte(types.SupportedFormats.GetMimeTypes())
|
||||
|
||||
w.Write(response)
|
||||
written, err := w.Write([]byte(types.SupportedFormats.GetMimeTypes()))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Available MIME type list (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(len(response)),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
response := []byte(formats.GetMimeTypes())
|
||||
|
||||
w.Write(response)
|
||||
written, err := w.Write([]byte(formats.GetMimeTypes()))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Registered MIME type list (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(len(response)),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
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+"/enabled_extensions", serveEnabledExtensions(formats))
|
||||
registerHandler(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes())
|
||||
registerHandler(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats))
|
||||
registerHandler(mux, Prefix+"/available_extensions", serveAvailableExtensions(errorChannel))
|
||||
registerHandler(mux, Prefix+"/enabled_extensions", serveEnabledExtensions(formats, errorChannel))
|
||||
registerHandler(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes(errorChannel))
|
||||
registerHandler(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats, errorChannel))
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
ReleaseVersion string = "2.5.0"
|
||||
ReleaseVersion string = "2.5.1"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
26
cmd/sort.go
26
cmd/sort.go
|
@ -17,41 +17,35 @@ type splitPath struct {
|
|||
}
|
||||
|
||||
func (splitPath *splitPath) increment() {
|
||||
length := len(splitPath.number)
|
||||
|
||||
asInt, err := strconv.Atoi(splitPath.number)
|
||||
if err != nil {
|
||||
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() {
|
||||
length := len(splitPath.number)
|
||||
|
||||
asInt, err := strconv.Atoi(splitPath.number)
|
||||
if err != nil {
|
||||
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) {
|
||||
p := splitPath{}
|
||||
|
||||
func split(path string, regexes *regexes) (*splitPath, error) {
|
||||
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
||||
|
||||
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]
|
||||
|
||||
p.extension = split[0][3]
|
||||
|
||||
return &p, len(p.number), nil
|
||||
return p, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue