Beispiel #1
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
}
Beispiel #2
0
func do_static_put(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	dir, file := path.Split(r.URI())

	// create static path if not exist
	realDir := path.Join(ctx.app.Options.StaticRoot, dir)
	if err := os.MkdirAll(realDir, os.ModePerm); err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	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)
			key, err := helper.PutFile(qiniu_bucket, key, file)
			if err != nil {
				ctx.Warn("request static qiniu put failed: (%s)", err.Error())
				return
			}

			if err := ctx.app.systems.Put(path.Join("/qiniu", r.URI()), key); err != nil {
				ctx.Warn("request static qiniu save failed: (%s)", err.Error())
				return
			}
			ctx.Info("request static qiniu put succeed with key: (%s)", key)

		}(third.QiniuKey(r.URI()), fn)
	}

	tmp := fn + ".tmp"
	fd, err := os.Create(tmp)
	if err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}
	defer fd.Close()

	if _, err := io.Copy(fd, r.Body); err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}

	if err := os.Rename(tmp, fn); err != nil {
		ctrl.JsonError(http.StatusInternalServerError, err)
		return true
	}

	ctrl.Json(map[string]interface{}{
		"file": file,
	})
	return true
}