示例#1
0
文件: block.go 项目: harryyeh/switch
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")
}
示例#2
0
// Handle handles and logs error by given status.
func (ctx *Context) Handle(status int, title string, err error) {
	if err != nil {
		log.Error(4, "%s: %v", title, err)
		if macaron.Env != macaron.PROD {
			ctx.Data["ErrorMsg"] = err
		}
	}

	switch status {
	case 404:
		ctx.Data["Title"] = "Page Not Found"
	case 500:
		ctx.Data["Title"] = "Internal Server Error"
	}
	ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
}
示例#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)
	}
}
示例#4
0
文件: block.go 项目: harryyeh/switch
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")
}
示例#5
0
文件: models.go 项目: harryyeh/switch
// uploadArchives checks and uploads local archives to QiNiu.
func uploadArchives() {
	revs, err := GetLocalRevisions()
	if err != nil {
		log.Error(4, "Fail to get local revisions: %v", err)
		return
	}

	// Upload.
	for _, rev := range revs {
		pkg, err := GetPakcageById(rev.PkgId)
		if err != nil {
			log.Error(4, "Fail to get package by ID(%d): %v", rev.PkgId, err)
			continue
		}

		ext := archive.GetExtension(pkg.ImportPath)
		key := pkg.ImportPath + "-" + rev.Revision + ext
		localPath := path.Join(pkg.ImportPath, rev.Revision)
		fpath := path.Join(setting.ArchivePath, localPath+ext)

		// Move.
		// rsCli := rs.New(nil)
		// log.Info(key)
		// err = rsCli.Move(nil, setting.BucketName, pkg.ImportPath+"-"+rev.Revision, setting.BucketName, key)
		// if err != nil {
		// 	log.Error(4, rev.Revision)
		// }
		// continue

		if !com.IsFile(fpath) {
			log.Debug("Delete: %v", fpath)
			DeleteRevisionById(rev.Id)
			continue
		}

		// Check archive size.
		f, err := os.Open(fpath)
		if err != nil {
			log.Error(4, "Fail to open file(%s): %v", fpath, err)
			continue
		}
		fi, err := f.Stat()
		if err != nil {
			log.Error(4, "Fail to get file info(%s): %v", fpath, err)
			continue
		}
		// Greater then MAX_UPLOAD_SIZE.
		if fi.Size() > setting.MaxUploadSize<<20 {
			log.Debug("Ignore large archive: %v", fpath)
			continue
		}

		log.Debug("Uploading: %s", localPath)
		if err = qiniu.UploadArchive(key, fpath); err != nil {
			log.Error(4, "Fail to upload file(%s): %v", fpath, err)
			continue
		}
		rev.Storage = QINIU
		if err := UpdateRevision(rev); err != nil {
			log.Error(4, "Fail to upadte revision(%d): %v", rev.Id, err)
			continue
		}
		os.Remove(fpath)
		log.Info("Uploaded: %s", localPath)
	}
}