mirror of
https://github.com/filebrowser/filebrowser.git
synced 2026-01-23 02:35:10 +00:00
refactor: add more go linters (#970)
This commit is contained in:
parent
54d92a2708
commit
700f32718e
56 changed files with 436 additions and 221 deletions
|
|
@ -1,8 +1,8 @@
|
|||
package files
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/md5" //nolint:gosec
|
||||
"crypto/sha1" //nolint:gosec
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
|
|
@ -17,9 +17,10 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/errors"
|
||||
"github.com/filebrowser/filebrowser/v2/rules"
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// FileInfo describes a file.
|
||||
|
|
@ -74,7 +75,10 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
|
|||
|
||||
if opts.Expand {
|
||||
if file.IsDir {
|
||||
return file, file.readListing(opts.Checker)
|
||||
if err := file.readListing(opts.Checker); err != nil { //nolint:shadow
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
err = file.detectType(opts.Modify, true)
|
||||
|
|
@ -105,6 +109,7 @@ func (i *FileInfo) Checksum(algo string) error {
|
|||
|
||||
var h hash.Hash
|
||||
|
||||
//nolint:gosec
|
||||
switch algo {
|
||||
case "md5":
|
||||
h = md5.New()
|
||||
|
|
@ -127,6 +132,8 @@ func (i *FileInfo) Checksum(algo string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
//nolint:goconst
|
||||
//TODO: use constants
|
||||
func (i *FileInfo) detectType(modify, saveContent bool) error {
|
||||
// failing to detect the type should not return error.
|
||||
// imagine the situation where a file in a dir with thousands
|
||||
|
|
@ -198,9 +205,9 @@ func (i *FileInfo) detectSubtitles() {
|
|||
|
||||
// TODO: detect multiple languages. Base.Lang.vtt
|
||||
|
||||
path := strings.TrimSuffix(i.Path, ext) + ".vtt"
|
||||
if _, err := i.Fs.Stat(path); err == nil {
|
||||
i.Subtitles = append(i.Subtitles, path)
|
||||
fPath := strings.TrimSuffix(i.Path, ext) + ".vtt"
|
||||
if _, err := i.Fs.Stat(fPath); err == nil {
|
||||
i.Subtitles = append(i.Subtitles, fPath)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -219,16 +226,16 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
|
|||
|
||||
for _, f := range dir {
|
||||
name := f.Name()
|
||||
path := path.Join(i.Path, name)
|
||||
fPath := path.Join(i.Path, name)
|
||||
|
||||
if !checker.Check(path) {
|
||||
if !checker.Check(fPath) {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(f.Mode().String(), "L") {
|
||||
// It's a symbolic link. We try to follow it. If it doesn't work,
|
||||
// we stay with the link information instead if the target's.
|
||||
info, err := i.Fs.Stat(path)
|
||||
info, err := i.Fs.Stat(fPath)
|
||||
if err == nil {
|
||||
f = info
|
||||
}
|
||||
|
|
@ -242,7 +249,7 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
|
|||
Mode: f.Mode(),
|
||||
IsDir: f.IsDir(),
|
||||
Extension: filepath.Ext(name),
|
||||
Path: path,
|
||||
Path: fPath,
|
||||
}
|
||||
|
||||
if file.IsDir {
|
||||
|
|
|
|||
|
|
@ -16,8 +16,10 @@ type Listing struct {
|
|||
}
|
||||
|
||||
// ApplySort applies the sort order using .Order and .Sort
|
||||
//nolint:goconst
|
||||
func (l Listing) ApplySort() {
|
||||
// Check '.Order' to know how to sort
|
||||
// TODO: use enum
|
||||
if !l.Sorting.Asc {
|
||||
switch l.Sorting.By {
|
||||
case "name":
|
||||
|
|
|
|||
|
|
@ -4,41 +4,45 @@ import (
|
|||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func isBinary(content []byte, n int) bool {
|
||||
func isBinary(content []byte, _ int) bool {
|
||||
maybeStr := string(content)
|
||||
runeCnt := utf8.RuneCount(content)
|
||||
runeIndex := 0
|
||||
gotRuneErrCnt := 0
|
||||
firstRuneErrIndex := -1
|
||||
|
||||
for _, b := range maybeStr {
|
||||
const (
|
||||
// 8 and below are control chars (e.g. backspace, null, eof, etc)
|
||||
if b <= 8 {
|
||||
maxControlCharsCode = 8
|
||||
// 0xFFFD(65533) is the "error" Rune or "Unicode replacement character"
|
||||
// see https://golang.org/pkg/unicode/utf8/#pkg-constants
|
||||
unicodeReplacementChar = 0xFFFD
|
||||
)
|
||||
|
||||
for _, b := range maybeStr {
|
||||
if b <= maxControlCharsCode {
|
||||
return true
|
||||
}
|
||||
|
||||
// 0xFFFD(65533) is the "error" Rune or "Unicode replacement character"
|
||||
// see https://golang.org/pkg/unicode/utf8/#pkg-constants
|
||||
if b == 0xFFFD {
|
||||
//if it is not the last (utf8.UTFMax - x) rune
|
||||
if b == unicodeReplacementChar {
|
||||
// if it is not the last (utf8.UTFMax - x) rune
|
||||
if runeCnt > utf8.UTFMax && runeIndex < runeCnt-utf8.UTFMax {
|
||||
return true
|
||||
} else {
|
||||
//else it is the last (utf8.UTFMax - x) rune
|
||||
//there maybe Vxxx, VVxx, VVVx, thus, we may got max 3 0xFFFD rune (asume V is the byte we got)
|
||||
//for Chinese, it can only be Vxx, VVx, we may got max 2 0xFFFD rune
|
||||
gotRuneErrCnt++
|
||||
}
|
||||
// else it is the last (utf8.UTFMax - x) rune
|
||||
// there maybe Vxxx, VVxx, VVVx, thus, we may got max 3 0xFFFD rune (assume V is the byte we got)
|
||||
// for Chinese, it can only be Vxx, VVx, we may got max 2 0xFFFD rune
|
||||
gotRuneErrCnt++
|
||||
|
||||
//mark the first time
|
||||
if firstRuneErrIndex == -1 {
|
||||
firstRuneErrIndex = runeIndex
|
||||
}
|
||||
// mark the first time
|
||||
if firstRuneErrIndex == -1 {
|
||||
firstRuneErrIndex = runeIndex
|
||||
}
|
||||
}
|
||||
runeIndex++
|
||||
}
|
||||
|
||||
//if last (utf8.UTFMax - x ) rune has the "error" Rune, but not all
|
||||
// if last (utf8.UTFMax - x ) rune has the "error" Rune, but not all
|
||||
if firstRuneErrIndex != -1 && gotRuneErrCnt != runeCnt-firstRuneErrIndex {
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue