mirror of
https://github.com/nabbar/golib.git
synced 2025-09-26 20:01:15 +08:00
Fix local path operations by using a wrong package (diff between package path and path/filepath)
Bump dependencies
This commit is contained in:
@@ -29,7 +29,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
libbz2 "github.com/nabbar/golib/archive/bz2"
|
||||
libgzp "github.com/nabbar/golib/archive/gzip"
|
||||
@@ -187,7 +187,7 @@ func ExtractAll(src libiot.FileProgress, originalName, outputPath string, defaul
|
||||
}
|
||||
|
||||
liblog.DebugLevel.Log("writing original file...")
|
||||
if dst, err = src.NewFilePathWrite(path.Join(outputPath, originalName), true, true, 0664); err != nil {
|
||||
if dst, err = src.NewFilePathWrite(filepath.Join(outputPath, originalName), true, true, 0664); err != nil {
|
||||
return ErrorFileOpen.Error(err)
|
||||
}
|
||||
|
||||
|
@@ -26,11 +26,18 @@
|
||||
package archive
|
||||
|
||||
import (
|
||||
"path"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
pathSeparatorOs = string(os.PathSeparator)
|
||||
pathSeparatorCommon = "/"
|
||||
pathParentCommon = ".."
|
||||
)
|
||||
|
||||
type File struct {
|
||||
Name string
|
||||
Path string
|
||||
@@ -71,15 +78,15 @@ func (a File) RegexFullPath(regex string) bool {
|
||||
}
|
||||
|
||||
func (a File) GetKeyMap() string {
|
||||
return path.Join(a.Path, a.Name)
|
||||
return filepath.Join(a.Path, a.Name)
|
||||
}
|
||||
|
||||
func (a File) GetDestFileOnly(baseDestination string) string {
|
||||
return path.Join(baseDestination, a.Name)
|
||||
return filepath.Join(baseDestination, a.Name)
|
||||
}
|
||||
|
||||
func (a File) GetDestWithPath(baseDestination string) string {
|
||||
return path.Join(baseDestination, a.Path, a.Name)
|
||||
return filepath.Join(baseDestination, a.Path, a.Name)
|
||||
}
|
||||
|
||||
func NewFile(name, path string) File {
|
||||
@@ -90,5 +97,19 @@ func NewFile(name, path string) File {
|
||||
}
|
||||
|
||||
func NewFileFullPath(fullpath string) File {
|
||||
return NewFile(path.Base(fullpath), path.Dir(fullpath))
|
||||
return NewFile(filepath.Base(fullpath), filepath.Dir(fullpath))
|
||||
}
|
||||
|
||||
func CleanPath(p string) string {
|
||||
for {
|
||||
if strings.HasPrefix(p, pathParentCommon) {
|
||||
p = strings.TrimPrefix(p, pathParentCommon)
|
||||
} else if strings.HasPrefix(p, pathSeparatorCommon+pathParentCommon) {
|
||||
p = strings.TrimPrefix(p, pathSeparatorCommon+pathParentCommon)
|
||||
} else if strings.HasPrefix(p, pathSeparatorOs+pathParentCommon) {
|
||||
p = strings.TrimPrefix(p, pathSeparatorOs+pathParentCommon)
|
||||
} else {
|
||||
return filepath.Clean(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -29,17 +29,16 @@ import (
|
||||
"archive/tar"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
libarc "github.com/nabbar/golib/archive/archive"
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
libiut "github.com/nabbar/golib/ioutils"
|
||||
libiot "github.com/nabbar/golib/ioutils"
|
||||
)
|
||||
|
||||
func GetFile(src, dst libiut.FileProgress, filenameContain, filenameRegex string) liberr.Error {
|
||||
func GetFile(src, dst libiot.FileProgress, filenameContain, filenameRegex string) liberr.Error {
|
||||
|
||||
if _, e := src.Seek(0, io.SeekStart); e != nil {
|
||||
return ErrorFileSeek.ErrorParent(e)
|
||||
@@ -95,7 +94,7 @@ func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode)
|
||||
|
||||
//nolint #nosec
|
||||
/* #nosec */
|
||||
if err := writeContent(r, h, path.Join(outputFolder, path.Clean(h.Name)), defaultDirPerm); err != nil {
|
||||
if err := writeContent(r, h, filepath.Join(outputFolder, libarc.CleanPath(h.Name)), defaultDirPerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -104,10 +103,10 @@ func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode)
|
||||
func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.FileMode) (err liberr.Error) {
|
||||
var (
|
||||
inf = h.FileInfo()
|
||||
dst libiut.FileProgress
|
||||
dst libiot.FileProgress
|
||||
)
|
||||
|
||||
if e := dirIsExistOrCreate(path.Dir(out), defaultDirPerm); e != nil {
|
||||
if e := dirIsExistOrCreate(filepath.Dir(out), defaultDirPerm); e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -126,12 +125,12 @@ func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.File
|
||||
} else if err = notDirExistCannotClean(out, h.Typeflag, h.Linkname); err != nil {
|
||||
return
|
||||
} else if h.Typeflag&tar.TypeLink == tar.TypeLink {
|
||||
return createLink(out, path.Clean(h.Linkname), false)
|
||||
return createLink(out, libarc.CleanPath(h.Linkname), false)
|
||||
} else if h.Typeflag&tar.TypeSymlink == tar.TypeSymlink {
|
||||
return createLink(out, path.Clean(h.Linkname), true)
|
||||
return createLink(out, libarc.CleanPath(h.Linkname), true)
|
||||
}
|
||||
|
||||
if dst, err = libiut.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
|
||||
if dst, err = libiot.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
|
||||
return ErrorFileOpen.Error(err)
|
||||
} else if _, e := io.Copy(dst, r); e != nil {
|
||||
return ErrorIOCopy.ErrorParent(e)
|
||||
@@ -207,12 +206,12 @@ func createLink(link, target string, sym bool) liberr.Error {
|
||||
}
|
||||
|
||||
if sym {
|
||||
err := os.Symlink(path.Clean(target), path.Clean(link))
|
||||
err := os.Symlink(libarc.CleanPath(target), libarc.CleanPath(link))
|
||||
if err != nil {
|
||||
return ErrorLinkCreate.ErrorParent(err)
|
||||
}
|
||||
} else {
|
||||
err := os.Link(path.Clean(target), path.Clean(link))
|
||||
err := os.Link(libarc.CleanPath(target), libarc.CleanPath(link))
|
||||
if err != nil {
|
||||
return ErrorLinkCreate.ErrorParent(err)
|
||||
}
|
||||
@@ -230,5 +229,5 @@ func compareLinkTarget(link, target string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.EqualFold(path.Clean(l), path.Clean(target))
|
||||
return strings.EqualFold(libarc.CleanPath(l), libarc.CleanPath(target))
|
||||
}
|
||||
|
@@ -29,14 +29,14 @@ import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/nabbar/golib/archive/archive"
|
||||
"github.com/nabbar/golib/errors"
|
||||
"github.com/nabbar/golib/ioutils"
|
||||
arcmod "github.com/nabbar/golib/archive/archive"
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
libiot "github.com/nabbar/golib/ioutils"
|
||||
)
|
||||
|
||||
func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex string) errors.Error {
|
||||
func GetFile(src, dst libiot.FileProgress, filenameContain, filenameRegex string) liberr.Error {
|
||||
var (
|
||||
arc *zip.Reader
|
||||
inf os.FileInfo
|
||||
@@ -58,7 +58,7 @@ func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex strin
|
||||
continue
|
||||
}
|
||||
|
||||
z := archive.NewFileFullPath(f.Name)
|
||||
z := arcmod.NewFileFullPath(f.Name)
|
||||
|
||||
if z.MatchingFullPath(filenameContain) || z.RegexFullPath(filenameRegex) {
|
||||
if f == nil {
|
||||
@@ -98,7 +98,7 @@ func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex strin
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAll(src ioutils.FileProgress, outputFolder string, defaultDirPerm os.FileMode) errors.Error {
|
||||
func GetAll(src libiot.FileProgress, outputFolder string, defaultDirPerm os.FileMode) liberr.Error {
|
||||
var (
|
||||
r *zip.Reader
|
||||
i os.FileInfo
|
||||
@@ -120,7 +120,7 @@ func GetAll(src ioutils.FileProgress, outputFolder string, defaultDirPerm os.Fil
|
||||
|
||||
//nolint #nosec
|
||||
/* #nosec */
|
||||
if err := writeContent(f, path.Join(outputFolder, path.Clean(f.Name)), defaultDirPerm); err != nil {
|
||||
if err := writeContent(f, filepath.Join(outputFolder, arcmod.CleanPath(f.Name)), defaultDirPerm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -128,16 +128,16 @@ func GetAll(src ioutils.FileProgress, outputFolder string, defaultDirPerm os.Fil
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err errors.Error) {
|
||||
func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err liberr.Error) {
|
||||
var (
|
||||
dst ioutils.FileProgress
|
||||
dst libiot.FileProgress
|
||||
inf = f.FileInfo()
|
||||
|
||||
r io.ReadCloser
|
||||
e error
|
||||
)
|
||||
|
||||
if err = dirIsExistOrCreate(path.Dir(out), defaultDirPerm); err != nil {
|
||||
if err = dirIsExistOrCreate(filepath.Dir(out), defaultDirPerm); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err erro
|
||||
return
|
||||
}
|
||||
|
||||
if dst, err = ioutils.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
|
||||
if dst, err = libiot.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
|
||||
return ErrorFileOpen.Error(err)
|
||||
} else if r, e = f.Open(); e != nil {
|
||||
return ErrorZipFileOpen.ErrorParent(e)
|
||||
@@ -181,9 +181,9 @@ func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err erro
|
||||
return nil
|
||||
}
|
||||
|
||||
func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) errors.Error {
|
||||
if i, e := os.Stat(path.Dir(dirname)); e != nil && os.IsNotExist(e) {
|
||||
if e = os.MkdirAll(path.Dir(dirname), dirPerm); e != nil {
|
||||
func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) liberr.Error {
|
||||
if i, e := os.Stat(filepath.Dir(dirname)); e != nil && os.IsNotExist(e) {
|
||||
if e = os.MkdirAll(filepath.Dir(dirname), dirPerm); e != nil {
|
||||
return ErrorDirCreate.ErrorParent(e)
|
||||
}
|
||||
} else if e != nil {
|
||||
@@ -195,7 +195,7 @@ func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) errors.Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func notDirExistCannotClean(filename string) errors.Error {
|
||||
func notDirExistCannotClean(filename string) liberr.Error {
|
||||
if i, e := os.Stat(filename); e != nil && !os.IsNotExist(e) {
|
||||
return ErrorDestinationStat.ErrorParent(e)
|
||||
} else if e == nil && i.IsDir() {
|
||||
|
@@ -37,7 +37,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"testing"
|
||||
@@ -214,7 +214,7 @@ func LaunchMinio(host, accessKey, secretKey string) {
|
||||
defer DelTempFolder(tmp)
|
||||
|
||||
if _, minio, _, ok := runtime.Caller(0); ok {
|
||||
if err := exec.CommandContext(ctx, path.Join(path.Dir(minio), "minio"), "server", "--address", host, tmp).Run(); err != nil {
|
||||
if err := exec.CommandContext(ctx, filepath.Join(filepath.Dir(minio), "minio"), "server", "--address", host, tmp).Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
|
@@ -32,7 +32,6 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -103,7 +102,7 @@ func (c *cobra) ConfigureWriteConfig(basename string, defaultConfig func() io.Re
|
||||
return err
|
||||
}
|
||||
|
||||
if len(path.Ext(cfgFile)) > 0 && strings.ToLower(path.Ext(cfgFile)) != ".json" {
|
||||
if len(filepath.Ext(cfgFile)) > 0 && strings.ToLower(filepath.Ext(cfgFile)) != ".json" {
|
||||
var mod = make(map[string]interface{}, 0)
|
||||
|
||||
err = json.Unmarshal(buf, &mod)
|
||||
@@ -111,13 +110,13 @@ func (c *cobra) ConfigureWriteConfig(basename string, defaultConfig func() io.Re
|
||||
return err
|
||||
}
|
||||
|
||||
switch strings.ToLower(path.Ext(cfgFile)) {
|
||||
switch strings.ToLower(filepath.Ext(cfgFile)) {
|
||||
case ".toml":
|
||||
buf, err = toml.Marshal(mod)
|
||||
case ".yml", ".yaml":
|
||||
buf, err = yaml.Marshal(mod)
|
||||
default:
|
||||
return fmt.Errorf("extension file '%s' not compatible", path.Ext(cfgFile))
|
||||
return fmt.Errorf("extension file '%s' not compatible", filepath.Ext(cfgFile))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -29,7 +29,7 @@ package cobra
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
liblog "github.com/nabbar/golib/logger"
|
||||
@@ -142,11 +142,11 @@ func (c *cobra) getLog() liblog.Logger {
|
||||
}
|
||||
|
||||
func (c *cobra) getPackageName() string {
|
||||
pkg := path.Base(os.Args[0])
|
||||
pkg := filepath.Base(os.Args[0])
|
||||
|
||||
if pkg == "" {
|
||||
if f, e := os.Executable(); e == nil {
|
||||
pkg = path.Base(f)
|
||||
pkg = filepath.Base(f)
|
||||
} else {
|
||||
pkg = c.s.GetPackage()
|
||||
}
|
||||
|
@@ -28,18 +28,31 @@ package errors
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
currPkgs = path.Base(reflect.TypeOf(UNK_ERROR).PkgPath())
|
||||
filterPkg = path.Clean(reflect.TypeOf(UNK_ERROR).PkgPath())
|
||||
const (
|
||||
PathSeparator = "/"
|
||||
pathVendor = "vendor"
|
||||
pathMod = "mod"
|
||||
pathPkg = "pkg"
|
||||
pkgRuntime = "runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
filterPkg = path.Clean(ConvPathFromLocal(reflect.TypeOf(UNK_ERROR).PkgPath()))
|
||||
currPkgs = path.Base(ConvPathFromLocal(filterPkg))
|
||||
)
|
||||
|
||||
func ConvPathFromLocal(str string) string {
|
||||
return strings.Replace(str, string(filepath.Separator), PathSeparator, -1)
|
||||
}
|
||||
|
||||
func init() {
|
||||
if i := strings.LastIndex(filterPkg, "/vendor/"); i != -1 {
|
||||
if i := strings.LastIndex(filterPkg, PathSeparator+pathVendor+PathSeparator); i != -1 {
|
||||
filterPkg = filterPkg[:i+1]
|
||||
}
|
||||
}
|
||||
@@ -101,9 +114,9 @@ func getFrameVendor() []runtime.Frame {
|
||||
|
||||
if strings.Contains(item.Function, currPkgs) {
|
||||
continue
|
||||
} else if strings.Contains(frame.File, "/vendor/") {
|
||||
} else if strings.Contains(ConvPathFromLocal(frame.File), PathSeparator+pathVendor+PathSeparator) {
|
||||
continue
|
||||
} else if strings.HasPrefix(frame.Function, "runtime") {
|
||||
} else if strings.HasPrefix(frame.Function, pkgRuntime) {
|
||||
continue
|
||||
} else if frameInSlice(res, item) {
|
||||
continue
|
||||
@@ -150,10 +163,12 @@ func getNilFrame() runtime.Frame {
|
||||
|
||||
func filterPath(pathname string) string {
|
||||
var (
|
||||
filterMod = "/pkg/mod/"
|
||||
filterVendor = "/vendor/"
|
||||
filterMod = PathSeparator + pathPkg + PathSeparator + pathMod + PathSeparator
|
||||
filterVendor = PathSeparator + pathVendor + PathSeparator
|
||||
)
|
||||
|
||||
pathname = ConvPathFromLocal(pathname)
|
||||
|
||||
if i := strings.LastIndex(pathname, filterMod); i != -1 {
|
||||
i = i + len(filterMod)
|
||||
pathname = pathname[i:]
|
||||
@@ -171,5 +186,5 @@ func filterPath(pathname string) string {
|
||||
|
||||
pathname = path.Clean(pathname)
|
||||
|
||||
return strings.Trim(pathname, "/")
|
||||
return strings.Trim(pathname, PathSeparator)
|
||||
}
|
||||
|
5
go.mod
5
go.mod
@@ -44,7 +44,7 @@ require (
|
||||
github.com/xhit/go-simple-mail v2.2.2+incompatible
|
||||
github.com/xujiajun/nutsdb v0.11.1
|
||||
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
|
||||
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab
|
||||
golang.org/x/exp v0.0.0-20221114191408-850992195362
|
||||
golang.org/x/net v0.2.0
|
||||
golang.org/x/oauth2 v0.2.0
|
||||
golang.org/x/sync v0.1.0
|
||||
@@ -63,7 +63,6 @@ require (
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
|
||||
github.com/ClickHouse/ch-go v0.49.0 // indirect
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.3.0 // indirect
|
||||
github.com/DataDog/zstd v1.5.2 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver v1.5.0 // indirect
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
|
||||
@@ -176,7 +175,7 @@ require (
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
|
||||
github.com/segmentio/asm v1.2.0 // indirect
|
||||
github.com/shopspring/decimal v1.3.1 // indirect
|
||||
github.com/spf13/afero v1.9.2 // indirect
|
||||
github.com/spf13/afero v1.9.3 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
||||
|
@@ -30,7 +30,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
)
|
||||
@@ -240,7 +240,7 @@ func (f *fileProgress) FilePath() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
return path.Clean(f.fs.Name())
|
||||
return filepath.Clean(f.fs.Name())
|
||||
}
|
||||
|
||||
func (f *fileProgress) FileStat() (os.FileInfo, liberr.Error) {
|
||||
|
@@ -26,15 +26,14 @@
|
||||
package ioutils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
. "github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
func NewTempFile() (*os.File, Error) {
|
||||
f, e := ioutil.TempFile(os.TempDir(), "")
|
||||
f, e := os.CreateTemp(os.TempDir(), "")
|
||||
return f, ErrorIOFileTempNew.Iferror(e)
|
||||
}
|
||||
|
||||
@@ -43,7 +42,7 @@ func GetTempFilePath(f *os.File) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
return path.Join(os.TempDir(), path.Base(f.Name()))
|
||||
return filepath.Join(os.TempDir(), filepath.Base(f.Name()))
|
||||
}
|
||||
|
||||
func DelTempFile(f *os.File) Error {
|
||||
|
@@ -40,6 +40,8 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -452,7 +454,7 @@ func (l *logger) getCaller() runtime.Frame {
|
||||
}
|
||||
|
||||
func (l *logger) filterPath(pathname string) string {
|
||||
var ()
|
||||
pathname = liberr.ConvPathFromLocal(pathname)
|
||||
|
||||
if i := strings.LastIndex(pathname, _TraceFilterMod); i != -1 {
|
||||
i = i + len(_TraceFilterMod)
|
||||
@@ -473,5 +475,5 @@ func (l *logger) filterPath(pathname string) string {
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Trim(path.Clean(pathname), "/")
|
||||
return strings.Trim(path.Clean(pathname), liberr.PathSeparator)
|
||||
}
|
||||
|
11
mail/mail.go
11
mail/mail.go
@@ -29,7 +29,7 @@ import (
|
||||
"io"
|
||||
mime2 "mime"
|
||||
"net/textproto"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
@@ -38,6 +38,7 @@ import (
|
||||
const (
|
||||
DateTimeLayout = time.RFC1123Z
|
||||
|
||||
mimeDownload = "application/octet-stream"
|
||||
headerMimeVersion = "MIME-Version"
|
||||
headerDate = "Date"
|
||||
headerSubject = "Subject"
|
||||
@@ -243,14 +244,14 @@ func (m *mail) AddAttachment(name string, mime string, data io.ReadCloser, inlin
|
||||
}
|
||||
}
|
||||
|
||||
func (m *mail) AttachFile(filepath string, data io.ReadCloser, inline bool) {
|
||||
mime := mime2.TypeByExtension(path.Ext(filepath))
|
||||
func (m *mail) AttachFile(filePath string, data io.ReadCloser, inline bool) {
|
||||
mime := mime2.TypeByExtension(filepath.Ext(filePath))
|
||||
|
||||
if mime == "" {
|
||||
mime = "application/octet-stream"
|
||||
mime = mimeDownload
|
||||
}
|
||||
|
||||
m.AddAttachment(path.Base(filepath), mime, data, inline)
|
||||
m.AddAttachment(filepath.Base(filePath), mime, data, inline)
|
||||
}
|
||||
|
||||
func (m *mail) GetAttachment(inline bool) []File {
|
||||
|
@@ -33,7 +33,7 @@ package nutsdb
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/nabbar/golib/archive"
|
||||
liberr "github.com/nabbar/golib/errors"
|
||||
@@ -156,7 +156,7 @@ func (s *snap) Load(opt Options, reader io.Reader) liberr.Error {
|
||||
return e
|
||||
}
|
||||
|
||||
if e = archive.ExtractAll(a, path.Base(arc), out, opt.Permission()); e != nil {
|
||||
if e = archive.ExtractAll(a, filepath.Base(arc), out, opt.Permission()); e != nil {
|
||||
return ErrorFolderExtract.Error(e)
|
||||
}
|
||||
|
||||
|
@@ -36,7 +36,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -438,7 +438,7 @@ func (r *request) SetPath(raw bool, path string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *request) AddPath(raw bool, path ...string) {
|
||||
func (r *request) AddPath(raw bool, pathPart ...string) {
|
||||
r.s.Lock()
|
||||
defer r.s.Unlock()
|
||||
|
||||
@@ -453,22 +453,22 @@ func (r *request) AddPath(raw bool, path ...string) {
|
||||
str = strings.Replace(r.u.Path, "/", string(os.PathSeparator), -1)
|
||||
}
|
||||
|
||||
for i := range path {
|
||||
if strings.HasSuffix(str, "/") && strings.HasPrefix(path[i], "/") {
|
||||
path[i] = strings.TrimPrefix(path[i], "/")
|
||||
for i := range pathPart {
|
||||
if strings.HasSuffix(str, "/") && strings.HasPrefix(pathPart[i], "/") {
|
||||
pathPart[i] = strings.TrimPrefix(pathPart[i], "/")
|
||||
}
|
||||
|
||||
if strings.HasSuffix(path[i], "/") {
|
||||
path[i] = strings.TrimSuffix(path[i], "/")
|
||||
if strings.HasSuffix(pathPart[i], "/") {
|
||||
pathPart[i] = strings.TrimSuffix(pathPart[i], "/")
|
||||
}
|
||||
|
||||
str = filepath.Join(str, path[i])
|
||||
str = path.Join(str, pathPart[i])
|
||||
}
|
||||
|
||||
if raw {
|
||||
r.u.RawPath = strings.Replace(str, string(os.PathSeparator), "/", -1)
|
||||
r.u.RawPath = path.Clean(str)
|
||||
} else {
|
||||
r.u.Path = strings.Replace(str, string(os.PathSeparator), "/", -1)
|
||||
r.u.Path = path.Clean(str)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -55,7 +55,10 @@ import (
|
||||
libsts "github.com/nabbar/golib/status"
|
||||
)
|
||||
|
||||
const _textEmbed = "Embed FS"
|
||||
const (
|
||||
urlPathSeparator = "/"
|
||||
textEmbed = "Embed FS"
|
||||
)
|
||||
|
||||
type staticHandler struct {
|
||||
m sync.Mutex
|
||||
@@ -74,7 +77,7 @@ type staticHandler struct {
|
||||
|
||||
func (s *staticHandler) _makeRoute(group, route string) string {
|
||||
if group == "" {
|
||||
group = "/"
|
||||
group = urlPathSeparator
|
||||
}
|
||||
return path.Join(group, route)
|
||||
}
|
||||
@@ -408,17 +411,17 @@ func (s *staticHandler) _fileTemp(pathFile string) (libiot.FileProgress, liberr.
|
||||
}
|
||||
|
||||
func (s *staticHandler) RegisterRouter(route string, register librtr.RegisterRouter, router ...gin.HandlerFunc) {
|
||||
s._setRouter(append(s._getRouter(), s._makeRoute("/", route)))
|
||||
s._setRouter(append(s._getRouter(), s._makeRoute(urlPathSeparator, route)))
|
||||
|
||||
router = append(router, s.Get)
|
||||
register(http.MethodGet, path.Join(route, "/*file"), router...)
|
||||
register(http.MethodGet, path.Join(route, urlPathSeparator+"*file"), router...)
|
||||
}
|
||||
|
||||
func (s *staticHandler) RegisterRouterInGroup(route, group string, register librtr.RegisterRouterInGroup, router ...gin.HandlerFunc) {
|
||||
s._setRouter(append(s._getRouter(), s._makeRoute(group, route)))
|
||||
|
||||
router = append(router, s.Get)
|
||||
register(group, http.MethodGet, path.Join(route, "/*file"), router...)
|
||||
register(group, http.MethodGet, path.Join(route, urlPathSeparator+"*file"), router...)
|
||||
}
|
||||
|
||||
func (s *staticHandler) RegisterLogger(log func() liblog.Logger) {
|
||||
@@ -642,9 +645,9 @@ func (s *staticHandler) _statusInfoPath(pathFile string) (name string, release s
|
||||
vers = strings.TrimLeft(vers, "GO")
|
||||
|
||||
if inf, err := s._fileInfo(pathFile); err != nil {
|
||||
return _textEmbed, vers, ""
|
||||
return textEmbed, vers, ""
|
||||
} else {
|
||||
return fmt.Sprintf("%s [%s]", _textEmbed, inf.Name()), vers, ""
|
||||
return fmt.Sprintf("%s [%s]", textEmbed, inf.Name()), vers, ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -680,7 +683,7 @@ func (s *staticHandler) _statusComponentPath(pathFile string, mandatory bool, me
|
||||
|
||||
func (s *staticHandler) StatusComponent(mandatory bool, message libsts.FctMessage, infoCacheTimeout, healthCacheTimeout time.Duration, sts libsts.RouteStatus) {
|
||||
for _, p := range s._getBase() {
|
||||
name := fmt.Sprintf("%s-%s", strings.ReplaceAll(_textEmbed, " ", "."), p)
|
||||
name := fmt.Sprintf("%s-%s", strings.ReplaceAll(textEmbed, " ", "."), p)
|
||||
sts.ComponentNew(name, s._statusComponentPath(p, mandatory, message, infoCacheTimeout, healthCacheTimeout))
|
||||
}
|
||||
}
|
||||
@@ -705,14 +708,14 @@ func (s *staticHandler) Get(c *gin.Context) {
|
||||
calledFile = idx
|
||||
} else {
|
||||
for _, p := range s._getRouter() {
|
||||
if p == "/" {
|
||||
if p == urlPathSeparator {
|
||||
continue
|
||||
}
|
||||
calledFile = strings.TrimLeft(calledFile, p)
|
||||
}
|
||||
}
|
||||
|
||||
calledFile = strings.Trim(calledFile, "/")
|
||||
calledFile = strings.Trim(calledFile, urlPathSeparator)
|
||||
|
||||
if !s.Has(calledFile) {
|
||||
for _, p := range s._getBase() {
|
||||
|
@@ -62,6 +62,7 @@ type rtrStatus struct {
|
||||
}
|
||||
|
||||
const (
|
||||
urlPathSeparator = "/"
|
||||
keyShortOutput = "short"
|
||||
keyOneLineOutput = "oneline"
|
||||
)
|
||||
@@ -81,7 +82,7 @@ func (r *rtrStatus) MiddlewareAdd(mdw ...gin.HandlerFunc) {
|
||||
}
|
||||
|
||||
func (r *rtrStatus) cleanPrefix(prefix string) string {
|
||||
return path.Clean(strings.TrimRight(path.Join("/", prefix), "/"))
|
||||
return path.Clean(strings.TrimRight(path.Join(urlPathSeparator, prefix), urlPathSeparator))
|
||||
}
|
||||
|
||||
func (r *rtrStatus) Register(prefix string, register librtr.RegisterRouter) {
|
||||
@@ -91,8 +92,8 @@ func (r *rtrStatus) Register(prefix string, register librtr.RegisterRouter) {
|
||||
m = append(m, r.Get)
|
||||
register(http.MethodGet, prefix, m...)
|
||||
|
||||
if prefix != "/" {
|
||||
register(http.MethodGet, prefix+"/", m...)
|
||||
if prefix != urlPathSeparator {
|
||||
register(http.MethodGet, prefix+urlPathSeparator, m...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,8 +104,8 @@ func (r *rtrStatus) RegisterGroup(group, prefix string, register librtr.Register
|
||||
m = append(m, r.Get)
|
||||
register(group, http.MethodGet, prefix, m...)
|
||||
|
||||
if prefix != "/" {
|
||||
register(group, http.MethodGet, prefix+"/", m...)
|
||||
if prefix != urlPathSeparator {
|
||||
register(group, http.MethodGet, prefix+urlPathSeparator, m...)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,3 +1,7 @@
|
||||
//+build examples
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/***********************************************************************************************************************
|
||||
*
|
||||
* MIT License
|
||||
|
@@ -1,3 +1,7 @@
|
||||
//+build examples
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/***********************************************************************************************************************
|
||||
*
|
||||
* MIT License
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/***********************************************************************************************************************
|
||||
*
|
||||
* MIT License
|
||||
|
@@ -1,3 +1,7 @@
|
||||
//+build examples
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,7 @@
|
||||
//+build examples
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/***********************************************************************************************************************
|
||||
*
|
||||
* MIT License
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,7 @@
|
||||
//+build examples
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -1,3 +1,6 @@
|
||||
//go:build examples
|
||||
// +build examples
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
|
@@ -27,7 +27,7 @@ package version
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -80,11 +80,11 @@ func NewVersion(License license, Package, Description, Date, Build, Release, Aut
|
||||
Source := rfl.PkgPath()
|
||||
|
||||
for i := 1; i <= numSubPackage; i++ {
|
||||
Source = path.Dir(Source)
|
||||
Source = filepath.Dir(Source)
|
||||
}
|
||||
|
||||
if Package == "" || Package == "noname" {
|
||||
Package = path.Base(Source)
|
||||
Package = filepath.Base(Source)
|
||||
}
|
||||
|
||||
return &versionModel{
|
||||
|
Reference in New Issue
Block a user