Exemplo 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")
}
Exemplo 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,
	})
}
Exemplo n.º 3
0
func cleanExpireRevesions() {
	if err := x.Where("updated<?", time.Now().Add(_EXPIRE_DURATION)).
		Iterate(new(Revision), func(idx int, bean interface{}) (err error) {
			rev := bean.(*Revision)
			if err = rev.GetPackage(); err != nil {
				return err
			}

			if _, err = x.Id(rev.Id).Delete(new(Revision)); err != nil {
				return err
			}

			ext := archive.GetExtension(rev.Pkg.ImportPath)
			fpath := path.Join(setting.ArchivePath, rev.Pkg.ImportPath, rev.Revision+ext)

			switch rev.Storage {
			case LOCAL:
				os.Remove(fpath)
				log.Info("Revision deleted: %s", fpath)
				return nil
			case QINIU:
				key, err := rev.KeyName()
				if err != nil {
					return err
				}
				if setting.ProdMode {
					if err = qiniu.DeleteArchive(key); err != nil {
						return err
					}
				}
				log.Info("Revision deleted: %s", key)
				return nil
			default:
				return nil
			}

			return nil
		}); err != nil {
		log.Error(3, "Fail to clean expire revisions: %v", err)
	}
}
Exemplo n.º 4
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")
}