1 Star 0 Fork 0

linngc / central-mirror

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
maven_get.go 3.71 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2024-05-20 15:30 . add:添加sdk发布历史接口
// Package maven
// @Link https://gitee.com/linngc/central-mirror
// @Copyright Copyright (c) 2024 central-mirror CLI
// @Author linngc
// @License
package maven
import (
"fmt"
cacheModel "gitee.com/linngc/central-mirror/contrib/container/core/memorycache/implement/model"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"io/ioutil"
"path"
"strconv"
"strings"
)
// Get maven pom.xml get/head请求时候入口
// 最终请求示例: http://127.0.0.1:10010/proxy/maven/public/org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
// libName 示例 public
// @param jarPath 示例 org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
func (c *MavenProxy) Get() (u *cacheModel.ObjectsInfo, err error) {
r := g.RequestFromCtx(c.ctx)
g.Log().Debugf(c.ctx, "接口请求地址-->%v", r.Request.URL.Path)
libName := r.Get("libName").String() //仓库ID
moduleName := r.Get("moduleName").String() //jar包路径名称
repository, err := c.getMavenRepository(c.cfg, libName)
if err != nil {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, err.Error())
}
if repository.Mode&4 != 4 {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, "本地代理仓库不允许读取操作")
}
ext := path.Ext(moduleName)
if ext == "" && !strings.HasSuffix(moduleName, "/") {
r.Response.RedirectTo(r.RequestURI)
return u, nil
}
localPath := path.Join(repository.Id, moduleName)
//请求授权判断
if repository.Auth {
if err = c.generateHash(localPath); err != nil {
return u, gerror.NewCode(gcode.CodeInternalError, fmt.Sprintf("hash值校验不正确,请检查: %v", err))
}
}
u, err = c.Com.GetOrAdd(fmt.Sprintf("%s%s", c.mirror, moduleName), localPath)
g.Log().Debugf(c.ctx, "应答请求文件uri:'%s'", localPath)
rawPath := fmt.Sprintf("/%s%s", repository.Id, moduleName)
g.Log().Debugf(c.ctx, "应答请求文件uri:'%s'", localPath)
r.Request.URL.RawPath = rawPath
r.Request.URL.Path = rawPath
return
}
// DeployMapping maven pom.xml put请求时候入口
// 最终请求示例: http://127.0.0.1:10010/proxy/maven/public/org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
// @param libName 示例 public
// @param moduleName 示例 org/springframework/boot/spring-boot/2.5.6/spring-boot-2.5.6.jar
func (c *MavenProxy) DeployMapping() (u *cacheModel.ObjectsInfo, err error) {
r := g.RequestFromCtx(c.ctx)
g.Log().Debugf(c.ctx, "接口请求地址-->%v", r.Request.URL.Path)
libName := r.Get("libName").String() //仓库ID
moduleName := r.Get("moduleName").String() //jar包路径名称
repository, err := c.getMavenRepository(c.cfg, libName)
localPath := path.Join(repository.Id, moduleName)
//请求授权判断
if repository.Auth {
if err = c.generateHash(localPath); err != nil {
return u, gerror.NewCode(gcode.CodeInternalError, fmt.Sprintf("hash值校验不正确,请检查: %v", err))
}
}
length, err1 := strconv.Atoi(r.Header.Get("Content-Length"))
data, err2 := ioutil.ReadAll(r.Request.Body)
if err1 != nil || err2 != nil || length <= 0 || length != len(data) {
return u, gerror.NewCode(gcode.CodeInvalidRequest, fmt.Sprintf("读取数据出错-->%v\n%v", err1, err2))
}
if err != nil {
return u, gerror.NewCode(gcode.CodeMissingConfiguration, err.Error())
}
if repository.Mode&2 != 2 {
return u, gerror.NewCode(gcode.CodeNotAuthorized, "本地代理仓库不支持写入操作")
}
if err := c.dial.Service.PutByObject(c.ctx, localPath, data); err == nil {
// tip 使用marshal.ObjectsInfo 数据组装
return c.dial.Service.GetObjectsInfo(localPath, data), nil
}
return u, gerror.NewCode(gcode.CodeOperationFailed, fmt.Sprintf("保存写入文件出错: %v\n", err))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linngc/central-mirror.git
git@gitee.com:linngc/central-mirror.git
linngc
central-mirror
central-mirror
a6b6bab2d6e5

搜索帮助

344bd9b3 5694891 D2dac590 5694891