|
@@ -0,0 +1,115 @@
|
|
|
+package conversion
|
|
|
+
|
|
|
+import (
|
|
|
+ IC "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/init"
|
|
|
+ "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/util"
|
|
|
+ "app.yhyue.com/moapp/jy_docs/services/model"
|
|
|
+ "app.yhyue.com/moapp/jy_docs/services/partner"
|
|
|
+ "app.yhyue.com/moapp/jyfs/rpc/filesystem"
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+ "os/exec"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type Conversion struct {
|
|
|
+ DocId string
|
|
|
+ FileName string
|
|
|
+ FileId string
|
|
|
+ Suffix string
|
|
|
+ Content []byte
|
|
|
+ Size int
|
|
|
+}
|
|
|
+
|
|
|
+var (
|
|
|
+ CL = &sync.Mutex{}
|
|
|
+ filePath = "conversion/input"
|
|
|
+ fileSuffix = "pdf"
|
|
|
+)
|
|
|
+
|
|
|
+func NewConversion(docId, fileName, fileId, suffix string, b []byte, size int) *Conversion {
|
|
|
+ return &Conversion{
|
|
|
+ DocId: docId,
|
|
|
+ FileName: fileName,
|
|
|
+ FileId: fileId,
|
|
|
+ Suffix: suffix,
|
|
|
+ Content: b,
|
|
|
+ Size: size,
|
|
|
+ }
|
|
|
+}
|
|
|
+func (c *Conversion) ToConversion() {
|
|
|
+ CL.Lock()
|
|
|
+ defer CL.Unlock()
|
|
|
+ // 检查目录是否存在
|
|
|
+ _, err := os.Stat(filePath)
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ // 创建输出文件夹
|
|
|
+ err := os.MkdirAll(filePath, 0755)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error creating output folder:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //写入文件
|
|
|
+ inputFilename := fmt.Sprintf("%s/%s.%s", filePath, c.FileId, c.Suffix)
|
|
|
+ file, err := os.Create(inputFilename)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error creating file:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+ // 设置文件权限
|
|
|
+ err = file.Chmod(0777)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Error setting file permissions for %s: %v\n", filePath, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 写入文件流
|
|
|
+ _, err = file.Write(c.Content)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error writing to file:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 开始执行转换
|
|
|
+ cmd := exec.Command("unoconv", "-f", fileSuffix, inputFilename)
|
|
|
+ cmd.Stdout = nil
|
|
|
+ cmd.Stderr = nil
|
|
|
+ err = cmd.Run()
|
|
|
+ if err != nil {
|
|
|
+ log.Println(fmt.Errorf("conversion failed: %w", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ pdfFile := fmt.Sprintf("%s/%s.%s", filePath, c.FileId, fileSuffix)
|
|
|
+ c.Content, err = os.ReadFile(pdfFile)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(fmt.Errorf("read pdf err:%s", err.Error()))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(c.Content) > 0 {
|
|
|
+ //上传文件
|
|
|
+ fileRes := util.FileUpload(&filesystem.SaveFileReq{
|
|
|
+ Domain: IC.C.OssInfo.BucketName,
|
|
|
+ FileId: fmt.Sprintf("%s.%s", c.FileId, fileSuffix),
|
|
|
+ Meta: map[string]string{
|
|
|
+ "docName": c.FileName,
|
|
|
+ "docSuffix": fileSuffix,
|
|
|
+ "docSize": fmt.Sprintf("%d", c.Size),
|
|
|
+ },
|
|
|
+ RawFileContent: c.Content,
|
|
|
+ Charset: util.GetFileChardet(c.Content),
|
|
|
+ })
|
|
|
+ if fileRes.OssDocId == "" {
|
|
|
+ fmt.Println(" upload oss err:", fileRes.Msg)
|
|
|
+ } else {
|
|
|
+ if err = partner.DocsUpdate(2, model.Doc{
|
|
|
+ Id: c.DocId,
|
|
|
+ OssPdfId: fmt.Sprintf("%s.%s", c.FileId, fileSuffix),
|
|
|
+ UpdateDate: time.Now(),
|
|
|
+ }); err != nil {
|
|
|
+ log.Println(" update doc osspdfid err :", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|