Exemplo n.º 1
0
func do_markdown_get(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.MarkdownRoot, r.URI())) {
		return false
	}

	if pg := ctx.get_page(r.URI()); pg != nil {
		data := map[string]interface{}{
			"url":  r.URL,
			"page": pg,
		}

		tname := r.Param("template")
		if tname == "" {
			tname = pg.Template()
		}
		if tname == "" {
			ctx.app.Configs.SetSection("h2object")
			tname = ctx.app.Configs.StringDefault("markdown.template", "")
		}

		if tname != "" {
			if t, err := ctx.app.templates.Template(tname); err == nil {
				ctrl.Template(t, data)
				return true
			}
		}
		ctrl.Html(pg.Markdown())
		return true
	}
	return false
}
Exemplo n.º 2
0
func do_static_delete(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.StaticRoot, r.URI())) {
		return false
	}

	fn := path.Join(ctx.app.Options.StaticRoot, r.URI())

	ctx.app.Configs.SetSection("third")
	qiniu_enable := ctx.app.Configs.BoolDefault("qiniu.enable", false)
	if qiniu_enable {

		qiniu_appid := ctx.app.Configs.StringDefault("qiniu.appid", "")
		qiniu_secret := ctx.app.Configs.StringDefault("qiniu.secret", "")
		qiniu_bucket := ctx.app.Configs.StringDefault("qiniu.bucket", "")

		defer func(key string, file string) {
			helper := third.NewQiniuHelper(qiniu_appid, qiniu_secret, ctx.app.cache)
			helper.Del(qiniu_bucket, key)
		}(third.QiniuKey(r.URI()), fn)
	}
	os.Remove(fn)
	ctrl.Json(map[string]interface{}{
		r.URI(): "deleted",
	})
	return true
}
Exemplo n.º 3
0
func do_template_delete(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.TemplateRoot, r.URI())) {
		return false
	}

	os.Remove(path.Join(ctx.app.Options.TemplateRoot, r.URI()))
	defer ctx.app.templates.Refresh()

	ctrl.Json(map[string]interface{}{
		r.URI(): "deleted",
	})
	return true
}
Exemplo n.º 4
0
func do_markdown_delete(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.MarkdownRoot, r.URI())) {
		return false
	}

	os.Remove(path.Join(ctx.app.Options.MarkdownRoot, r.URI()))
	ctx.del_page(r.URI())

	ctrl.Json(map[string]interface{}{
		r.URI(): "deleted",
	})
	return true
}
Exemplo n.º 5
0
func do_template_get(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request
	if !util.Exist(path.Join(ctx.app.Options.TemplateRoot, r.URI())) {
		return false
	}

	data := map[string]interface{}{
		"url": r.URL,
	}

	if t, err := ctx.app.templates.Template(r.URI()); err == nil {
		ctx.Info("template get (%s) ok", r.URI())
		ctrl.Template(t, data)
	} else {
		ctrl.JsonError(http.StatusInternalServerError, err)
	}
	return true
}
Exemplo n.º 6
0
func do_static_get(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.StaticRoot, r.URI())) {
		return false
	}

	ctx.app.Configs.SetSection("third")
	qiniu_enable := ctx.app.Configs.BoolDefault("qiniu.enable", false)
	qiniu_domain := ctx.app.Configs.StringDefault("qiniu.domain", "")

	if qiniu_enable {
		if val, err := ctx.app.systems.Get(path.Join("/qiniu", r.URI()), true); err == nil {
			var key string
			if err := util.Convert(val, &key); err == nil {
				uri := fmt.Sprintf("http://%s", path.Join(qiniu_domain, key))
				ctrl.Redirect(uri, r.URL.RawQuery)
				return true
			}
		}
	}
	ctrl.File(path.Join(ctx.app.Options.StaticRoot, r.URI()))
	return true
}
Exemplo n.º 7
0
func (ctx *context) qrcode_generate(u *url.URL, size int) (*QRCode, error) {
	u2 := ctx.qrcode_url(u)

	var qc QRCode
	qc.Value = u2.String()
	qc.File = path.Join(ctx.app.Options.StaticRoot, "qrcode", util.QrcodeKey(u2.Path, size))

	query := u2.Query()
	query.Set("qrcode", strconv.Itoa(size))

	u3 := u2
	u3.RawQuery = query.Encode()
	qc.Link = u3.String()

	ctx.Info("qrcode value:(%s) link:(%s)", qc.Value, qc.Link)
	if util.Exist(qc.File) {
		return &qc, nil
	}
	if err := qrcode.WriteFile(qc.Value, qrcode.High, size, qc.File); err != nil {
		return nil, err
	}

	return &qc, nil
}
Exemplo n.º 8
0
func do_params(ctx *context, ctrl *ext.Controller) bool {
	qr := ctrl.Request.Param("qrcode")
	if qr != "" {
		if size, err := strconv.ParseInt(qr, 10, 64); err == nil {
			return do_qrcode(ctx, ctrl, int(size))
		}
	}

	resize := ctrl.Request.Param("resize")
	if resize != "" {
		fn := path.Join(ctx.app.Options.StaticRoot, "resize", util.ResizeKey(ctrl.Request.URI(), resize))
		if util.Exist(fn) {
			ctrl.File(fn)
			return true
		}
		if rect, err := util.ParseRect(resize, "x"); err == nil {
			f1 := path.Join(ctx.app.Options.StaticRoot, ctrl.Request.URI())
			ctx.Info("f1 :%s", f1)
			if src, err := imaging.Open(f1); err == nil {
				dst := imaging.Resize(src, rect.Width, rect.Height, imaging.Lanczos)
				if err := imaging.Save(dst, fn); err == nil {
					ctrl.File(fn)
					return true
				}
			}
		}
	}

	fit := ctrl.Request.Param("fit")
	if fit != "" {
		fn := path.Join(ctx.app.Options.StaticRoot, "fit", util.ResizeKey(ctrl.Request.URI(), resize))
		if util.Exist(fn) {
			ctrl.File(fn)
			return true
		}
		if rect, err := util.ParseRect(resize, "x"); err == nil {
			f1 := path.Join(ctx.app.Options.StaticRoot, ctrl.Request.URI())
			if src, err := imaging.Open(f1); err == nil {
				dst := imaging.Fit(src, rect.Width, rect.Height, imaging.Lanczos)
				if err := imaging.Save(dst, fn); err == nil {
					ctrl.File(fn)
					return true
				}
			}
		}
	}

	thumbnail := ctrl.Request.Param("thumbnail")
	if thumbnail != "" {
		fn := path.Join(ctx.app.Options.StaticRoot, "thumbnail", util.ResizeKey(ctrl.Request.URI(), resize))
		if util.Exist(fn) {
			ctrl.File(fn)
			return true
		}
		if rect, err := util.ParseRect(thumbnail, "x"); err == nil {
			f1 := path.Join(ctx.app.Options.StaticRoot, ctrl.Request.URI())
			if src, err := imaging.Open(f1); err == nil {
				dst := imaging.Thumbnail(src, rect.Width, rect.Height, imaging.Lanczos)
				if err := imaging.Save(dst, fn); err == nil {
					ctrl.File(fn)
					return true
				}
			}
		}
	}

	return false
}