init push
This commit is contained in:
341
backend/pkg/anydoc/anydoc.go
Normal file
341
backend/pkg/anydoc/anydoc.go
Normal file
@@ -0,0 +1,341 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/chaitin/panda-wiki/domain"
|
||||
"github.com/chaitin/panda-wiki/log"
|
||||
"github.com/chaitin/panda-wiki/mq"
|
||||
"github.com/chaitin/panda-wiki/mq/types"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
logger *log.Logger
|
||||
mqConsumer mq.MQConsumer
|
||||
taskWaiters map[string]chan *domain.AnydocTaskExportEvent
|
||||
mutex sync.RWMutex
|
||||
subscribed bool
|
||||
subscribeMu sync.Mutex
|
||||
}
|
||||
|
||||
const (
|
||||
apiUploaderUrl = "http://panda-wiki-api:8000/api/v1/file/upload/anydoc"
|
||||
uploaderDir = "/image"
|
||||
crawlerServiceHost = "http://panda-wiki-crawler:8080"
|
||||
SpaceIdCloud = "cloud_disk"
|
||||
getUrlPath = "/api/docs/url/list"
|
||||
UrlExportPath = "/api/docs/url/export"
|
||||
TaskListPath = "/api/tasks/list"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusPending Status = "pending"
|
||||
StatusInProgress Status = "in_process"
|
||||
StatusCompleted Status = "completed"
|
||||
StatusFailed Status = "failed"
|
||||
)
|
||||
|
||||
type UploaderType uint
|
||||
|
||||
const (
|
||||
uploaderTypeDefault UploaderType = iota
|
||||
uploaderTypeHTTP
|
||||
)
|
||||
|
||||
func NewClient(logger *log.Logger, mqConsumer mq.MQConsumer) (*Client, error) {
|
||||
client := &Client{
|
||||
logger: logger.WithModule("anydoc.client"),
|
||||
httpClient: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
taskWaiters: make(map[string]chan *domain.AnydocTaskExportEvent),
|
||||
mqConsumer: mqConsumer,
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetUrlList(ctx context.Context, targetURL, id string) (*ListDocResponse, error) {
|
||||
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = getUrlPath
|
||||
q := u.Query()
|
||||
q.Set("url", targetURL)
|
||||
q.Set("uuid", id)
|
||||
u.RawQuery = q.Encode()
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.logger.Info("scrape url", "requestURL:", requestURL, "resp", string(respBody))
|
||||
var scrapeResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &scrapeResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !scrapeResp.Success {
|
||||
return nil, errors.New(scrapeResp.Msg)
|
||||
}
|
||||
|
||||
return &scrapeResp, nil
|
||||
}
|
||||
|
||||
func (c *Client) UrlExport(ctx context.Context, id, docID, kbId string) (*UrlExportRes, error) {
|
||||
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = UrlExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": id,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.logger.Info("UrlExport", "requestURL:", requestURL, "resp", string(respBody))
|
||||
var res UrlExportRes
|
||||
err = json.Unmarshal(respBody, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !res.Success {
|
||||
return nil, errors.New(res.Msg)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// ensureSubscribed 确保已订阅消息队列,只订阅一次
|
||||
func (c *Client) ensureSubscribed() error {
|
||||
c.subscribeMu.Lock()
|
||||
defer c.subscribeMu.Unlock()
|
||||
|
||||
if c.subscribed {
|
||||
return nil
|
||||
}
|
||||
|
||||
if c.mqConsumer == nil {
|
||||
return fmt.Errorf("MQ consumer not initialized")
|
||||
}
|
||||
|
||||
err := c.mqConsumer.RegisterHandler(domain.AnydocTaskExportTopic, c.handleTaskExportEvent)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to register task export handler: %w", err)
|
||||
}
|
||||
|
||||
c.subscribed = true
|
||||
c.logger.Info("successfully subscribed to anydoc task export topic")
|
||||
return nil
|
||||
}
|
||||
|
||||
// TaskWaitForCompletion 通过 NATS 消息队列等待任务完成(推荐方式)
|
||||
func (c *Client) TaskWaitForCompletion(ctx context.Context, taskID string) (*domain.AnydocTaskExportEvent, error) {
|
||||
if c.mqConsumer == nil {
|
||||
return nil, fmt.Errorf("MQ consumer not initialized, use NewClientWithMQ instead")
|
||||
}
|
||||
|
||||
// 延迟订阅:只有在需要时才订阅
|
||||
if err := c.ensureSubscribed(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a channel to wait for the specific task
|
||||
taskChan := make(chan *domain.AnydocTaskExportEvent, 1)
|
||||
|
||||
c.mutex.Lock()
|
||||
c.taskWaiters[taskID] = taskChan
|
||||
c.mutex.Unlock()
|
||||
|
||||
// Cleanup when done
|
||||
defer func() {
|
||||
c.mutex.Lock()
|
||||
delete(c.taskWaiters, taskID)
|
||||
c.mutex.Unlock()
|
||||
close(taskChan)
|
||||
}()
|
||||
|
||||
// Wait for task completion or context cancellation
|
||||
select {
|
||||
case event := <-taskChan:
|
||||
return event, nil
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// TaskListPoll 轮询方式(保留兼容性)
|
||||
func (c *Client) TaskListPoll(ctx context.Context, ids []string) (*TaskRes, error) {
|
||||
depth := 0
|
||||
const maxDepth = 10
|
||||
|
||||
for depth < maxDepth {
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
resp, err := c.TaskList(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Data[0].Status == StatusCompleted {
|
||||
return resp, nil
|
||||
}
|
||||
depth++
|
||||
}
|
||||
return nil, fmt.Errorf("task list poll timeout")
|
||||
}
|
||||
|
||||
// handleTaskExportEvent 处理任务导出完成事件
|
||||
func (c *Client) handleTaskExportEvent(ctx context.Context, msg types.Message) error {
|
||||
var event domain.AnydocTaskExportEvent
|
||||
if err := json.Unmarshal(msg.GetData(), &event); err != nil {
|
||||
c.logger.Error("failed to unmarshal task export event", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.logger.Info("received task export event",
|
||||
"task_id", event.TaskID,
|
||||
"status", event.Status,
|
||||
"doc_id", event.DocID)
|
||||
|
||||
// Notify waiting goroutines
|
||||
c.mutex.RLock()
|
||||
if taskChan, exists := c.taskWaiters[event.TaskID]; exists {
|
||||
select {
|
||||
case taskChan <- &event:
|
||||
default:
|
||||
// Channel is full or closed, ignore
|
||||
}
|
||||
}
|
||||
c.mutex.RUnlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) TaskList(ctx context.Context, ids []string) (*TaskRes, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = TaskListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"ids": ids,
|
||||
}
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.logger.Info("TaskList url", "requestURL", requestURL, "resp", string(respBody))
|
||||
var res TaskRes
|
||||
err = json.Unmarshal(respBody, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !res.Success {
|
||||
return nil, errors.New(res.Msg)
|
||||
}
|
||||
if len(res.Data) == 0 {
|
||||
return nil, errors.New("data list is empty")
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func (c *Client) DownloadDoc(ctx context.Context, filepath string) ([]byte, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = "/api/tasks/download" + filepath
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.logger.Info("DownloadDoc", "requestURL:", requestURL, "resp length", len(respBody))
|
||||
return respBody, nil
|
||||
}
|
||||
154
backend/pkg/anydoc/confluence.go
Normal file
154
backend/pkg/anydoc/confluence.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
ConfluenceListPath = "/api/docs/confluence/list"
|
||||
ConfluenceExportPath = "/api/docs/confluence/export"
|
||||
)
|
||||
|
||||
// ConfluenceListDocsRequest Confluence 获取文档列表请求
|
||||
type ConfluenceListDocsRequest struct {
|
||||
URL string `json:"url"` // Confluence 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// ConfluenceExportDocRequest Confluence 导出文档请求
|
||||
type ConfluenceExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // confluence-doc-id
|
||||
}
|
||||
|
||||
// ConfluenceExportDocResponse Confluence 导出文档响应
|
||||
type ConfluenceExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// ConfluenceExportDocData Confluence 导出文档数据
|
||||
type ConfluenceExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// ConfluenceListDocs 获取 Confluence 文档列表
|
||||
func (c *Client) ConfluenceListDocs(ctx context.Context, confluenceURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = ConfluenceListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": confluenceURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("ConfluenceListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var confluenceResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &confluenceResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !confluenceResp.Success {
|
||||
return nil, errors.New(confluenceResp.Msg)
|
||||
}
|
||||
|
||||
return &confluenceResp, nil
|
||||
}
|
||||
|
||||
// ConfluenceExportDoc 导出 Confluence 文档
|
||||
func (c *Client) ConfluenceExportDoc(ctx context.Context, uuid, docID, kbId string) (*ConfluenceExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = ConfluenceExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("ConfluenceExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp ConfluenceExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
70
backend/pkg/anydoc/dingtalk.go
Normal file
70
backend/pkg/anydoc/dingtalk.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
dingtalkListPath = "/api/docs/dingtalk/list"
|
||||
dingtalkExportPath = "/api/docs/dingtalk/export"
|
||||
)
|
||||
|
||||
// DingtalkListDocs 获取 dingtalk 文档列表
|
||||
func (c *Client) DingtalkListDocs(ctx context.Context, uuid string, dingtalkSetting DingtalkSetting) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = dingtalkListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"app_id": dingtalkSetting.AppID,
|
||||
"app_secret": dingtalkSetting.AppSecret,
|
||||
"unionid": dingtalkSetting.UnionID,
|
||||
"space_id": dingtalkSetting.SpaceID,
|
||||
"phone": dingtalkSetting.Phone,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("dingtalkListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var dingtalkResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &dingtalkResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !dingtalkResp.Success {
|
||||
return nil, errors.New(dingtalkResp.Msg)
|
||||
}
|
||||
|
||||
return &dingtalkResp, nil
|
||||
}
|
||||
173
backend/pkg/anydoc/epub.go
Normal file
173
backend/pkg/anydoc/epub.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
epubpListPath = "/api/docs/epubp/list"
|
||||
epubpExportPath = "/api/docs/epubp/export"
|
||||
)
|
||||
|
||||
// EpubpListDocsRequest Epubp 获取文档列表请求
|
||||
type EpubpListDocsRequest struct {
|
||||
URL string `json:"url"` // Epubp 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// EpubpListDocsResponse Epubp 获取文档列表响应
|
||||
type EpubpListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data EpubpListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// EpubpListDocsData Epubp 文档列表数据
|
||||
type EpubpListDocsData struct {
|
||||
Docs []EpubpDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// EpubpDoc Epubp 文档信息
|
||||
type EpubpDoc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// EpubpExportDocRequest Epubp 导出文档请求
|
||||
type EpubpExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // epubp-doc-id
|
||||
}
|
||||
|
||||
// EpubpExportDocResponse Epubp 导出文档响应
|
||||
type EpubpExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// EpubpExportDocData Epubp 导出文档数据
|
||||
type EpubpExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// EpubpListDocs 获取 Epubp 文档列表
|
||||
func (c *Client) EpubpListDocs(ctx context.Context, epubpURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = epubpListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": epubpURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("EpubpListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var epubpResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &epubpResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !epubpResp.Success {
|
||||
return nil, errors.New(epubpResp.Msg)
|
||||
}
|
||||
|
||||
return &epubpResp, nil
|
||||
}
|
||||
|
||||
// EpubpExportDoc 导出 Epubp 文档
|
||||
func (c *Client) EpubpExportDoc(ctx context.Context, uuid, docID, kbId string) (*EpubpExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = epubpExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("EpubpExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp EpubpExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
175
backend/pkg/anydoc/feishu.go
Normal file
175
backend/pkg/anydoc/feishu.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
feishuListPath = "/api/docs/feishu/list"
|
||||
feishuExportPath = "/api/docs/feishu/export"
|
||||
)
|
||||
|
||||
// FeishuListDocsRequest Feishu 获取文档列表请求
|
||||
type FeishuListDocsRequest struct {
|
||||
URL string `json:"url"` // Feishu 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// FeishuListDocsResponse Feishu 获取文档列表响应
|
||||
type FeishuListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data FeishuListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// FeishuListDocsData Feishu 文档列表数据
|
||||
type FeishuListDocsData struct {
|
||||
Docs []FeishuDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// FeishuDoc Feishu 文档信息
|
||||
type FeishuDoc struct {
|
||||
ID string `json:"id"`
|
||||
FileType string `json:"file_type"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
// FeishuExportDocRequest Feishu 导出文档请求
|
||||
type FeishuExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // feishu-doc-id
|
||||
}
|
||||
|
||||
// FeishuExportDocResponse Feishu 导出文档响应
|
||||
type FeishuExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// FeishuExportDocData Feishu 导出文档数据
|
||||
type FeishuExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// FeishuListDocs 获取 Feishu 文档列表
|
||||
func (c *Client) FeishuListDocs(ctx context.Context, uuid, appId, appSecret, accessToken, spaceId string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = feishuListPath
|
||||
|
||||
q := u.Query()
|
||||
q.Set("uuid", uuid)
|
||||
q.Set("app_id", appId)
|
||||
q.Set("app_secret", appSecret)
|
||||
q.Set("access_token", accessToken)
|
||||
if spaceId != "" {
|
||||
q.Set("space_id", spaceId)
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("FeishuListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var feishuResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &feishuResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !feishuResp.Success {
|
||||
return nil, errors.New(feishuResp.Msg)
|
||||
}
|
||||
|
||||
return &feishuResp, nil
|
||||
}
|
||||
|
||||
// FeishuExportDoc 导出 Feishu 文档
|
||||
func (c *Client) FeishuExportDoc(ctx context.Context, uuid, docID, fileType, spaceId, kbId string) (*UrlExportRes, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = feishuExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"file_type": fileType,
|
||||
"space_id": spaceId,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("FeishuDoc", "requestURL:", requestURL, "body", string(jsonData), "resp", string(respBody))
|
||||
|
||||
var exportResp UrlExportRes
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
173
backend/pkg/anydoc/mindoc.go
Normal file
173
backend/pkg/anydoc/mindoc.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
mindocListPath = "/api/docs/mindoc/list"
|
||||
mindocExportPath = "/api/docs/mindoc/export"
|
||||
)
|
||||
|
||||
// MindocListDocsRequest Mindoc 获取文档列表请求
|
||||
type MindocListDocsRequest struct {
|
||||
URL string `json:"url"` // Mindoc 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// MindocListDocsResponse Mindoc 获取文档列表响应
|
||||
type MindocListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data MindocListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// MindocListDocsData Mindoc 文档列表数据
|
||||
type MindocListDocsData struct {
|
||||
Docs []MindocDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// MindocDoc Mindoc 文档信息
|
||||
type MindocDoc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// MindocExportDocRequest Mindoc 导出文档请求
|
||||
type MindocExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // mindoc-doc-id
|
||||
}
|
||||
|
||||
// MindocExportDocResponse Mindoc 导出文档响应
|
||||
type MindocExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// MindocExportDocData Mindoc 导出文档数据
|
||||
type MindocExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// MindocListDocs 获取 Mindoc 文档列表
|
||||
func (c *Client) MindocListDocs(ctx context.Context, mindocURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = mindocListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": mindocURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("MindocListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var mindocResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &mindocResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !mindocResp.Success {
|
||||
return nil, errors.New(mindocResp.Msg)
|
||||
}
|
||||
|
||||
return &mindocResp, nil
|
||||
}
|
||||
|
||||
// MindocExportDoc 导出 Mindoc 文档
|
||||
func (c *Client) MindocExportDoc(ctx context.Context, uuid, docID, kbId string) (*MindocExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = mindocExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("MindocExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp MindocExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
148
backend/pkg/anydoc/notion.go
Normal file
148
backend/pkg/anydoc/notion.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
notionListPath = "/api/docs/notion/list"
|
||||
notionExportPath = "/api/docs/notion/export"
|
||||
)
|
||||
|
||||
// NotionListDocsResponse Notion 获取文档列表响应
|
||||
type NotionListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data NotionListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// NotionListDocsData Notion 文档列表数据
|
||||
type NotionListDocsData struct {
|
||||
Docs []NotionDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// NotionDoc Notion 文档信息
|
||||
type NotionDoc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// NotionExportDocResponse Notion 导出文档响应
|
||||
type NotionExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// NotionListDocs 获取 Notion 文档列表
|
||||
func (c *Client) NotionListDocs(ctx context.Context, secret, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = notionListPath
|
||||
|
||||
q := u.Query()
|
||||
q.Set("uuid", uuid)
|
||||
q.Set("secret", secret)
|
||||
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("NotionListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var notionResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, ¬ionResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !notionResp.Success {
|
||||
return nil, errors.New(notionResp.Msg)
|
||||
}
|
||||
|
||||
return ¬ionResp, nil
|
||||
}
|
||||
|
||||
// NotionExportDoc 导出 Notion 文档
|
||||
func (c *Client) NotionExportDoc(ctx context.Context, uuid, docID, kbId string) (*NotionExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = notionExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("NotionExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp NotionExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
16
backend/pkg/anydoc/req.go
Normal file
16
backend/pkg/anydoc/req.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package anydoc
|
||||
|
||||
type FeishuSetting struct {
|
||||
UserAccessToken string `json:"user_access_token"`
|
||||
AppID string `json:"app_id"`
|
||||
AppSecret string `json:"app_secret"`
|
||||
SpaceId string `json:"space_id"`
|
||||
}
|
||||
|
||||
type DingtalkSetting struct {
|
||||
AppID string `json:"app_id"`
|
||||
AppSecret string `json:"app_secret"`
|
||||
SpaceID string `json:"space_id"`
|
||||
UnionID string `json:"unionid"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
63
backend/pkg/anydoc/res.go
Normal file
63
backend/pkg/anydoc/res.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package anydoc
|
||||
|
||||
type GetUrlListResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data GetUrlListData `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
Err string `json:"err"`
|
||||
TraceId interface{} `json:"trace_id"`
|
||||
}
|
||||
type GetUrlListData struct {
|
||||
Docs []struct {
|
||||
Id string `json:"id"`
|
||||
FileType string `json:"file_type"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
} `json:"docs"`
|
||||
}
|
||||
|
||||
type UrlExportRes struct {
|
||||
Success bool `json:"success"`
|
||||
Data string `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
Err string `json:"err"`
|
||||
TraceId interface{} `json:"trace_id"`
|
||||
}
|
||||
type TaskRes struct {
|
||||
Success bool `json:"success"`
|
||||
Data []struct {
|
||||
TaskId string `json:"task_id"`
|
||||
PlatformId string `json:"platform_id"`
|
||||
DocId string `json:"doc_id"`
|
||||
Status Status `json:"status"`
|
||||
Err string `json:"err"`
|
||||
Markdown string `json:"markdown"`
|
||||
Json string `json:"json"`
|
||||
} `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type ListDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data ListDocsData `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
Err string `json:"err"`
|
||||
TraceID string `json:"trace_id"`
|
||||
}
|
||||
|
||||
type ListDocsData struct {
|
||||
Docs Child `json:"docs"`
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
ID string `json:"id"`
|
||||
File bool `json:"file"`
|
||||
FileType string `json:"file_type"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
type Child struct {
|
||||
Value Value `json:"value"`
|
||||
Children []Child `json:"children"`
|
||||
}
|
||||
161
backend/pkg/anydoc/rss.go
Normal file
161
backend/pkg/anydoc/rss.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
rssListPath = "/api/docs/rss/list"
|
||||
rssExportPath = "/api/docs/rss/export"
|
||||
)
|
||||
|
||||
// RssListDocsResponse Rss 获取文档列表响应
|
||||
type RssListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data RssListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// RssListDocsData Rss 文档列表数据
|
||||
type RssListDocsData struct {
|
||||
Docs []RssDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// RssDoc Rss 文档信息
|
||||
type RssDoc struct {
|
||||
Id string `json:"id"`
|
||||
FileType string `json:"file_type"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
// RssExportDocRequest Rss 导出文档请求
|
||||
type RssExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // rss-doc-id
|
||||
}
|
||||
|
||||
// RssExportDocResponse Rss 导出文档响应
|
||||
type RssExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// RssExportDocData Rss 导出文档数据
|
||||
type RssExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// RssListDocs 获取 Rss 文档列表
|
||||
func (c *Client) RssListDocs(ctx context.Context, xmlUrl, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = rssListPath
|
||||
|
||||
q := u.Query()
|
||||
q.Set("uuid", uuid)
|
||||
q.Set("url", xmlUrl)
|
||||
u.RawQuery = q.Encode()
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("RssListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var rssResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &rssResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !rssResp.Success {
|
||||
return nil, errors.New(rssResp.Msg)
|
||||
}
|
||||
|
||||
return &rssResp, nil
|
||||
}
|
||||
|
||||
// RssExportDoc 导出 Rss 文档
|
||||
func (c *Client) RssExportDoc(ctx context.Context, uuid, docID, kbId string) (*RssExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = rssExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("RssExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp RssExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
161
backend/pkg/anydoc/sitemap.go
Normal file
161
backend/pkg/anydoc/sitemap.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
sitemapListPath = "/api/docs/sitemap/list"
|
||||
sitemapExportPath = "/api/docs/sitemap/export"
|
||||
)
|
||||
|
||||
// SitemapListDocsResponse Sitemap 获取文档列表响应
|
||||
type SitemapListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data SitemapListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// SitemapListDocsData Sitemap 文档列表数据
|
||||
type SitemapListDocsData struct {
|
||||
Docs []SitemapDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// SitemapDoc Sitemap 文档信息
|
||||
type SitemapDoc struct {
|
||||
Id string `json:"id"`
|
||||
FileType string `json:"file_type"`
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
// SitemapExportDocRequest Sitemap 导出文档请求
|
||||
type SitemapExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // sitemap-doc-id
|
||||
}
|
||||
|
||||
// SitemapExportDocResponse Sitemap 导出文档响应
|
||||
type SitemapExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// SitemapExportDocData Sitemap 导出文档数据
|
||||
type SitemapExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// SitemapListDocs 获取 Sitemap 文档列表
|
||||
func (c *Client) SitemapListDocs(ctx context.Context, xmlUrl, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = sitemapListPath
|
||||
|
||||
q := u.Query()
|
||||
q.Set("uuid", uuid)
|
||||
q.Set("url", xmlUrl)
|
||||
u.RawQuery = q.Encode()
|
||||
requestURL := u.String()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("SitemapListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var sitemapResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &sitemapResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !sitemapResp.Success {
|
||||
return nil, errors.New(sitemapResp.Msg)
|
||||
}
|
||||
|
||||
return &sitemapResp, nil
|
||||
}
|
||||
|
||||
// SitemapExportDoc 导出 Sitemap 文档
|
||||
func (c *Client) SitemapExportDoc(ctx context.Context, uuid, docID, kbId string) (*SitemapExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = sitemapExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("SitemapExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp SitemapExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
173
backend/pkg/anydoc/siyuan.go
Normal file
173
backend/pkg/anydoc/siyuan.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
siyuanListPath = "/api/docs/siyuan/list"
|
||||
siyuanExportPath = "/api/docs/siyuan/export"
|
||||
)
|
||||
|
||||
// SiyuanListDocsRequest Siyuan 获取文档列表请求
|
||||
type SiyuanListDocsRequest struct {
|
||||
URL string `json:"url"` // Siyuan 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// SiyuanListDocsResponse Siyuan 获取文档列表响应
|
||||
type SiyuanListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data SiyuanListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// SiyuanListDocsData Siyuan 文档列表数据
|
||||
type SiyuanListDocsData struct {
|
||||
Docs []SiyuanDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// SiyuanDoc Siyuan 文档信息
|
||||
type SiyuanDoc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// SiyuanExportDocRequest Siyuan 导出文档请求
|
||||
type SiyuanExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // siyuan-doc-id
|
||||
}
|
||||
|
||||
// SiyuanExportDocResponse Siyuan 导出文档响应
|
||||
type SiyuanExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// SiyuanExportDocData Siyuan 导出文档数据
|
||||
type SiyuanExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// SiyuanListDocs 获取 Siyuan 文档列表
|
||||
func (c *Client) SiyuanListDocs(ctx context.Context, siyuanURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = siyuanListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": siyuanURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("SiyuanListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var siyuanResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &siyuanResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !siyuanResp.Success {
|
||||
return nil, errors.New(siyuanResp.Msg)
|
||||
}
|
||||
|
||||
return &siyuanResp, nil
|
||||
}
|
||||
|
||||
// SiyuanExportDoc 导出 Siyuan 文档
|
||||
func (c *Client) SiyuanExportDoc(ctx context.Context, uuid, docID, kbId string) (*SiyuanExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = siyuanExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("SiyuanExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp SiyuanExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
154
backend/pkg/anydoc/wikijs.go
Normal file
154
backend/pkg/anydoc/wikijs.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
wikijsListPath = "/api/docs/wikijs/list"
|
||||
wikijsExportPath = "/api/docs/wikijs/export"
|
||||
)
|
||||
|
||||
// WikijsListDocsRequest Wikijs 获取文档列表请求
|
||||
type WikijsListDocsRequest struct {
|
||||
URL string `json:"url"` // Wikijs 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// WikijsExportDocRequest Wikijs 导出文档请求
|
||||
type WikijsExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // wikijs-doc-id
|
||||
}
|
||||
|
||||
// WikijsExportDocResponse Wikijs 导出文档响应
|
||||
type WikijsExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// WikijsExportDocData Wikijs 导出文档数据
|
||||
type WikijsExportDocData struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
FilePath string `json:"file_path"`
|
||||
}
|
||||
|
||||
// WikijsListDocs 获取 Wikijs 文档列表
|
||||
func (c *Client) WikijsListDocs(ctx context.Context, wikijsURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = wikijsListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": wikijsURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("WikijsListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var wikijsResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &wikijsResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !wikijsResp.Success {
|
||||
return nil, errors.New(wikijsResp.Msg)
|
||||
}
|
||||
|
||||
return &wikijsResp, nil
|
||||
}
|
||||
|
||||
// WikijsExportDoc 导出 Wikijs 文档
|
||||
func (c *Client) WikijsExportDoc(ctx context.Context, uuid, docID, kbId string) (*WikijsExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = wikijsExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("WikijsExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp WikijsExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, errors.New(exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
165
backend/pkg/anydoc/yuque.go
Normal file
165
backend/pkg/anydoc/yuque.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package anydoc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const (
|
||||
yuqueListPath = "/api/docs/yuque/list"
|
||||
yuqueExportPath = "/api/docs/yuque/export"
|
||||
)
|
||||
|
||||
// YuqueListDocsRequest Yuque 获取文档列表请求
|
||||
type YuqueListDocsRequest struct {
|
||||
URL string `json:"url"` // Yuque 配置文件
|
||||
Filename string `json:"filename"` // 文件名,需要带扩展名
|
||||
UUID string `json:"uuid"` // 必填的唯一标识符
|
||||
}
|
||||
|
||||
// YuqueListDocsResponse Yuque 获取文档列表响应
|
||||
type YuqueListDocsResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data YuqueListDocsData `json:"data"`
|
||||
}
|
||||
|
||||
// YuqueListDocsData Yuque 文档列表数据
|
||||
type YuqueListDocsData struct {
|
||||
Docs []YuqueDoc `json:"docs"`
|
||||
}
|
||||
|
||||
// YuqueDoc Yuque 文档信息
|
||||
type YuqueDoc struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// YuqueExportDocRequest Yuque 导出文档请求
|
||||
type YuqueExportDocRequest struct {
|
||||
UUID string `json:"uuid"` // 必须与 list 接口使用的 uuid 相同
|
||||
DocID string `json:"doc_id"` // yuque-doc-id
|
||||
}
|
||||
|
||||
// YuqueExportDocResponse Yuque 导出文档响应
|
||||
type YuqueExportDocResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// YuqueListDocs 获取 Yuque 文档列表
|
||||
func (c *Client) YuqueListDocs(ctx context.Context, yuqueURL, filename, uuid string) (*ListDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = yuqueListPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"url": yuqueURL,
|
||||
"filename": filename,
|
||||
"uuid": uuid,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("YuqueListDocs", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var yuqueResp ListDocResponse
|
||||
err = json.Unmarshal(respBody, &yuqueResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !yuqueResp.Success {
|
||||
return nil, fmt.Errorf("yuque list docs API failed - URL: %s, UUID: %s, Error: %s", yuqueURL, uuid, yuqueResp.Msg)
|
||||
}
|
||||
|
||||
return &yuqueResp, nil
|
||||
}
|
||||
|
||||
// YuqueExportDoc 导出 Yuque 文档
|
||||
func (c *Client) YuqueExportDoc(ctx context.Context, uuid, docID, kbId string) (*YuqueExportDocResponse, error) {
|
||||
u, err := url.Parse(crawlerServiceHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Path = yuqueExportPath
|
||||
requestURL := u.String()
|
||||
|
||||
bodyMap := map[string]interface{}{
|
||||
"uuid": uuid,
|
||||
"doc_id": docID,
|
||||
"uploader": map[string]interface{}{
|
||||
"type": uploaderTypeHTTP,
|
||||
"http": map[string]interface{}{
|
||||
"url": apiUploaderUrl,
|
||||
},
|
||||
"dir": fmt.Sprintf("/%s", kbId),
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(bodyMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request body: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.logger.Info("YuqueExportDoc", "requestURL:", requestURL, "resp", string(respBody))
|
||||
|
||||
var exportResp YuqueExportDocResponse
|
||||
err = json.Unmarshal(respBody, &exportResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exportResp.Success {
|
||||
return nil, fmt.Errorf("yuque export doc API failed - UUID: %s, DocID: %s, Error: %s", uuid, docID, exportResp.Msg)
|
||||
}
|
||||
|
||||
return &exportResp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user