Skip to content

Commit

Permalink
svg处理
Browse files Browse the repository at this point in the history
  • Loading branch information
TruthHun committed Nov 19, 2023
1 parent 785b5ac commit 3a1bec6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 17 additions & 1 deletion model/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,23 @@ func (m *DBModel) reconvertDocument(doc *Document, ext string) {

// 2. 转换文档预览文件
convertedTargetFile := filepath.Join(cacheDir, fmt.Sprintf("%d%s", i, ext))
err = converter.ConvertByImageMagick(dstFile, convertedTargetFile)
if strings.HasSuffix(oldExt, ".svg") {
// 如果是svg文件,则需要使用inkscape预先转为png
tmpFile := filepath.Join(cacheDir, fmt.Sprintf("tmp-%d.png", i))
err = converter.ConvertByInkscape(dstFile, tmpFile)
if err == nil {
if strings.HasSuffix(convertedTargetFile, ".png") {
// 如果目标文件是png,则直接使用inkscape转换后的文件
convertedTargetFile = tmpFile
} else {
// 如果目标文件不是png,则需要使用ImageMagick转换
err = converter.ConvertByImageMagick(tmpFile, convertedTargetFile)
os.RemoveAll(tmpFile)
}
}
} else {
err = converter.ConvertByImageMagick(dstFile, convertedTargetFile)
}
if err != nil {
m.logger.Error("reconvertDocument", zap.String("msg", "转换文档预览文件失败"), zap.String("document", doc.Title+doc.Ext), zap.Error(err))
return
Expand Down
11 changes: 11 additions & 0 deletions util/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,14 @@ func ConvertByImageMagick(src, dst string, moreArgs ...string) (err error) {
_, err = command.ExecCommand(imageMagick, args)
return
}

// 通过Inksacpe进行转换,如将svg转为png
func ConvertByInkscape(src, dst string, moreArgs ...string) (err error) {
args := []string{
"-o", dst,
}
args = append(args, moreArgs...)
args = append(args, src)
_, err = command.ExecCommand(inkscape, args)
return
}

0 comments on commit 3a1bec6

Please sign in to comment.