Ejemplo 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")
}
Ejemplo n.º 2
0
func BlockPackage(ctx *middleware.Context) {
	id := ctx.QueryInt64("id")
	pkg, err := models.GetPakcageById(id)
	if err != nil {
		if err == models.ErrPackageNotExist {
			ctx.JSON(404, map[string]string{
				"error": err.Error(),
			})
		} else {
			ctx.JSON(500, map[string]string{
				"error": fmt.Sprintf("fail to get package by ID(%d): %v", id, err),
			})
		}
		return
	}

	revs, err := pkg.GetRevisions()
	if err != nil {
		ctx.JSON(500, map[string]string{
			"error": fmt.Sprintf("fail to get package revisions by ID(%d): %v", id, err),
		})
		return
	}

	// Delete package archives.
	ext := archive.GetExtension(pkg.ImportPath)
	for _, rev := range revs {
		switch rev.Storage {
		case models.QINIU:
			key := pkg.ImportPath + "-" + rev.Revision + ext
			if err = qiniu.DeleteArchive(key); err != nil {
				ctx.JSON(500, map[string]string{
					"error": fmt.Sprintf("fail to delete archive(%s): %v", key, err),
				})
				return
			}
		}
	}
	os.RemoveAll(path.Join(setting.ArchivePath, pkg.ImportPath))

	if err = models.BlockPackage(pkg, revs, ctx.Query("note")); err != nil {
		ctx.JSON(500, map[string]string{
			"error": fmt.Sprintf("fail to block package by ID(%d): %v", id, err),
		})
		return
	}

	ctx.JSON(200, map[string]interface{}{
		"ok": true,
	})
}