コード例 #1
0
ファイル: header.go プロジェクト: e154/www.novshin.ru
func (h *HeaderController) saveToBase(value string) error {

	// save to base
	if err := models.SetSettings(settings_key, value); err != nil {
		return err
	}

	return nil
}
コード例 #2
0
ファイル: logo.go プロジェクト: e154/www.novshin.ru
func (l *LogoController) saveToBase(value string) error {

	// save to base
	if err := models.SetSettings("logo", value); err != nil {
		return err
	}

	return nil
}
コード例 #3
0
ファイル: favicon.go プロジェクト: e154/www.novshin.ru
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()
}
コード例 #4
0
ファイル: phoneicon.go プロジェクト: e154/www.novshin.ru
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
	}
}