Ejemplo n.º 1
0
func GetRevision(ctx *middleware.Context) {
	importPath := archive.GetRootPath(ctx.Query("pkgname"))
	rev := ctx.Query("revision")
	n := archive.NewNode(importPath, rev)
	if err := n.GetRevision(); err != nil {
		ctx.JSON(422, map[string]interface{}{
			"error": err.Error(),
		})
		return
	}
	ctx.JSON(200, map[string]interface{}{
		"sha": n.Revision,
	})
}
Ejemplo 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)
	}
}
Ejemplo n.º 3
0
func ListLargeRevisions(ctx *middleware.Context) {
	revs, err := models.GetLocalRevisions()
	if err != nil {
		ctx.JSON(500, map[string]string{
			"error": fmt.Sprintf("fail to get local revisions: %v", err),
		})
		return
	}

	largeRevs := make([]*ApiRevesion, 0, len(revs)/2)
	for _, rev := range revs {
		pkg, err := models.GetPakcageById(rev.PkgId)
		if err != nil {
			ctx.JSON(500, map[string]string{
				"error": fmt.Sprintf("fail to get package by ID(%d): %v", rev.PkgId, err),
			})
			return
		}

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

		if !com.IsFile(fpath) {
			continue
		}

		// Check archive size.
		f, err := os.Open(fpath)
		if err != nil {
			ctx.JSON(500, map[string]string{
				"error": fmt.Sprintf("fail to open file(%s): %v", fpath, err),
			})
			return
		}
		fi, err := f.Stat()
		if err != nil {
			ctx.JSON(500, map[string]string{
				"error": fmt.Sprintf("fail to get file info(%s): %v", fpath, err),
			})
			return
		}
		// Greater then MAX_UPLOAD_SIZE.
		if fi.Size() > setting.MaxUploadSize<<20 {
			largeRevs = append(largeRevs, &ApiRevesion{
				Id: rev.Id,
				Package: &ApiPackage{
					Id:         pkg.Id,
					ImportPath: pkg.ImportPath,
					Created:    pkg.Created,
				},
				Revision: rev.Revision,
				Size:     fi.Size(),
				Updated:  rev.Updated,
			})
			continue
		}
	}

	ctx.JSON(200, map[string]interface{}{
		"revisions": &largeRevs,
	})
}
Ejemplo n.º 4
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,
	})
}