文档转换 @param tmpFile 临时存储的文件 @param fileMD5 文件md5 @param page 需要转换的页数,0表示全部转换
(tmpFile string, fileMD5 string, page ...int)
| 213 | // @param fileMD5 文件md5 |
| 214 | // @param page 需要转换的页数,0表示全部转换 |
| 215 | func DocumentConvert(tmpFile string, fileMD5 string, page ...int) (err error) { |
| 216 | // 1. 先把原文档上传到云存储 |
| 217 | // 2. 把文档转成 PDF(如果原文档不是PDF的话) |
| 218 | // 3. 提取 PDF 文档中的部分文本内容 |
| 219 | // 4. 把 PDF 转 svg |
| 220 | // 5. 把 SVG 转 JPG 封面(封面需要裁剪) |
| 221 | // 6. 判断云存储类型,以确定是否需要压缩 svg 成gzip(先加水印再压缩) |
| 222 | // 7. 上传 svg、jpeg 等到云存储 |
| 223 | // 8. 更新 document_store 表的信息,如页数、SVG宽高 |
| 224 | // 9. 更新 document_info 中的文档转换状态为正常状态 |
| 225 | // 10. 更新 document_text 中的文档内容信息 |
| 226 | |
| 227 | if _, err = os.Stat(tmpFile); err != nil { |
| 228 | helper.Logger.Error(err.Error()) |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | if helper.Debug { |
| 233 | helper.Logger.Debug("文档转换中: %v ==> %v", tmpFile, fileMD5) |
| 234 | } |
| 235 | |
| 236 | ext := filepath.Ext(tmpFile) |
| 237 | extLower := strings.ToLower(ext) |
| 238 | tmpDir := strings.TrimSuffix(tmpFile, ext) |
| 239 | os.MkdirAll(tmpDir, os.ModePerm) |
| 240 | pdfFile := tmpDir + helper.ExtPDF |
| 241 | coverJPG := tmpDir + ".jpg" //default |
| 242 | |
| 243 | defer func() { |
| 244 | os.Remove(tmpFile) |
| 245 | os.Remove(pdfFile) |
| 246 | os.Remove(coverJPG) |
| 247 | os.RemoveAll(tmpDir) |
| 248 | }() |
| 249 | |
| 250 | var ( |
| 251 | clientPrivate *CloudStore |
| 252 | clientPublic *CloudStore |
| 253 | ) |
| 254 | |
| 255 | if clientPrivate, err = NewCloudStore(true); err != nil { |
| 256 | helper.Logger.Error(err.Error()) |
| 257 | return |
| 258 | } |
| 259 | |
| 260 | if clientPublic, err = NewCloudStore(false); err != nil { |
| 261 | helper.Logger.Error(err.Error()) |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | if err = clientPrivate.Upload(tmpFile, fileMD5+ext); err != nil { |
| 266 | helper.Logger.Error(err.Error()) |
| 267 | return |
| 268 | } |
| 269 | |
| 270 | if _, ok := helper.AllowedUploadDocsExt[extLower]; !ok { |
| 271 | return errors.New("不允许上传的文档类型") |
| 272 | } |
no test coverage detected