Fix: get execute file path dynamically

pull/247/head
HFO4 4 years ago
parent b6e1e04ce0
commit 4d70f9fa3e

@ -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)
}

@ -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()

@ -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)
}

@ -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)
// 列出文件

@ -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()

@ -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

@ -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

@ -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
}

@ -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)

@ -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)
}

@ -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)
}

@ -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",

Loading…
Cancel
Save