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

Method loadByFolder

controllers/BookController.go:1151–1225  ·  view source on GitHub ↗
(bookId int, identify, folder string)

Source from the content-addressed store, hash-verified

1149}
1150
1151func (this *BookController) loadByFolder(bookId int, identify, folder string) {
1152 //说明:
1153
1154 imgMap := map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".bmp": true, ".svg": true, ".webp": true}
1155
1156 defer func() {
1157 os.RemoveAll(folder) //删除解压后的文件夹
1158 }()
1159
1160 //注意:这里的prefix必须是判断是否是GitHub之前的prefix
1161
1162 //读取文件,把图片文档录入oss
1163 files, err := filetil.ScanFiles(folder)
1164 if err != nil {
1165 beego.Error(err)
1166 return
1167 }
1168
1169 this.fixFileLinks(folder, identify)
1170
1171 ModelStore := new(models.DocumentStore)
1172
1173 //文档对应的标识
1174 for _, file := range files {
1175 if !file.IsDir {
1176 ext := strings.ToLower(filepath.Ext(file.Path))
1177
1178 if ok, _ := imgMap[ext]; ok { //图片,录入oss
1179 switch utils.StoreType {
1180 case utils.StoreOss:
1181 if err := store.ModelStoreOss.MoveToOss(file.Path, "projects/"+identify+strings.TrimPrefix(file.Path, folder), false, false); err != nil {
1182 beego.Error(err)
1183 }
1184 case utils.StoreLocal:
1185 if err := store.ModelStoreLocal.MoveToStore(file.Path, "uploads/projects/"+identify+strings.TrimPrefix(file.Path, folder)); err != nil {
1186 beego.Error(err)
1187 }
1188 }
1189 } else if ext == ".md" || ext == ".markdown" { //markdown文档,提取文档内容,录入数据库
1190 doc := new(models.Document)
1191 if b, err := ioutil.ReadFile(file.Path); err == nil {
1192 mdCont := strings.TrimSpace(string(b))
1193 if !strings.HasPrefix(mdCont, "[TOC]") {
1194 mdCont = "[TOC]\r\n\r\n" + mdCont
1195 }
1196 htmlStr := mdtil.Md2html(mdCont)
1197 doc.DocumentName = utils.ParseTitleFromMdHtml(htmlStr)
1198 doc.BookId = bookId
1199 //文档标识
1200 doc.Identify = strings.Replace(strings.Trim(strings.TrimPrefix(file.Path, folder), "/"), "/", "-", -1)
1201 doc.MemberId = this.Member.MemberId
1202 doc.OrderSort = 1
1203 if strings.HasSuffix(strings.ToLower(file.Name), "summary.md") {
1204 doc.OrderSort = 0
1205 }
1206 if docId, err := doc.InsertOrUpdate(); err == nil {
1207 if err := ModelStore.InsertOrUpdate(models.DocumentStore{
1208 DocumentId: int(docId),

Callers 1

GitPullMethod · 0.95

Calls 7

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