-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 未命名版本内容支持从历史版本/从其他服务导入 --story=116463975 #3190
feat: 未命名版本内容支持从历史版本/从其他服务导入 --story=116463975 #3190
Conversation
f6c683e
to
514871b
Compare
buffer := make([]byte, 512) | ||
// 读取前512字节以检测文件类型 | ||
n, errR := r.Body.Read(buffer[:512]) | ||
if errR != nil && errR != io.EOF { | ||
_ = render.Render(w, r, rest.BadRequest(errR)) | ||
return | ||
} | ||
|
||
// 删除临时文件夹 | ||
defer func() { _ = os.RemoveAll(unpackTempDir) }() | ||
// 组合上一次读取 | ||
combinedReader := io.MultiReader(bytes.NewReader(buffer[:n]), r.Body) | ||
|
||
newFiles, err := scanFiles(unpackTempDir) | ||
tempDir, err := archive.GenerateTempDir("configItem-unzipped-") | ||
if err != nil { | ||
_ = render.Render(w, r, rest.BadRequest(err)) | ||
return | ||
} | ||
defer func() { _ = os.RemoveAll(tempDir) }() | ||
identifyFileType := archive.IdentifyFileType(buffer[:n]) | ||
|
||
// 获取某个空间下的所有配置文件 | ||
items, err := c.cfgClient.ListTemplates(kt.RpcCtx(), &pbcs.ListTemplatesReq{ | ||
BizId: kt.BizID, | ||
TemplateSpaceId: uint32(tmplSpaceID), | ||
All: true, | ||
}) | ||
if err != nil { | ||
_ = render.Render(w, r, rest.BadRequest(err)) | ||
switch identifyFileType { | ||
case string(archive.ZIP): | ||
zipArchive := archive.NewZipArchive(tempDir) | ||
if err = zipArchive.Unpack(combinedReader); err != nil { | ||
_ = render.Render(w, r, rest.BadRequest(err)) | ||
return | ||
} | ||
case string(archive.GZIP): | ||
gzipArchive := archive.NewTgzArchive(tempDir) | ||
if err = gzipArchive.Unpack(combinedReader); err != nil { | ||
_ = render.Render(w, r, rest.BadRequest(err)) | ||
return | ||
} | ||
case string(archive.TAR): | ||
tarArchive := archive.NewTgzArchive(tempDir) | ||
if err = tarArchive.UnTar(combinedReader); err != nil { | ||
_ = render.Render(w, r, rest.BadRequest(err)) | ||
return | ||
} | ||
case string(archive.Unknown): | ||
default: | ||
_ = render.Render(w, r, rest.BadRequest(errors.New("file type detection failed"))) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这一部分可以合并为一个解压函数,这个函数太大了,需要拆一下
_ = render.Render(w, r, rest.BadRequest(err)) | ||
return | ||
} | ||
case string(archive.Unknown): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看起来下面很多地方用到了 archive.Unknow 来判断是文件类型还是压缩包类型,而且下面还用了 tempDir 是否为 “” 来判断是文件类型还是压缩包类型,这个可读性有点差,不好维护
这个接口应该是一个文件调用一次吧,这样的话可以把文件和压缩包分成两个子函数处理,不要全都放在一个函数逻辑里,文件直接透传到 bkrepo 上传,压缩包再决定要不要解压还是直接作为文件上传
或者直接判断 unpack 为 false,就全部都透传到 bkrepo,为 true再判断透传还是解压
@@ -134,4 +134,6 @@ const ( | |||
MaxUploadContentLength = 100 * 1024 * 1024 | |||
// MaxConcurrentUpload 限制上传文件并发数 | |||
MaxConcurrentUpload = 10 | |||
// UploadBatchSize 上传时分批检测文件路冲突 | |||
UploadBatchSize = 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个分批size太大了吧,文件数量应该都不会允许到 1000,这个调整为 50 吧
|
||
// 写入缓冲区,计算哈希、检测类型、上传都用到了 | ||
// 防止数据已被读完 | ||
teeReader := io.TeeReader(reader, buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会不会导致,文件一定会被整个读入 buffer,实际只需要在检测类型的时候读入前 512 字节的内容进内存就够了
@@ -708,3 +709,77 @@ func checkExistingPathConflict(existing []string) (uint32, map[string]bool) { | |||
|
|||
return conflictNums, conflictPaths | |||
} | |||
|
|||
// CompareConfigItemConflicts compare config item version conflicts | |||
func (s *Service) CompareConfigItemConflicts(ctx context.Context, req *pbcs.CompareConfigItemConflictsReq) ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个接口是干啥的呀,为啥要和别的 app 的配置文件比较
if bytes.Equal(magicGZ, magic[0:2]) { | ||
return "gzip", nil | ||
// IdentifyFileType 检测文件类型:zip、zip、tar | ||
func IdentifyFileType(buf []byte) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面定义了 ArchiveType 类型,这里就直接返回 ArchiveType 吧
return "", err | ||
} | ||
// GenerateTempDir 生成临时目录 | ||
func GenerateTempDir(destPath string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个就一行代码,就不用封装函数了吧
return "", fmt.Errorf("this package is not supported") | ||
func isZip(buf []byte) bool { | ||
// ZIP files start with 50 4B 03 04 or 50 4B 05 06 or 50 4B 07 08 | ||
zipMagic := []byte{0x50, 0x4B} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
514871b
to
6b8e9c2
Compare
6b8e9c2
to
cd0c4f3
Compare
No description provided.