mirror of https://github.com/cloudreve/Cloudreve
Feat: eject internal static files
parent
60745ac8ba
commit
ee0f8e964d
2
assets
2
assets
|
@ -1 +1 @@
|
||||||
Subproject commit b44dc0514520580fc284937400743123bbb2c6d4
|
Subproject commit c2b69970cb2405307e5dceb45297bc7afa756092
|
|
@ -7,10 +7,14 @@ import (
|
||||||
_ "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"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const StaticFolder = "statics"
|
||||||
|
|
||||||
type GinFS struct {
|
type GinFS struct {
|
||||||
FS http.FileSystem
|
FS http.FileSystem
|
||||||
}
|
}
|
||||||
|
@ -42,7 +46,7 @@ func (b *GinFS) Exists(prefix string, filepath string) bool {
|
||||||
func InitStatic() {
|
func InitStatic() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if util.Exists(util.RelativePath("statics")) {
|
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("statics"), false)
|
||||||
|
|
||||||
|
@ -89,3 +93,65 @@ func InitStatic() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Eject 抽离内置静态资源
|
||||||
|
func Eject() {
|
||||||
|
staticFS, err := fs.New()
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Panic("无法初始化静态资源, %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
root, err := staticFS.Open("/")
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Panic("根目录不存在, %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var walk func(relPath string, object http.File)
|
||||||
|
walk = func(relPath string, object http.File) {
|
||||||
|
stat, err := object.Stat()
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Error("无法获取[%s]的信息, %s, 跳过...", relPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
util.Log().Info("导出 [%s]...", relPath)
|
||||||
|
if _, err := io.Copy(out, object); err != nil {
|
||||||
|
util.Log().Error("无法写入文件[%s], %s, 跳过...", relPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 列出目录
|
||||||
|
objects, err := object.Readdir(0)
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Error("无法步入子目录[%s], %s, 跳过...", relPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递归遍历子目录
|
||||||
|
for _, newObject := range objects {
|
||||||
|
newPath := path.Join(relPath, newObject.Name())
|
||||||
|
newRoot, err := staticFS.Open(newPath)
|
||||||
|
if err != nil {
|
||||||
|
util.Log().Error("无法打开对象[%s], %s, 跳过...", newPath, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
walk(newPath, newRoot)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
util.Log().Info("开始导出内置静态资源...")
|
||||||
|
walk("/", root)
|
||||||
|
util.Log().Info("内置静态资源导出完成")
|
||||||
|
}
|
||||||
|
|
12
main.go
12
main.go
|
@ -8,15 +8,25 @@ import (
|
||||||
"github.com/HFO4/cloudreve/routers"
|
"github.com/HFO4/cloudreve/routers"
|
||||||
)
|
)
|
||||||
|
|
||||||
var confPath string
|
var (
|
||||||
|
isEject bool
|
||||||
|
confPath string
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
|
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
|
||||||
|
flag.BoolVar(&isEject, "eject", false, "导出内置静态资源")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
bootstrap.Init(confPath)
|
bootstrap.Init(confPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if isEject {
|
||||||
|
// 开始导出内置静态资源文件
|
||||||
|
bootstrap.Eject()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
api := routers.InitRouter()
|
api := routers.InitRouter()
|
||||||
|
|
||||||
// 如果启用了SSL
|
// 如果启用了SSL
|
||||||
|
|
Loading…
Reference in New Issue