Example #1
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)
	}
}
Example #2
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")
}