diff --git a/bootstrap/static.go b/bootstrap/static.go index f40ffe22..39cf5bcf 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -1,10 +1,13 @@ package bootstrap import ( + "encoding/json" + "github.com/HFO4/cloudreve/pkg/conf" "github.com/HFO4/cloudreve/pkg/util" _ "github.com/HFO4/cloudreve/statik" "github.com/gin-contrib/static" "github.com/rakyll/statik/fs" + "io/ioutil" "net/http" ) @@ -12,6 +15,11 @@ type GinFS struct { FS http.FileSystem } +type staticVersion struct { + Name string `json:"name"` + Version string `json:"version"` +} + // StaticFS 内置静态文件资源 var StaticFS static.ServeFileSystem @@ -45,4 +53,38 @@ func InitStatic() { } } + // 检查静态资源的版本 + f, err := StaticFS.Open("version.json") + if err != nil { + util.Log().Warning("静态资源版本标识文件不存在,请重新构建或删除 statics 目录") + return + } + + b, err := ioutil.ReadAll(f) + if err != nil { + util.Log().Warning("无法读取静态资源文件版本,请重新构建或删除 statics 目录") + return + } + + var v staticVersion + if err := json.Unmarshal(b, &v); err != nil { + util.Log().Warning("无法解析静态资源文件版本, %s", err) + return + } + + staticName := "cloudreve-frontend" + if conf.IsPro == "true" { + staticName += "-pro" + } + + if v.Name != staticName { + util.Log().Warning("静态资源版本不匹配,请重新构建或删除 statics 目录") + return + } + + if v.Version != conf.RequiredStaticVersion { + util.Log().Warning("静态资源版本不匹配 [当前 %s, 需要: %s],请重新构建或删除 statics 目录", v.Version, conf.RequiredStaticVersion) + return + } + } diff --git a/models/init.go b/models/init.go index 47141999..9153f013 100644 --- a/models/init.go +++ b/models/init.go @@ -32,7 +32,7 @@ func Init() { // 未指定数据库时,使用SQLite db, err = gorm.Open("sqlite3", util.RelativePath(conf.DatabaseConfig.DBFile)) } else { - db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", + db, err = gorm.Open(conf.DatabaseConfig.Type, fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", conf.DatabaseConfig.User, conf.DatabaseConfig.Password, conf.DatabaseConfig.Host, diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 8bf72f58..d28d0434 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -15,7 +15,7 @@ type database struct { Name string TablePrefix string DBFile string - Port string + Port int } // system 系统通用配置 diff --git a/pkg/conf/defaults.go b/pkg/conf/defaults.go index f2b2fadb..f4ff903f 100644 --- a/pkg/conf/defaults.go +++ b/pkg/conf/defaults.go @@ -13,6 +13,7 @@ var RedisConfig = &redis{ var DatabaseConfig = &database{ Type: "UNSET", DBFile: "cloudreve.db", + Port: 3306, } // SystemConfig 系统公用配置 diff --git a/pkg/conf/version.go b/pkg/conf/version.go index a53d3094..ac9e4047 100644 --- a/pkg/conf/version.go +++ b/pkg/conf/version.go @@ -6,6 +6,9 @@ var BackendVersion = "3.0.0-beta1" // RequiredDBVersion 与当前版本匹配的数据库版本 var RequiredDBVersion = "3.0.0-rc1" +// RequiredStaticVersion 与当前版本匹配的静态资源版本 +var RequiredStaticVersion = "3.0.0" + // IsPro 是否为Pro版本 var IsPro = "false"