diff --git a/commands/command.go b/commands/command.go index f44261a9..3d2f526c 100644 --- a/commands/command.go +++ b/commands/command.go @@ -55,51 +55,68 @@ func RegisterModel() { //初始化数据 func Initialization() { - options := []models.Option { - { OptionName: "ENABLED_CAPTCHA", OptionValue: "false", OptionTitle:"是否启用验证码"}, - { OptionName: "ENABLED_REGISTER",OptionValue:"false",OptionTitle:"是否启用注册"}, - { OptionName: "ENABLE_ANONYMOUS" , OptionValue:"false", OptionTitle:"启用匿名访问"}, - { OptionName: "SITE_NAME", OptionValue:"MinDoc", OptionTitle: "站点名称"}, + o := orm.NewOrm() + + _,err := o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '是否启用注册','ENABLED_REGISTER','false' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLED_REGISTER');`).Exec() + + if err != nil { + panic("ENABLED_REGISTER => " + err.Error()) + os.Exit(1) } + _,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '是否启用验证码','ENABLED_CAPTCHA','false' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLED_CAPTCHA');`).Exec() + if err != nil { + panic("ENABLED_CAPTCHA => " + err.Error()) + os.Exit(1) + } + _,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '启用匿名访问','ENABLE_ANONYMOUS','true' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLE_ANONYMOUS');`).Exec() - if err := models.NewOption().InsertMulti(options...);err != nil { - panic("Option.InsertMulti => " + err.Error()) + if err != nil { + panic("ENABLE_ANONYMOUS => " + err.Error()) + os.Exit(1) + } + _,err = o.Raw(`INSERT INTO md_options (option_title, option_name, option_value) SELECT '站点名称','SITE_NAME','MinDoc' FROM dual WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'SITE_NAME');`).Exec() + + if err != nil { + panic("SITE_NAME => " + err.Error()) os.Exit(1) } - member := models.NewMember() - member.Account = "admin" - member.Avatar = "/static/images/headimgurl.jpg" - member.Password = "123456" - member.Role = 0 - member.Email = "admin@iminho.me" + member,err := models.NewMember().FindByFieldFirst("account","admin") + if err == orm.ErrNoRows { - if err := member.Add();err != nil { - panic("Member.Add => " + err.Error()) - os.Exit(0) - } + member.Account = "admin" + member.Avatar = "/static/images/headimgurl.jpg" + member.Password = "123456" + member.Role = 0 + member.Email = "admin@iminho.me" - book := models.NewBook() + if err := member.Add(); err != nil { + panic("Member.Add => " + err.Error()) + os.Exit(0) + } - book.MemberId = member.MemberId - book.BookName = "MinDoc演示项目" - book.Status = 0 - book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。" - book.CommentCount = 0 - book.PrivatelyOwned = 0 - book.CommentStatus = "closed" - book.Identify = "mindoc" - book.DocCount = 0 - book.CommentCount = 0 - book.Version = time.Now().Unix() - book.Cover = conf.GetDefaultCover() - book.Editor = "markdown" - book.Theme = "default" + book := models.NewBook() - if err := book.Insert(); err != nil { - panic("Book.Insert => " + err.Error()) - os.Exit(0) + book.MemberId = member.MemberId + book.BookName = "MinDoc演示项目" + book.Status = 0 + book.Description = "这是一个MinDoc演示项目,该项目是由系统初始化时自动创建。" + book.CommentCount = 0 + book.PrivatelyOwned = 0 + book.CommentStatus = "closed" + book.Identify = "mindoc" + book.DocCount = 0 + book.CommentCount = 0 + book.Version = time.Now().Unix() + book.Cover = conf.GetDefaultCover() + book.Editor = "markdown" + book.Theme = "default" + + if err := book.Insert(); err != nil { + panic("Book.Insert => " + err.Error()) + os.Exit(0) + } } } @@ -128,7 +145,7 @@ func RegisterLogger() { func RegisterCommand() { if _,err := os.Stat("install.lock"); os.IsNotExist(err){ - err = orm.RunSyncdb("default",true,false) + err = orm.RunSyncdb("default",false,true) if err == nil { Initialization() f, _ := os.Create("install.lock") @@ -139,6 +156,7 @@ func RegisterCommand() { } } + CheckUpdate() } func RegisterFunction() { diff --git a/commands/update.go b/commands/update.go new file mode 100644 index 00000000..28f3c3a7 --- /dev/null +++ b/commands/update.go @@ -0,0 +1,52 @@ +package commands + +import ( + "os" + "net/http" + "github.com/astaxie/beego" + "encoding/json" + "io/ioutil" + "fmt" +) + +func Update() { + if len(os.Args) > 2 && os.Args[1] == "update" { + + os.Exit(0) + } +} + +//检查最新版本. +func CheckUpdate() { + + if len(os.Args) >= 2 && os.Args[1] == "version" { + + resp, err := http.Get("https://api.github.com/repos/lifei6671/godoc/tags") + + if err != nil { + beego.Error("CheckUpdate => ", err) + os.Exit(1) + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + beego.Error("CheckUpdate => ", err) + os.Exit(1) + } + + var result []*struct { + Name string `json:"name"` + } + + err = json.Unmarshal(body, &result) + + if err != nil { + beego.Error("CheckUpdate => ", err) + os.Exit(1) + } + + fmt.Println("MinDoc last version => ",result[0].Name) + os.Exit(0) + } +} \ No newline at end of file diff --git a/main.go b/main.go index cc0ae305..b56f0546 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "os" "github.com/lifei6671/godoc/controllers" "github.com/lifei6671/godoc/conf" + "github.com/lifei6671/godoc/utils" ) var ( @@ -36,6 +37,7 @@ func main() { beego.ErrorController(&controllers.ErrorController{}) + utils.ConverterPdf("a.pdf",nil) beego.Run() } diff --git a/static/pdf-fonts/msyh.ttc b/static/pdf-fonts/msyh.ttc new file mode 100644 index 00000000..796cc2d9 Binary files /dev/null and b/static/pdf-fonts/msyh.ttc differ diff --git a/static/pdf-fonts/msyh.ttf b/static/pdf-fonts/msyh.ttf new file mode 100644 index 00000000..aa23ae1f Binary files /dev/null and b/static/pdf-fonts/msyh.ttf differ diff --git a/static/pdf-fonts/msyhbd.ttc b/static/pdf-fonts/msyhbd.ttc new file mode 100644 index 00000000..5e2f4dd9 Binary files /dev/null and b/static/pdf-fonts/msyhbd.ttc differ diff --git a/static/pdf-fonts/msyhbold.ttf b/static/pdf-fonts/msyhbold.ttf new file mode 100644 index 00000000..d66a9709 Binary files /dev/null and b/static/pdf-fonts/msyhbold.ttf differ diff --git a/static/pdf-fonts/msyhl.ttc b/static/pdf-fonts/msyhl.ttc new file mode 100644 index 00000000..062adb96 Binary files /dev/null and b/static/pdf-fonts/msyhl.ttc differ diff --git a/utils/pdf.go b/utils/pdf.go new file mode 100644 index 00000000..67cda6ac --- /dev/null +++ b/utils/pdf.go @@ -0,0 +1,28 @@ +package utils + +import ( + "github.com/signintech/gopdf" + "github.com/astaxie/beego" +) +func ConverterPdf(output string,htmlList map[string]string) error { + pdf := gopdf.GoPdf{} + pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) + + pdf.AddPage() + + err := pdf.AddTTFFont("HDZB_5", "./static/pdf-fonts/msyh.ttf") + if err != nil { + beego.Error("ConverterPdf => ",err) + return err + } + err = pdf.SetFont("HDZB_5", "", 14) + if err != nil { + beego.Error("ConverterPdf => " , err) + return err + } + + pdf.Cell(nil, "您好") + pdf.WritePdf(output) + + return nil +}