feat: replace export url
This commit is contained in:
@@ -3,10 +3,10 @@ package service
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -151,19 +151,29 @@ func (svc *TaskService) ExportTask(ctx context.Context, req *task.ExportTaskRequ
|
|||||||
return nil, "", errors.New("markdown not found")
|
return nil, "", errors.New("markdown not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
// call http://localhost:8055/export
|
// 获取文件名(去掉扩展名)
|
||||||
body := &bytes.Buffer{}
|
filename := strings.TrimSuffix(recognitionTask.FileName, "."+strings.ToLower(strings.Split(recognitionTask.FileName, ".")[len(strings.Split(recognitionTask.FileName, "."))-1]))
|
||||||
writer := multipart.NewWriter(body)
|
if filename == "" {
|
||||||
_ = writer.WriteField("markdown_input", markdown)
|
filename = "texpixel"
|
||||||
_ = writer.WriteField("type", req.Type)
|
}
|
||||||
writer.Close()
|
|
||||||
|
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://cloud.texpixel.com:10443/doc_converter/v1/export", body)
|
// 构建 JSON 请求体
|
||||||
|
requestBody := map[string]string{
|
||||||
|
"markdown": markdown,
|
||||||
|
"filename": filename,
|
||||||
|
}
|
||||||
|
jsonData, err := json.Marshal(requestBody)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(ctx, "func", "ExportTask", "msg", "json marshal failed", "error", err)
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://cloud.texpixel.com:10443/doc_process/v1/convert/file", bytes.NewReader(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(ctx, "func", "ExportTask", "msg", "create http request failed", "error", err)
|
log.Error(ctx, "func", "ExportTask", "msg", "create http request failed", "error", err)
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
httpReq.Header.Set("Content-Type", writer.FormDataContentType())
|
httpReq.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(httpReq)
|
resp, err := client.Do(httpReq)
|
||||||
@@ -184,16 +194,8 @@ func (svc *TaskService) ExportTask(ctx context.Context, req *task.ExportTaskRequ
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine content type based on export type
|
// 新接口只返回 DOCX 格式
|
||||||
var contentType string
|
contentType := "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||||
switch req.Type {
|
|
||||||
case "pdf":
|
|
||||||
contentType = "application/pdf"
|
|
||||||
case "docx":
|
|
||||||
contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
||||||
default:
|
|
||||||
contentType = "application/octet-stream"
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileData, contentType, nil
|
return fileData, contentType, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user