调整文档转换文件夹规则

dev
truthhun 1 year ago
parent cbcb4236b0
commit 55c1e4e0da

3
.gitignore vendored

@ -15,4 +15,5 @@ sitemap
!util/sitemap
license.cert
*.cert
/logs
/logs
/util/cloudstore

@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/gofrs/uuid"
"go.uber.org/zap"
)
@ -47,11 +48,13 @@ func NewConverter(logger *zap.Logger, timeout ...time.Duration) *Converter {
}
defaultCachePath := "cache/convert"
os.MkdirAll(defaultCachePath, os.ModePerm)
return &Converter{
cvt := &Converter{
cachePath: defaultCachePath,
timeout: expire,
logger: logger.Named("converter"),
}
cvt.workspace = cvt.makeWorkspace()
return cvt
}
func (c *Converter) SetCachePath(cachePath string) {
@ -113,8 +116,7 @@ func (c *Converter) ConvertMOBIToPDF(src string) (dst string, err error) {
// ConvertPDFToTxt 将PDF转为TXT
func (c *Converter) ConvertPDFToTxt(src string) (dst string, err error) {
workspace := c.makeWorkspace(src)
dst = workspace + "/dst.txt"
dst = c.workspace + "/dst.txt"
args := []string{
"convert",
"-o",
@ -168,8 +170,7 @@ func (c *Converter) ConvertPDFToPNG(src string, fromPage, toPage int) (pages []P
}
func (c *Converter) PDFToPDF(src string) (dst string, err error) {
workspace := c.makeWorkspace(src)
dst = workspace + "/dst.pdf"
dst = c.workspace + "/dst.pdf"
err = util.CopyFile(src, dst)
if err != nil {
c.logger.Error("copy file error", zap.Error(err))
@ -180,8 +181,7 @@ func (c *Converter) PDFToPDF(src string) (dst string, err error) {
// ext 可选值: .png, .svg
func (c *Converter) convertPDFToPage(src string, fromPage, toPage int, ext string) (pages []Page, err error) {
pageRange := fmt.Sprintf("%d-%d", fromPage, toPage)
workspace := c.makeWorkspace(src)
cacheFileFormat := workspace + "/%d" + ext
cacheFileFormat := c.workspace + "/%d" + ext
args := []string{
"convert",
"-o",
@ -212,14 +212,13 @@ func (c *Converter) convertPDFToPage(src string, fromPage, toPage int, ext strin
}
func (c *Converter) convertToPDFBySoffice(src string) (dst string, err error) {
workspace := c.makeWorkspace(src)
dst = workspace + "/" + strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + ".pdf"
dst = c.workspace + "/" + strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + ".pdf"
args := []string{
"--headless",
"--convert-to",
"pdf",
"--outdir",
workspace,
c.workspace,
}
args = append(args, src)
c.logger.Info("convert to pdf by soffice", zap.String("cmd", soffice), zap.Strings("args", args))
@ -231,8 +230,7 @@ func (c *Converter) convertToPDFBySoffice(src string) (dst string, err error) {
}
func (c *Converter) convertToPDFByCalibre(src string) (dst string, err error) {
workspace := c.makeWorkspace(src)
dst = workspace + "/dst.pdf"
dst = c.workspace + "/dst.pdf"
args := []string{
src,
dst,
@ -349,12 +347,13 @@ func (c *Converter) CompressSVGByGZIP(svgFile string) (dst string, err error) {
return
}
func (c *Converter) makeWorkspace(src string) (workspaceDir string) {
func (c *Converter) makeWorkspace() (workspaceDir string) {
if c.workspace != "" {
workspaceDir = c.workspace
return
}
c.workspace = strings.ReplaceAll(filepath.Join(c.cachePath, time.Now().Format(dirDteFmt), strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))), "\\", "/")
uid := uuid.Must(uuid.NewV4()).String()
c.workspace = filepath.ToSlash(filepath.Join(c.cachePath, time.Now().Format(dirDteFmt), uid))
return c.workspace
}

Loading…
Cancel
Save