-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemanager.processing.formatconverter.go
125 lines (104 loc) · 3.13 KB
/
filemanager.processing.formatconverter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package filemanager
import (
"bytes"
"encoding/csv"
"fmt"
"strings"
"time"
"github.com/xuri/excelize/v2"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
type FormatConverterPlugin struct{}
func (p *FormatConverterPlugin) Process(files []*ManagedFile, fileProcess *FileProcess) ([]*ManagedFile, error) {
var processedFiles []*ManagedFile
for _, file := range files {
var convertedContent []byte
var err error
status := ProcessingStatus{
ProcessID: fileProcess.ID,
TimeStamp: int(time.Now().UnixNano() / int64(time.Millisecond)),
ProcessorName: "FormatConverter",
StatusDescription: fmt.Sprintf("Converting file format: %s", file.FileName),
}
fileProcess.AddProcessingUpdate(status)
switch strings.ToLower(file.MimeType) {
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
convertedContent, err = convertDocxToText(file.Content)
if err != nil {
convertedContent, err = convertDocxToMarkdown(file.Content)
}
case "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
convertedContent, err = convertExcelToCSV(file.Content)
default:
processedFiles = append(processedFiles, file)
continue
}
if err != nil {
return nil, fmt.Errorf("failed to convert file format: %v", err)
}
convertedFile := &ManagedFile{
FileName: file.FileName,
Content: convertedContent,
MimeType: "text/plain",
FileSize: int64(len(convertedContent)),
MetaData: file.MetaData,
ProcessingErrors: []string{},
}
processedFiles = append(processedFiles, convertedFile)
}
return processedFiles, nil
}
func convertDocxToText(content []byte) ([]byte, error) {
// Convert DOCX to plain text using a library or custom implementation
// Here's a placeholder implementation that assumes the content is already in plain text format
return content, nil
}
func convertDocxToMarkdown(content []byte) ([]byte, error) {
// Convert DOCX to Markdown using the goldmark library
var buf bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
html.WithXHTML(),
),
)
if err := md.Convert(content, &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func convertExcelToCSV(content []byte) ([]byte, error) {
// Load the Excel file
xlsx, err := excelize.OpenReader(bytes.NewReader(content))
if err != nil {
return nil, err
}
// Get the first sheet name
sheetName := xlsx.GetSheetName(1)
// Get all the rows in the sheet
rows, err := xlsx.GetRows(sheetName)
if err != nil {
return nil, err
}
// Create a new CSV writer
var csvBuf bytes.Buffer
csvWriter := csv.NewWriter(&csvBuf)
// Write the rows to the CSV writer
for _, row := range rows {
if err := csvWriter.Write(row); err != nil {
return nil, err
}
}
csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
return nil, err
}
return csvBuf.Bytes(), nil
}