Esempio n. 1
0
func BlockPackagePost(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksList"] = true

	keys, err := models.BlockPackage(ctx.Query("import_path"), ctx.Query("note"))
	if err != nil {
		if err == models.ErrPackageNotExist {
			ctx.RenderWithErr(err.Error(), "blocks/new", nil)
		} else {
			ctx.Handle(500, "BlockPackage", err)
		}
		return
	}

	if setting.ProdMode {
		for _, k := range keys {
			log.Trace("Deleting archive: %s", k)
			if err = qiniu.DeleteArchive(k); err != nil {
				log.Error(4, "Fail to delete archive(%s): %v", k, err)
			}
			log.Info("Archive deleted: %s", k)
		}
	}

	ctx.Flash.Success("New package has been blocked!")
	ctx.Redirect("/admin/blocks")
}
Esempio n. 2
0
func Download(ctx *middleware.Context) {
	importPath := archive.GetRootPath(ctx.Query("pkgname"))
	rev := ctx.Query("revision")
	r, err := models.CheckPkg(importPath, rev)
	if err != nil {
		ctx.JSON(422, map[string]interface{}{
			"error": err.Error(),
		})
		return
	}

	if err = models.IncreasePackageDownloadCount(importPath); err != nil {
		ctx.JSON(500, map[string]interface{}{
			"error": err.Error(),
		})
		return
	} else if err = models.AddDownloader(ctx.RemoteAddr()); err != nil {
		ctx.JSON(500, map[string]interface{}{
			"error": err.Error(),
		})
		return
	}

	ext := archive.GetExtension(importPath)
	serveName := path.Base(importPath) + "-" + base.ShortSha(r.Revision) + ext
	switch r.Storage {
	case models.LOCAL:
		ctx.ServeFile(path.Join(setting.ArchivePath, importPath, r.Revision+ext), serveName)
	case models.QINIU:
		ctx.Redirect("http://" + setting.BucketUrl + "/" + importPath + "-" + r.Revision + ext)
	}
}
Esempio n. 3
0
func UnblockPackage(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksList"] = true

	if err := models.UnblockPackage(ctx.ParamsInt64(":id")); err != nil {
		ctx.Handle(500, "UnblockPackage", err)
		return
	}

	ctx.Flash.Success("Package has been unblocked!")
	ctx.Redirect("/admin/blocks")
}
Esempio n. 4
0
func DeleteBlockRule(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksRules"] = true

	if err := models.DeleteBlockRule(ctx.ParamsInt64(":id")); err != nil {
		ctx.Handle(500, "DeleteBlockRule", err)
		return
	}

	ctx.Flash.Success("Block rule has been deleted!")
	ctx.Redirect("/admin/blocks/rules")
}
Esempio n. 5
0
func Badge(ctx *middleware.Context) {
	importPath := ctx.Params("*")
	pkg, err := models.GetPakcageByPath(importPath)
	if err != nil {
		if err == models.ErrPackageNotExist {
			ctx.Error(404)
		} else {
			ctx.Handle(500, "Badge", err)
		}
		return
	}
	ctx.Redirect(fmt.Sprintf("http://img.shields.io/badge/downloads-%d_total-blue.svg?style=flat", pkg.DownloadCount))
}
Esempio n. 6
0
func NewBlockRulePost(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksRules"] = true

	r := &models.BlockRule{
		Rule: ctx.Query("rule"),
		Note: ctx.Query("note"),
	}
	if err := models.NewBlockRule(r); err != nil {
		ctx.Handle(500, "NewBlockRule", err)
		return
	}

	ctx.Flash.Success("New block rule has been added!")
	ctx.Redirect("/admin/blocks/rules")
}
Esempio n. 7
0
func Download(ctx *middleware.Context) {
	ctx.Data["Title"] = ctx.Tr("download")
	ctx.Data["PageIsDownload"] = true
	importPath := archive.GetRootPath(ctx.Query("pkgname"))

	if ctx.Req.Method == "POST" {
		rev := ctx.Query("revision")
		r, err := models.CheckPkg(importPath, rev)
		if err != nil {
			ctx.Data["pkgname"] = importPath
			ctx.Data["revision"] = rev

			errMsg := err.Error()
			if err == archive.ErrNotMatchAnyService {
				ctx.Data["Err_PkgName"] = true
				errMsg = ctx.Tr("download.err_not_match_service")
			} else if _, ok := err.(*models.BlockError); ok {
				errMsg = ctx.Tr("download.err_package_blocked", err.Error())
			}
			ctx.RenderWithErr(errMsg, "download", nil)
			return
		}

		if err = models.IncreasePackageDownloadCount(importPath); err != nil {
			ctx.Handle(500, "IncreasePackageDownloadCount", err)
			return
		} else if err = models.AddDownloader(ctx.RemoteAddr()); err != nil {
			ctx.Handle(500, "AddDownloader", err)
			return
		}

		ext := archive.GetExtension(importPath)
		serveName := path.Base(importPath) + "-" + base.ShortSha(r.Revision) + ext
		switch r.Storage {
		case models.LOCAL:
			ctx.ServeFile(path.Join(setting.ArchivePath, importPath, r.Revision+ext), serveName)
		case models.QINIU:
			ctx.Redirect("http://" + setting.BucketUrl + "/" + importPath + "-" + r.Revision + ext)
		}
		return
	}

	ctx.Data["pkgname"] = importPath
	ctx.HTML(200, "download")
}
Esempio n. 8
0
func RunRule(ctx *middleware.Context) {
	ctx.Data["PageIsBlocks"] = true
	ctx.Data["PageIsBlocksRules"] = true

	rid := ctx.ParamsInt64(":id")
	count, keys, err := models.RunBlockRule(rid)
	if err != nil {
		ctx.Handle(500, "RunBlockRule", err)
		return
	}

	if setting.ProdMode {
		for _, k := range keys {
			log.Trace("Deleting archive: %s", k)
			if err = qiniu.DeleteArchive(k); err != nil {
				log.Error(4, "Fail to delete archive(%s): %v", k, err)
			}
			log.Info("Archive deleted: %s", k)
		}
	}

	ctx.Flash.Success(fmt.Sprintf("%d packages are blocked by rule ID: %d.", count, rid))
	ctx.Redirect("/admin/blocks/rules")
}
Esempio n. 9
0
func AuthPost(ctx *middleware.Context) {
	ctx.SetCookie("access_token", ctx.Query("token"))
	ctx.Redirect("/admin")
}