// beego filter handler for serve captcha image func (c *Captcha) Handler(ctx *context.Context) { var chars []byte id := path.Base(ctx.Request.RequestURI) if i := strings.Index(id, "."); i != -1 { id = id[:i] } key := c.key(id) if len(ctx.Input.Query("reload")) > 0 { chars = c.genRandChars() if err := c.store.Put(key, chars, c.Expiration); err != nil { ctx.Output.SetStatus(500) ctx.WriteString("captcha reload error") beego.Error("Reload Create Captcha Error:", err) return } } else { if v, ok := c.store.Get(key).([]byte); ok { chars = v } else { ctx.Output.SetStatus(404) ctx.WriteString("captcha not found") return } } img := NewImage(chars, c.StdWidth, c.StdHeight) if _, err := img.WriteTo(ctx.ResponseWriter); err != nil { beego.Error("Write Captcha Image Error:", err) } }
func (c *ApiController) Get() { url := c.Input().Get("url") resp := utils.GetUrl(url, nil) html := resp.String() beego.Info(fmt.Sprint("Get from url[%s] return html length: [%d]", url, len(html))) var parser utils.ZingParser err, album := parser.ToAlbum(url, html) if err != nil { beego.Error(err.Error()) return } staticDir := beego.AppPath + "/" + beego.StaticDir["/file"] album.Folder = staticDir + "/" + album.Title // if err := os.Mkdir(album.Folder, 0777); err != nil { // beego.Error(err.Error()) // return // } beego.Info("Album folder: " + album.Folder + ".zip") zfile, err := os.Create(album.Folder + ".zip") if err != nil { beego.Error(err.Error()) return } w := zip.NewWriter(zfile) // w := zip.NewWriter(c.Ctx.ResponseWriter) // album.Song = album.Song[0:1] for i, song := range album.Song { beego.Info("Downloading: " + song.Source) song.Filename = song.Title + " - " + song.Performer + "." + song.Type // song.Filename = album.Folder + "/" + song.Filename resp := grequests.Get(song.Source, nil) beego.Info("Download status:", resp.StatusCode) if resp.Error != nil { beego.Error(resp.Error.Error()) } else { beego.Info(fmt.Sprintf("Downloaded %s. Done: %d/%d.", song.Source, i+1, len(album.Song))) wr, err := w.Create(song.Filename) if err == nil { _, err = wr.Write(resp.Bytes()) } if err != nil { beego.Error(err.Error()) } } } // c.Ctx.Output.Header("Content-Description", "File Transfer") // c.Ctx.Output.Header("Content-Type", "application/octet-stream") // c.Ctx.Output.Header("Content-Transfer-Encoding", "binary") // c.Ctx.Output.Header("Expires", "0") // c.Ctx.Output.Header("Cache-Control", "must-revalidate") // c.Ctx.Output.Header("Pragma", "public") // c.Ctx.Output.Header("Content-Disposition", "attachment; filename="+album.Title+".zip") if err := w.Close(); err != nil { beego.Error(err.Error()) return } c.Data["json"] = `{"success":"true"}` c.ServeJson() }
// reset all migration // run all migration's down function func Reset() error { sm := sortMap(migrationMap) i := 0 for j := len(sm) - 1; j >= 0; j-- { v := sm[j] if isRollBack(v.name) { beego.Info("skip the", v.name) time.Sleep(1 * time.Second) continue } beego.Info("start reset:", v.name) v.m.Reset() v.m.Down() err := v.m.Exec(v.name, "down") if err != nil { beego.Error("execute error:", err) time.Sleep(2 * time.Second) return err } i++ beego.Info("end reset:", v.name) } beego.Info("total success reset:", i, " migration") time.Sleep(2 * time.Second) return nil }
// first Reset, then Upgrade func Refresh() error { err := Reset() if err != nil { beego.Error("execute error:", err) time.Sleep(2 * time.Second) return err } err = Upgrade(0) return err }
//rollback the migration by the name func Rollback(name string) error { if v, ok := migrationMap[name]; ok { beego.Info("start rollback") v.Reset() v.Down() err := v.Exec(name, "down") if err != nil { beego.Error("execute error:", err) time.Sleep(2 * time.Second) return err } beego.Info("end rollback") time.Sleep(2 * time.Second) return nil } else { beego.Error("not exist the migrationMap name:" + name) time.Sleep(2 * time.Second) return errors.New("not exist the migrationMap name:" + name) } }
// tempalte func for output html func (c *Captcha) CreateCaptchaHtml() template.HTML { value, err := c.CreateCaptcha() if err != nil { beego.Error("Create Captcha Error:", err) return "" } // create html return template.HTML(fmt.Sprintf(`<input type="hidden" name="%s" value="%s">`+ `<a class="captcha" href="javascript:">`+ `<img onclick="this.src=('%s%s.png?reload='+(new Date()).getTime())" class="captcha-img" src="%s%s.png">`+ `</a>`, c.FieldIdName, value, c.URLPrefix, value, c.URLPrefix, value)) }
// upgrate the migration from lasttime func Upgrade(lasttime int64) error { sm := sortMap(migrationMap) i := 0 for _, v := range sm { if v.created > lasttime { beego.Info("start upgrade", v.name) v.m.Reset() v.m.Up() err := v.m.Exec(v.name, "up") if err != nil { beego.Error("execute error:", err) time.Sleep(2 * time.Second) return err } beego.Info("end upgrade:", v.name) i++ } } beego.Info("total success upgrade:", i, " migration") time.Sleep(2 * time.Second) return nil }