MCPcopy Index your code
hub / github.com/TruthHun/BookStack / unzipToData

Method unzipToData

controllers/BookController.go:1047–1149  ·  view source on GitHub ↗

将zip压缩文件解压并录入数据库 @param book_id 书籍id(其实有想不标识了可以不要这个的,但是这里的书籍标识只做目录) @param identify 书籍标识 @param zipfile 压缩文件 @param originFilename 上传文件的原始文件名

(bookId int, identify, zipFile, originFilename string)

Source from the content-addressed store, hash-verified

1045// @param zipfile 压缩文件
1046// @param originFilename 上传文件的原始文件名
1047func (this *BookController) unzipToData(bookId int, identify, zipFile, originFilename string) {
1048
1049 //说明:
1050 //OSS中的图片存储规则为"projects/$identify/书籍中图片原路径"
1051 //本地存储规则为"uploads/projects/$identify/书籍中图片原路径"
1052
1053 projectRoot := "" //书籍根目录
1054
1055 //解压目录
1056 unzipPath := "store/" + identify
1057
1058 //如果存在相同目录,则率先移除
1059 if err := os.RemoveAll(unzipPath); err != nil {
1060 beego.Error(err.Error())
1061 }
1062 os.MkdirAll(unzipPath, os.ModePerm)
1063
1064 imgMap := map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".bmp": true, ".svg": true, ".webp": true}
1065
1066 defer func() {
1067 os.Remove(zipFile) //最后删除上传的临时文件
1068 os.RemoveAll(unzipPath) //删除解压后的文件夹
1069 }()
1070
1071 //注意:这里的prefix必须是判断是否是GitHub之前的prefix
1072 if err := ziptil.Unzip(zipFile, unzipPath); err != nil {
1073 beego.Error("解压失败", zipFile, err.Error())
1074 return
1075 }
1076
1077 //读取文件,把图片文档录入oss
1078 if files, err := filetil.ScanFiles(unzipPath); err == nil {
1079 projectRoot = this.getProjectRoot(files)
1080
1081 this.fixFileLinks(projectRoot, identify)
1082
1083 ModelStore := new(models.DocumentStore)
1084 //文档对应的标识
1085 for _, file := range files {
1086 if !file.IsDir {
1087 ext := strings.ToLower(filepath.Ext(file.Path))
1088 if ok, _ := imgMap[ext]; ok { //图片,录入oss
1089 switch utils.StoreType {
1090 case utils.StoreOss:
1091 if err := store.ModelStoreOss.MoveToOss(file.Path, filepath.Join("projects/"+identify, strings.TrimPrefix(file.Path, projectRoot)), false, false); err != nil {
1092 beego.Error(err)
1093 }
1094 case utils.StoreLocal:
1095 if err := store.ModelStoreLocal.MoveToStore(file.Path, filepath.Join("uploads/projects/"+identify, strings.TrimPrefix(file.Path, projectRoot))); err != nil {
1096 beego.Error(err)
1097 }
1098 }
1099 } else if ext == ".md" || ext == ".markdown" || ext == ".html" { //markdown文档,提取文档内容,录入数据库
1100 doc := new(models.Document)
1101 var mdcont string
1102 var htmlStr string
1103 if b, err := ioutil.ReadFile(file.Path); err == nil {
1104 mdcont = strings.TrimSpace(string(b))

Callers 2

DownloadProjectMethod · 0.95
UploadProjectMethod · 0.95

Calls 8

getProjectRootMethod · 0.95
fixFileLinksMethod · 0.95
ParseTitleFromMdHtmlFunction · 0.92
stringFunction · 0.85
MoveToOssMethod · 0.80
MoveToStoreMethod · 0.80
ReplaceMethod · 0.45
InsertOrUpdateMethod · 0.45

Tested by

no test coverage detected