func (h *HeaderController) loadFromBase() (images []*HeaderImage, err error) { value, err := models.GetSettings(settings_key) if err != nil || value == nil { return } if err = json.Unmarshal([]byte(value.(string)), &images); err != nil { return } return images, nil }
func (l *LogoController) loadFromBase() (result []string, err error) { value, err := models.GetSettings("logo") if err != nil || value == nil { return } if err = json.Unmarshal([]byte(value.(string)), &result); err != nil { return } return result, nil }
func (p *PhoneIconController) Get() { original, err := models.GetSettings("phoneicon") if err != nil || original == nil { if original == nil { err = errors.New("Image not found") } p.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} p.ServeJson() return } p.Data["json"] = original p.ServeJson() }
func (f *FaviconController) Get() { original, err := models.GetSettings("favicon") if err != nil { f.Ctx.ResponseWriter.WriteHeader(500) f.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} f.ServeJson() return } if original == nil { original = "" } f.Data["json"] = original f.ServeJson() }
func (f *FaviconController) Update() { // save image file and rename file, h, err := f.GetFile("file") if err != nil { f.Ctx.ResponseWriter.WriteHeader(500) f.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} f.ServeJson() return } t := time.Now().UnixNano() filename := h.Filename[strings.LastIndex(h.Filename, `:`)+1:] ext := path.Ext(filename) icon_dir := beego.AppConfig.String("icon_dir") dir := data_dir + icon_dir + "/" original := fmt.Sprintf("%d_original%s", t, ext) os.MkdirAll(dir, os.ModePerm) file.Close() err = f.SaveToFile("file", dir+original) if err != nil { f.Ctx.ResponseWriter.WriteHeader(500) f.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} f.ServeJson() return } // image resize type resizeMap struct { width uint height uint name string filter resize.InterpolationFunction } var m = map[string]resizeMap{ "favicon": resizeMap{16, 16, "favicon", resize.Bilinear}, } var sm = make(map[string]string) sm["original"] = original for key, value := range m { var newname = fmt.Sprintf("%s.png", value.name) err = lib.ImageResize(dir+original, dir+newname, value.width, value.height, value.filter, "png") if err != nil { f.Ctx.ResponseWriter.WriteHeader(501) f.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} f.ServeJson() return } sm[key] = newname } // delete old files if oldLogo, err := models.GetSettings("favicon"); err == nil && oldLogo != nil { fileName := oldLogo.(string) os.Remove(dir + fileName) } // save to base if err := models.SetSettings("favicon", original); err != nil { f.Ctx.ResponseWriter.WriteHeader(500) f.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} f.ServeJson() return } f.Data["json"] = original f.ServeJson() }
func (p *PhoneIconController) Update() { // save image file and rename f, h, err := p.GetFile("file") if err != nil { p.Ctx.ResponseWriter.WriteHeader(500) p.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} p.ServeJson() return } t := time.Now().UnixNano() filename := h.Filename[strings.LastIndex(h.Filename, `:`)+1:] ext := path.Ext(filename) icon_dir := beego.AppConfig.String("icon_dir") dir := data_dir + icon_dir + "/" original := fmt.Sprintf("%d_original%s", t, ext) // original_w := fmt.Sprintf("%d_original-w%s", t, ext) os.MkdirAll(dir, os.ModePerm) f.Close() err = p.SaveToFile("file", dir+original) if err != nil { p.Ctx.ResponseWriter.WriteHeader(500) p.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} p.ServeJson() return } // image resize // weight, err := beego.AppConfig.Int("logo_weight") // height, err := beego.AppConfig.Int("logo_height") // if err != nil { // weight = 150 // height = 150 // } type resizeMap struct { width uint height uint name string filter resize.InterpolationFunction } var m = map[string]resizeMap{ "57x57": resizeMap{57, 57, "apple-touch-icon-57x57-precomposed", resize.Bilinear}, "57x57x2": resizeMap{57, 57, "apple-touch-icon-precomposed", resize.Bilinear}, "57x57x3": resizeMap{57, 57, "apple-touch-icon", resize.Bilinear}, "72x72": resizeMap{72, 72, "apple-touch-icon-72x72-precomposed", resize.Bilinear}, "114x114": resizeMap{114, 114, "apple-touch-icon-114x114-precomposed", resize.Bilinear}, "144x144": resizeMap{144, 144, "apple-touch-icon-144x144-precomposed", resize.Bilinear}, } var sm = make(map[string]string) sm["original"] = original for key, value := range m { var newname = fmt.Sprintf("%s.png", value.name) err = lib.ImageResize(dir+original, dir+newname, value.width, value.height, value.filter, "png") if err != nil { p.Ctx.ResponseWriter.WriteHeader(501) p.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} p.ServeJson() return } sm[key] = newname } // inrert image // if err := lib.ImageInvert(dir + original, dir + original_w); err != nil { // p.Ctx.ResponseWriter.WriteHeader(500) // p.Data["json"] = &map[string]interface {}{"status":"warning", "message":err.Error()} // p.ServeJson() // } // err = lib.ImageResize(dir + original_w, dir + "logo-w.png", 150, 150, resize.Bilinear, "png") // if err != nil { // p.Ctx.ResponseWriter.WriteHeader(500) // p.Data["json"] = &map[string]interface {}{"status":"warning", "message":err.Error()} // p.ServeJson() // } // delete old files if oldLogo, err := models.GetSettings("phoneicon"); err == nil && oldLogo != nil { fileName := oldLogo.(string) // ext := path.Ext(fileName) // base := fileName[0:len(fileName) - len(ext)] os.Remove(dir + fileName) // os.Remove(dir + fmt.Sprintf("%s-w%s", base, ext)) } // save to base if err := models.SetSettings("phoneicon", original); err != nil { p.Ctx.ResponseWriter.WriteHeader(500) p.Data["json"] = &map[string]interface{}{"status": "error", "message": err.Error()} p.ServeJson() return } }