提交 4d70f9fa 编写于 作者: H HFO4

Fix: get execute file path dynamically

上级 b6e1e04c
...@@ -31,7 +31,7 @@ V` + conf.BackendVersion + ` Commit #` + conf.LastCommit + ` Pro=` + conf.IsPr ...@@ -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 { if err != nil {
util.Log().Panic("%s", err) util.Log().Panic("%s", err)
} }
......
...@@ -34,9 +34,9 @@ func (b *GinFS) Exists(prefix string, filepath string) bool { ...@@ -34,9 +34,9 @@ func (b *GinFS) Exists(prefix string, filepath string) bool {
func InitStatic() { func InitStatic() {
var err error var err error
if util.Exists("statics") { if util.Exists(util.RelativePath("statics")) {
util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件")
StaticFS = static.LocalFile("statics", false) StaticFS = static.LocalFile(util.RelativePath("statics"), false)
} else { } else {
StaticFS = &GinFS{} StaticFS = &GinFS{}
StaticFS.(*GinFS).FS, err = fs.New() StaticFS.(*GinFS).FS, err = fs.New()
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
var confPath string var confPath string
func init() { func init() {
flag.StringVar(&confPath, "c", "conf.ini", "配置文件路径") flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
flag.Parse() flag.Parse()
bootstrap.Init(confPath) bootstrap.Init(confPath)
} }
......
...@@ -24,7 +24,7 @@ func garbageCollect() { ...@@ -24,7 +24,7 @@ func garbageCollect() {
func collectArchiveFile() { func collectArchiveFile() {
// 读取有效期、目录设置 // 读取有效期、目录设置
tempPath := model.GetSettingByName("temp_path") tempPath := util.RelativePath(model.GetSettingByName("temp_path"))
expires := model.GetIntSetting("download_timeout", 30) expires := model.GetIntSetting("download_timeout", 30)
// 列出文件 // 列出文件
......
...@@ -78,7 +78,7 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint, i ...@@ -78,7 +78,7 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint, i
saveFolder = "compress" saveFolder = "compress"
} }
zipFilePath := filepath.Join( zipFilePath := filepath.Join(
model.GetSettingByName("temp_path"), util.RelativePath(model.GetSettingByName("temp_path")),
saveFolder, saveFolder,
fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()), fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()),
) )
...@@ -217,7 +217,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error { ...@@ -217,7 +217,7 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
} }
tempZipFilePath = filepath.Join( tempZipFilePath = filepath.Join(
model.GetSettingByName("temp_path"), util.RelativePath(model.GetSettingByName("temp_path")),
"decompress", "decompress",
fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()), fmt.Sprintf("archive_%d.zip", time.Now().UnixNano()),
) )
...@@ -291,8 +291,8 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error { ...@@ -291,8 +291,8 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
select { select {
case <-worker: case <-worker:
wg.Add(1)
go func(fileStream io.ReadCloser, size int64) { go func(fileStream io.ReadCloser, size int64) {
wg.Add(1)
defer func() { defer func() {
worker <- 1 worker <- 1
wg.Done() wg.Done()
......
...@@ -26,7 +26,7 @@ type Driver struct { ...@@ -26,7 +26,7 @@ type Driver struct {
// Get 获取文件内容 // Get 获取文件内容
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) { 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 { if err != nil {
util.Log().Debug("无法打开文件:%s", err) util.Log().Debug("无法打开文件:%s", err)
return nil, err return nil, err
...@@ -51,7 +51,7 @@ func closeReader(ctx context.Context, closer io.Closer) { ...@@ -51,7 +51,7 @@ func closeReader(ctx context.Context, closer io.Closer) {
// Put 将文件流保存到指定目录 // Put 将文件流保存到指定目录
func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error { func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
defer file.Close() defer file.Close()
dst = filepath.FromSlash(dst) dst = util.RelativePath(filepath.FromSlash(dst))
// 如果目标目录不存在,创建 // 如果目标目录不存在,创建
basePath := filepath.Dir(dst) basePath := filepath.Dir(dst)
...@@ -83,7 +83,7 @@ func (handler Driver) Delete(ctx context.Context, files []string) ([]string, err ...@@ -83,7 +83,7 @@ func (handler Driver) Delete(ctx context.Context, files []string) ([]string, err
var retErr error var retErr error
for _, value := range files { for _, value := range files {
err := os.Remove(filepath.FromSlash(value)) err := os.Remove(util.RelativePath(filepath.FromSlash(value)))
if err != nil { if err != nil {
util.Log().Warning("无法删除文件,%s", err) util.Log().Warning("无法删除文件,%s", err)
retErr = err retErr = err
......
...@@ -73,7 +73,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) { ...@@ -73,7 +73,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
// 生成缩略图 // 生成缩略图
image.GetThumb(fs.GenerateThumbnailSize(w, h)) 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 { if err != nil {
util.Log().Warning("无法保存缩略图:%s", err) util.Log().Warning("无法保存缩略图:%s", err)
return return
......
...@@ -235,7 +235,7 @@ func (fs *FileSystem) UploadFromPath(ctx context.Context, src, dst string) error ...@@ -235,7 +235,7 @@ func (fs *FileSystem) UploadFromPath(ctx context.Context, src, dst string) error
return err return err
} }
file, err := os.Open(src) file, err := os.Open(util.RelativePath(src))
if err != nil { if err != nil {
return err return err
} }
......
...@@ -83,7 +83,7 @@ func (image *Thumb) Save(path string) (err error) { ...@@ -83,7 +83,7 @@ func (image *Thumb) Save(path string) (err error) {
// CreateAvatar 创建头像 // CreateAvatar 创建头像
func (image *Thumb) CreateAvatar(uid uint) error { 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) s := model.GetIntSetting("avatar_size_s", 50)
m := model.GetIntSetting("avatar_size_m", 130) m := model.GetIntSetting("avatar_size_m", 130)
l := model.GetIntSetting("avatar_size_l", 200) l := model.GetIntSetting("avatar_size_l", 200)
......
package util package util
import ( import (
"os"
"path" "path"
"path/filepath"
"strings" "strings"
) )
...@@ -45,3 +47,12 @@ func SplitPath(path string) []string { ...@@ -45,3 +47,12 @@ func SplitPath(path string) []string {
func FormSlash(old string) string { func FormSlash(old string) string {
return path.Clean(strings.ReplaceAll(old, "\\", "/")) 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)
}
...@@ -264,7 +264,7 @@ func (service *PathTestService) Test() serializer.Response { ...@@ -264,7 +264,7 @@ func (service *PathTestService) Test() serializer.Response {
policy := model.Policy{DirNameRule: service.Path} policy := model.Policy{DirNameRule: service.Path}
path := policy.GeneratePath(1, "/My File") path := policy.GeneratePath(1, "/My File")
path = filepath.Join(path, "test.txt") path = filepath.Join(path, "test.txt")
file, err := util.CreatNestedFile(path) file, err := util.CreatNestedFile(util.RelativePath(path))
if err != nil { if err != nil {
return serializer.ParamErr(fmt.Sprintf("无法创建路径 %s , %s", path, err.Error()), nil) return serializer.ParamErr(fmt.Sprintf("无法创建路径 %s , %s", path, err.Error()), nil)
} }
......
...@@ -294,7 +294,7 @@ func (service *AvatarService) Get(c *gin.Context) serializer.Response { ...@@ -294,7 +294,7 @@ func (service *AvatarService) Get(c *gin.Context) serializer.Response {
// 本地文件头像 // 本地文件头像
if user.Avatar == "file" { if user.Avatar == "file" {
avatarRoot := model.GetSettingByName("avatar_path") avatarRoot := util.RelativePath(model.GetSettingByName("avatar_path"))
sizeToInt := map[string]string{ sizeToInt := map[string]string{
"s": "0", "s": "0",
"m": "1", "m": "1",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册