From ff976132bfbb9b0552b860465afb2d99f4c05e90 Mon Sep 17 00:00:00 2001 From: mritd Date: Tue, 8 Sep 2020 14:30:44 +0800 Subject: [PATCH] feat(RelativePath): change to get abs path from the runtime directory change to get abs path from the runtime directory Signed-off-by: mritd --- bootstrap/static.go | 18 ++++++++---------- go.sum | 1 + pkg/filesystem/driver/local/handler_test.go | 13 +++++++------ pkg/util/path.go | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/bootstrap/static.go b/bootstrap/static.go index d3f89679..680ab9cd 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -2,15 +2,16 @@ package bootstrap import ( "encoding/json" + "io" + "io/ioutil" + "net/http" + "path" + "github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/util" _ "github.com/HFO4/cloudreve/statik" "github.com/gin-contrib/static" "github.com/rakyll/statik/fs" - "io" - "io/ioutil" - "net/http" - "path" ) const StaticFolder = "statics" @@ -33,13 +34,11 @@ func (b *GinFS) Open(name string) (http.File, error) { } // Exists 文件是否存在 -func (b *GinFS) Exists(prefix string, filepath string) bool { - +func (b *GinFS) Exists(_ string, filepath string) bool { if _, err := b.FS.Open(filepath); err != nil { return false } return true - } // InitStatic 初始化静态资源文件 @@ -48,7 +47,7 @@ func InitStatic() { if util.Exists(util.RelativePath(StaticFolder)) { util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") - StaticFS = static.LocalFile(util.RelativePath("statics"), false) + StaticFS = static.LocalFile(util.RelativePath(StaticFolder), false) // 检查静态资源的版本 f, err := StaticFS.Open("version.json") @@ -117,12 +116,11 @@ func Eject() { if !stat.IsDir() { // 写入文件 out, err := util.CreatNestedFile(util.RelativePath(StaticFolder + relPath)) - defer out.Close() - if err != nil { util.Log().Error("无法创建文件[%s], %s, 跳过...", relPath, err) return } + defer out.Close() util.Log().Info("导出 [%s]...", relPath) if _, err := io.Copy(out, object); err != nil { diff --git a/go.sum b/go.sum index 00f2c4de..bb6ef148 100644 --- a/go.sum +++ b/go.sum @@ -239,6 +239,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible h1:dqpmYaez7VBT7PCRBcBxkzlDOiTk7Td8ATiia1b1GuE= github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4= diff --git a/pkg/filesystem/driver/local/handler_test.go b/pkg/filesystem/driver/local/handler_test.go index e0c3ddba..314781a3 100644 --- a/pkg/filesystem/driver/local/handler_test.go +++ b/pkg/filesystem/driver/local/handler_test.go @@ -2,6 +2,13 @@ package local import ( "context" + "io" + "io/ioutil" + "net/url" + "os" + "strings" + "testing" + model "github.com/HFO4/cloudreve/models" "github.com/HFO4/cloudreve/pkg/auth" "github.com/HFO4/cloudreve/pkg/conf" @@ -9,12 +16,6 @@ import ( "github.com/HFO4/cloudreve/pkg/util" "github.com/jinzhu/gorm" "github.com/stretchr/testify/assert" - "io" - "io/ioutil" - "net/url" - "os" - "strings" - "testing" ) func TestHandler_Put(t *testing.T) { diff --git a/pkg/util/path.go b/pkg/util/path.go index ff51d579..66690976 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -53,6 +53,6 @@ func RelativePath(name string) string { if filepath.IsAbs(name) { return name } - e, _ := os.Executable() - return filepath.Join(filepath.Dir(e), name) + e, _ := filepath.Abs(filepath.Dir(os.Args[0])) + return filepath.Join(e, name) }