feat(RelativePath): change to get abs path from the runtime directory

change to get abs path from the runtime directory

Signed-off-by: mritd <mritd@linux.com>
pull/560/head
mritd 5 years ago
parent 7df09537e0
commit ff976132bf
No known key found for this signature in database
GPG Key ID: 98C41327E6D3E645

@ -2,15 +2,16 @@ package bootstrap
import ( import (
"encoding/json" "encoding/json"
"io"
"io/ioutil"
"net/http"
"path"
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
_ "github.com/HFO4/cloudreve/statik" _ "github.com/HFO4/cloudreve/statik"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
"github.com/rakyll/statik/fs" "github.com/rakyll/statik/fs"
"io"
"io/ioutil"
"net/http"
"path"
) )
const StaticFolder = "statics" const StaticFolder = "statics"
@ -33,13 +34,11 @@ func (b *GinFS) Open(name string) (http.File, error) {
} }
// Exists 文件是否存在 // 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 { if _, err := b.FS.Open(filepath); err != nil {
return false return false
} }
return true return true
} }
// InitStatic 初始化静态资源文件 // InitStatic 初始化静态资源文件
@ -48,7 +47,7 @@ func InitStatic() {
if util.Exists(util.RelativePath(StaticFolder)) { if util.Exists(util.RelativePath(StaticFolder)) {
util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件")
StaticFS = static.LocalFile(util.RelativePath("statics"), false) StaticFS = static.LocalFile(util.RelativePath(StaticFolder), false)
// 检查静态资源的版本 // 检查静态资源的版本
f, err := StaticFS.Open("version.json") f, err := StaticFS.Open("version.json")
@ -117,12 +116,11 @@ func Eject() {
if !stat.IsDir() { if !stat.IsDir() {
// 写入文件 // 写入文件
out, err := util.CreatNestedFile(util.RelativePath(StaticFolder + relPath)) out, err := util.CreatNestedFile(util.RelativePath(StaticFolder + relPath))
defer out.Close()
if err != nil { if err != nil {
util.Log().Error("无法创建文件[%s], %s, 跳过...", relPath, err) util.Log().Error("无法创建文件[%s], %s, 跳过...", relPath, err)
return return
} }
defer out.Close()
util.Log().Info("导出 [%s]...", relPath) util.Log().Info("导出 [%s]...", relPath)
if _, err := io.Copy(out, object); err != nil { if _, err := io.Copy(out, object); err != nil {

@ -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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 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.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/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 h1:dqpmYaez7VBT7PCRBcBxkzlDOiTk7Td8ATiia1b1GuE=
github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4= github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=

@ -2,6 +2,13 @@ package local
import ( import (
"context" "context"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
model "github.com/HFO4/cloudreve/models" model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/auth" "github.com/HFO4/cloudreve/pkg/auth"
"github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/conf"
@ -9,12 +16,6 @@ import (
"github.com/HFO4/cloudreve/pkg/util" "github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
) )
func TestHandler_Put(t *testing.T) { func TestHandler_Put(t *testing.T) {

@ -53,6 +53,6 @@ func RelativePath(name string) string {
if filepath.IsAbs(name) { if filepath.IsAbs(name) {
return name return name
} }
e, _ := os.Executable() e, _ := filepath.Abs(filepath.Dir(os.Args[0]))
return filepath.Join(filepath.Dir(e), name) return filepath.Join(e, name)
} }

Loading…
Cancel
Save