From 4d70f9fa3edfb8f4e42a30c4dcbc33410a502206 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 11 Mar 2020 10:32:35 +0800 Subject: [PATCH] Fix: get execute file path dynamically --- bootstrap/app.go | 2 +- bootstrap/static.go | 4 ++-- main.go | 2 +- pkg/crontab/collect.go | 2 +- pkg/filesystem/archive.go | 6 +++--- pkg/filesystem/driver/local/handler.go | 6 +++--- pkg/filesystem/image.go | 2 +- pkg/filesystem/upload.go | 2 +- pkg/thumb/image.go | 2 +- pkg/util/path.go | 11 +++++++++++ service/admin/policy.go | 2 +- service/user/setting.go | 2 +- 12 files changed, 27 insertions(+), 16 deletions(-) diff --git a/bootstrap/app.go b/bootstrap/app.go index b4d634f..9e1f3e5 100644 --- a/bootstrap/app.go +++ b/bootstrap/app.go @@ -31,7 +31,7 @@ V` + conf.BackendVersion + ` Commit #` + conf.LastCommit + ` Pro=` + conf.IsPr ================================================ `) - data, err := ioutil.ReadFile(string([]byte{107, 101, 121, 46, 98, 105, 110})) + data, err := ioutil.ReadFile(util.RelativePath(string([]byte{107, 101, 121, 46, 98, 105, 110}))) if err != nil { util.Log().Panic("%s", err) } diff --git a/bootstrap/static.go b/bootstrap/static.go index 6b9322a..f40ffe2 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -34,9 +34,9 @@ func (b *GinFS) Exists(prefix string, filepath string) bool { func InitStatic() { var err error - if util.Exists("statics") { + if util.Exists(util.RelativePath("statics")) { util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") - StaticFS = static.LocalFile("statics", false) + StaticFS = static.LocalFile(util.RelativePath("statics"), false) } else { StaticFS = &GinFS{} StaticFS.(*GinFS).FS, err = fs.New() diff --git a/main.go b/main.go index d847ade..a05bd5a 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( var confPath string func init() { - flag.StringVar(&confPath, "c", "conf.ini", "配置文件路径") + flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") flag.Parse() bootstrap.Init(confPath) } diff --git a/pkg/crontab/collect.go b/pkg/crontab/collect.go index 16fb01d..512f8b5 100644 --- a/pkg/crontab/collect.go +++ b/pkg/crontab/collect.go @@ -24,7 +24,7 @@ func garbageCollect() { func collectArchiveFile() { // 读取有效期、目录设置 - tempPath := model.GetSettingByName("temp_path") + tempPath := util.RelativePath(model.GetSettingByName("temp_path")) expires := model.GetIntSetting("download_timeout", 30) // 列出文件 diff --git a/pkg/filesystem/archive.go b/pkg/filesystem/archive.go index 2c53c56..f867de4 100644 --- a/pkg/filesystem/archive.go +++ b/pkg/filesystem/archive.go @@ -78,7 +78,7 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint, i saveFolder = "compress" } zipFilePath := filepath.Join( - model.GetSettingByName("temp_path"), + util.RelativePath(model.GetSettingByName("temp_path")), saveFolder, fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()), ) @@ -217,7 +217,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error { } tempZipFilePath = filepath.Join( - model.GetSettingByName("temp_path"), + util.RelativePath(model.GetSettingByName("temp_path")), "decompress", fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()), ) @@ -291,8 +291,8 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error { select { case <-worker: + wg.Add(1) go func(fileStream io.ReadCloser, size int64) { - wg.Add(1) defer func() { worker <- 1 wg.Done() diff --git a/pkg/filesystem/driver/local/handler.go b/pkg/filesystem/driver/local/handler.go index bf83a29..e0a54e5 100644 --- a/pkg/filesystem/driver/local/handler.go +++ b/pkg/filesystem/driver/local/handler.go @@ -26,7 +26,7 @@ type Driver struct { // Get 获取文件内容 func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) { // 打开文件 - file, err := os.Open(path) + file, err := os.Open(util.RelativePath(path)) if err != nil { util.Log().Debug("无法打开文件:%s", err) return nil, err @@ -51,7 +51,7 @@ func closeReader(ctx context.Context, closer io.Closer) { // Put 将文件流保存到指定目录 func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error { defer file.Close() - dst = filepath.FromSlash(dst) + dst = util.RelativePath(filepath.FromSlash(dst)) // 如果目标目录不存在,创建 basePath := filepath.Dir(dst) @@ -83,7 +83,7 @@ func (handler Driver) Delete(ctx context.Context, files []string) ([]string, err var retErr error for _, value := range files { - err := os.Remove(filepath.FromSlash(value)) + err := os.Remove(util.RelativePath(filepath.FromSlash(value))) if err != nil { util.Log().Warning("无法删除文件,%s", err) retErr = err diff --git a/pkg/filesystem/image.go b/pkg/filesystem/image.go index 6024e10..40ff3da 100644 --- a/pkg/filesystem/image.go +++ b/pkg/filesystem/image.go @@ -73,7 +73,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) { // 生成缩略图 image.GetThumb(fs.GenerateThumbnailSize(w, h)) // 保存到文件 - err = image.Save(file.SourceName + conf.ThumbConfig.FileSuffix) + err = image.Save(util.RelativePath(file.SourceName + conf.ThumbConfig.FileSuffix)) if err != nil { util.Log().Warning("无法保存缩略图:%s", err) return diff --git a/pkg/filesystem/upload.go b/pkg/filesystem/upload.go index ee446ef..7ca9b0f 100644 --- a/pkg/filesystem/upload.go +++ b/pkg/filesystem/upload.go @@ -235,7 +235,7 @@ func (fs *FileSystem) UploadFromPath(ctx context.Context, src, dst string) error return err } - file, err := os.Open(src) + file, err := os.Open(util.RelativePath(src)) if err != nil { return err } diff --git a/pkg/thumb/image.go b/pkg/thumb/image.go index c361990..4e25f89 100644 --- a/pkg/thumb/image.go +++ b/pkg/thumb/image.go @@ -83,7 +83,7 @@ func (image *Thumb) Save(path string) (err error) { // CreateAvatar 创建头像 func (image *Thumb) CreateAvatar(uid uint) error { // 读取头像相关设定 - savePath := model.GetSettingByName("avatar_path") + savePath := util.RelativePath(model.GetSettingByName("avatar_path")) s := model.GetIntSetting("avatar_size_s", 50) m := model.GetIntSetting("avatar_size_m", 130) l := model.GetIntSetting("avatar_size_l", 200) diff --git a/pkg/util/path.go b/pkg/util/path.go index a1b78cb..ff51d57 100644 --- a/pkg/util/path.go +++ b/pkg/util/path.go @@ -1,7 +1,9 @@ package util import ( + "os" "path" + "path/filepath" "strings" ) @@ -45,3 +47,12 @@ func SplitPath(path string) []string { func FormSlash(old string) string { return path.Clean(strings.ReplaceAll(old, "\\", "/")) } + +// RelativePath 获取相对可执行文件的路径 +func RelativePath(name string) string { + if filepath.IsAbs(name) { + return name + } + e, _ := os.Executable() + return filepath.Join(filepath.Dir(e), name) +} diff --git a/service/admin/policy.go b/service/admin/policy.go index 3e36bb8..af8bd42 100644 --- a/service/admin/policy.go +++ b/service/admin/policy.go @@ -264,7 +264,7 @@ func (service *PathTestService) Test() serializer.Response { policy := model.Policy{DirNameRule: service.Path} path := policy.GeneratePath(1, "/My File") path = filepath.Join(path, "test.txt") - file, err := util.CreatNestedFile(path) + file, err := util.CreatNestedFile(util.RelativePath(path)) if err != nil { return serializer.ParamErr(fmt.Sprintf("无法创建路径 %s , %s", path, err.Error()), nil) } diff --git a/service/user/setting.go b/service/user/setting.go index d5d8811..59cb399 100644 --- a/service/user/setting.go +++ b/service/user/setting.go @@ -294,7 +294,7 @@ func (service *AvatarService) Get(c *gin.Context) serializer.Response { // 本地文件头像 if user.Avatar == "file" { - avatarRoot := model.GetSettingByName("avatar_path") + avatarRoot := util.RelativePath(model.GetSettingByName("avatar_path")) sizeToInt := map[string]string{ "s": "0", "m": "1",