[REV] Optimize process of ftp upload

This commit is contained in:
pujielan 2018-02-08 10:36:22 +08:00
parent b488aea375
commit 644c53e1ec
4 changed files with 50 additions and 28 deletions

View File

@ -6865,10 +6865,6 @@
"type": "string",
"x-go-name": "FTPUserName"
},
"inner_registry": {
"type": "string",
"x-go-name": "InnerRegistry"
},
"outer_registry": {
"type": "string",
"x-go-name": "OuterRegistry"

View File

@ -932,7 +932,6 @@ type ShareConfItems struct {
FTPUserName string `json:"ftp_username" valiate:"ftp_username"`
FTPPassWord string `json:"ftp_password" validate:"ftp_password"`
FTPNamespace string `json:"ftp_namespace" validate:"ftp_namespace"`
InnerRegistry string `json:"inner_registry" validate:"inner_registry"`
OuterRegistry string `json:"outer_registry" validate:"outer_registry"`
}

View File

@ -122,43 +122,43 @@ func createMD5(packageName string) (string, error) {
return packageName+".md5", nil
}
//ShareToYB ShareToYB
func (i *SlugShareItem)ShareToYB()error {
return nil
}
//ShareToYS ShareToYS
func (i *SlugShareItem)ShareToYS(file string)error {
i.Logger.Info("开始分享云市", map[string]string{"step":"slug-share"})
if err:= i.UploadFtp(i.FTPConf.FTPNamespace, file); err != nil {
return err
}
md5, err := createMD5(file)
if err != nil {
i.Logger.Error("生成md5失败", map[string]string{"step":"slug-share", "status":"success"})
}
if err := i.UploadFtp(i.FTPConf.FTPNamespace, md5); err != nil {
if err := i.UploadFtp(i.FTPConf.FTPNamespace, file, md5); err != nil {
return err
}
i.Logger.Info("分享云市完成", map[string]string{"step":"slug-share", "status":"success"})
return nil
}
//ShareInfoData ShareInfoData
func (i *SlugShareItem)ShareInfoData() error {
//TODO:
return nil
}
//UploadFtp UploadFt
func (i *SlugShareItem)UploadFtp(path, file string) error {
func (i *SlugShareItem)UploadFtp(path, file, md5 string) error {
i.Logger.Info(fmt.Sprintf("开始上传代码包: %s", file), map[string]string{"step":"slug-share"})
ftp := sources.NewFTPManager(i.FTPConf.Username, i.FTPConf.Password, i.FTPConf.Host)
err := ftp.UploadFile(path, file, i.Logger)
sc, err := ftp.LoginFTP(i.Logger)
if err != nil {
return err
}
defer ftp.LogoutFTP(sc, i.Logger)
bl, err := ftp.CheckMd5FileName(sc, path, md5)
if err != nil {
return err
}
if bl {
i.Logger.Info(fmt.Sprintf("文件(%s)已上传", file), map[string]string{"step":"slug-share", "status":"success"})
return nil
}
if err := ftp.UploadFile(sc, path, file, i.Logger); err != nil {
i.Logger.Error(fmt.Sprintf("上传代码包%s失败", file), map[string]string{"step":"slug-share", "status":"failure"})
}
if err := ftp.UploadFile(sc, path, md5, i.Logger); err != nil {
i.Logger.Error("上传md5文件失败", map[string]string{"step":"slug-share", "status":"failure"})
}
i.Logger.Info("代码包上传完成", map[string]string{"step":"slug-share", "status":"success"})
return nil
}

View File

@ -52,19 +52,32 @@ func NewFTPManager(username, password, host string, port...int) *FTPBase {
return fb
}
//UploadFile UploadFile
func (f *FTPBase)UploadFile(path, file string, logger event.Logger) error {
//LoginFTP LoginFTP
func (f *FTPBase)LoginFTP(logger event.Logger) (*ftp.ServerConn, error){
sc, err:= ftp.DialTimeout(fmt.Sprintf("%s:%d", f.Host, f.Port), 5*time.Second)
if err != nil {
logger.Error("ftp服务器连接错误", map[string]string{"step":"slug-share", "status":"failure"})
return err
return nil, err
}
if err := sc.Login(f.UserName, f.PassWord); err != nil {
logger.Error("ftp服务器登录错误", map[string]string{"step":"slug-share", "status":"failure"})
return nil, err
}
return sc, nil
}
//LogoutFTP LogoutFTP
func (f *FTPBase)LogoutFTP(sc *ftp.ServerConn, logger event.Logger) error {
if err := sc.Logout(); err != nil {
logger.Error("ftp服务器登出错误", map[string]string{"step":"slug-share", "status":"failure"})
return err
}
defer sc.Logout()
if err = sc.ChangeDir(path); err != nil {
return nil
}
//UploadFile UploadFile
func (f *FTPBase)UploadFile(sc *ftp.ServerConn, path, file string, logger event.Logger) error {
if err := sc.ChangeDir(path); err != nil {
return err
}
fi, err := os.Open(file)
@ -76,6 +89,7 @@ func (f *FTPBase)UploadFile(path, file string, logger event.Logger) error {
if err != nil {
return err
}
//TODO: 大于缓冲区大小的文件,存在误差
var bufSize int64 = 1024 * 1024 * 50
if stat.Size() < bufSize {
bufSize = stat.Size()
@ -157,3 +171,16 @@ func (f *FTPBase) TransFile(path, file string) error {
return nil
}
//CheckMd5FileName ListFile
func (f *FTPBase) CheckMd5FileName(sc *ftp.ServerConn, path , md5file string) (bool, error) {
entrys, err := sc.NameList(path)
if err != nil {
return false, err
}
for _, entry := range entrys{
if entry == md5file {
return true, nil
}
}
return false, nil
}