forked from mnt-ltd/moredoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrateContent.go
59 lines (53 loc) · 1.68 KB
/
migrateContent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package service
import (
"moredoc/conf"
"moredoc/model"
"os"
"path/filepath"
"strings"
"go.uber.org/zap"
"gorm.io/gorm"
)
func MigrateContent(cfg *conf.Config, logger *zap.Logger) {
lg := logger.Named("MigrateContent")
lg.Info("start...")
dbModel, err := model.NewDBModel(&cfg.Database, logger)
if err != nil {
lg.Fatal("NewDBModel", zap.Error(err))
return
}
page := 1
size := 100
for {
var attachments []model.Attachment
err := dbModel.DB().Where("type = ?", model.AttachmentTypeDocument).Select("hash", "path").Group("hash").Offset((page - 1) * size).Limit(size).Order("id asc").Find(&attachments).Error
if err != nil && err != gorm.ErrRecordNotFound {
lg.Error("查询附件失败", zap.Error(err))
break
}
if len(attachments) == 0 {
break
}
for _, attachment := range attachments {
lg.Info("MigrateContent", zap.String("hash", attachment.Hash))
textFile := strings.TrimLeft(strings.TrimSuffix(attachment.Path, filepath.Ext(attachment.Path)), "./") + "/content.txt"
content, err := os.ReadFile(textFile)
if err != nil {
lg.Debug("读取文本内容失败,跳过...", zap.String("hash", attachment.Hash), zap.Error(err), zap.String("textFile", textFile))
continue
}
contentStr := string(content)
replacer := strings.NewReplacer("\r", " ", "\n", " ", "\t", " ")
contentStr = strings.TrimSpace(replacer.Replace(contentStr))
err = dbModel.SetAttachmentContentByHash(attachment.Hash, []byte(contentStr))
if err != nil {
lg.Error("SetAttachmentContentByHash", zap.Error(err), zap.String("hash", attachment.Hash))
} else {
// 删除原 txt 文件
os.Remove(textFile)
}
}
page++
}
lg.Info("migrate content done!")
}