From ee0f8e964ddae189915f19905a864d6a1aa8a59a Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sat, 23 May 2020 13:57:02 +0800 Subject: [PATCH] Feat: eject internal static files --- assets | 2 +- bootstrap/static.go | 68 ++++++++++++++++++++++++++++++++++++++++++++- main.go | 12 +++++++- 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/assets b/assets index b44dc05..c2b6997 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit b44dc0514520580fc284937400743123bbb2c6d4 +Subproject commit c2b69970cb2405307e5dceb45297bc7afa756092 diff --git a/bootstrap/static.go b/bootstrap/static.go index e22ebd0..d3f8967 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -7,10 +7,14 @@ import ( _ "github.com/HFO4/cloudreve/statik" "github.com/gin-contrib/static" "github.com/rakyll/statik/fs" + "io" "io/ioutil" "net/http" + "path" ) +const StaticFolder = "statics" + type GinFS struct { FS http.FileSystem } @@ -42,7 +46,7 @@ func (b *GinFS) Exists(prefix string, filepath string) bool { func InitStatic() { var err error - if util.Exists(util.RelativePath("statics")) { + if util.Exists(util.RelativePath(StaticFolder)) { util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") 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("内置静态资源导出完成") +} diff --git a/main.go b/main.go index d65ff07..de6bce0 100644 --- a/main.go +++ b/main.go @@ -8,15 +8,25 @@ import ( "github.com/HFO4/cloudreve/routers" ) -var confPath string +var ( + isEject bool + confPath string +) func init() { flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") + flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.Parse() bootstrap.Init(confPath) } func main() { + if isEject { + // 开始导出内置静态资源文件 + bootstrap.Eject() + return + } + api := routers.InitRouter() // 如果启用了SSL -- GitLab