-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencrucible.go
188 lines (163 loc) · 4.87 KB
/
opencrucible.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package opencrucible
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"github.com/Tulip-Data/pdf"
"github.com/flotzilla/pdf_parser"
"github.com/gabriel-vasile/mimetype"
"github.com/gocaio/metagoffice"
"github.com/gocaio/metagopenoffice"
"github.com/h2non/filetype"
"github.com/lu4p/cat"
doctotext "github.com/vytek/doc-to-text"
)
// Version exposes the current package version.
const Version = "0.0.7"
//Detects
func DetectFileTypeMIME(StreamToDetect []byte) (string, string, error) {
// If no StreamToDetect was given, return an error with a message.
if len(StreamToDetect) == 0 {
return "", "", errors.New("stream to parse is empty")
}
mtype, err := mimetype.DetectReader(bytes.NewReader(StreamToDetect))
return mtype.String(), mtype.Extension(), err
}
func DetectFileType(StreamToDetect []byte) (string, string, error) {
kind, _ := filetype.Match(StreamToDetect)
if kind == filetype.Unknown {
return "", "", errors.New("unknown file type")
} else {
return kind.MIME.Value, kind.Extension, nil
}
}
//Parsers
func TXTParseToString(StreamToParse []byte) (string, error) {
// If no StreamToDetect was given, return an error with a message.
if len(StreamToParse) == 0 {
return "", errors.New("stream to parse is empty")
}
txt, err := cat.FromBytes(StreamToParse)
return txt, err
}
func TXTFileParseToString(FileToParse string) (string, error) {
txt, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := TXTParseToString(txt)
return got, err
}
func RTFParseToString(StreamToParse []byte) (string, error) {
// If no StreamToDetect was given, return an error with a message.
if len(StreamToParse) == 0 {
return "", errors.New("stream to parse is empty")
}
rtf, err := cat.FromBytes(StreamToParse)
return rtf, err
}
func RTFFileParseToString(FileToParse string) (string, error) {
rtf, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := RTFParseToString(rtf)
return got, err
}
func DOCXFileParseToString(FileToParse string) (string, error) {
// extract text from an DOCX file
file, err := os.Open(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
stat, _ := file.Stat()
docx, err := DOCX2Text(file, stat.Size())
return docx, err
}
func ODTParseToString(StreamToParse []byte) (string, error) {
// If no StreamToDetect was given, return an error with a message.
if len(StreamToParse) == 0 {
return "", errors.New("stream to parse is empty")
}
odt, err := cat.FromBytes(StreamToParse)
return odt, err
}
func ODTFileParseToString(FileToParse string) (string, error) {
odt, err := os.ReadFile(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
got, err := ODTParseToString(odt)
return got, err
}
func PDFFileParseToString(FileToParse string) (string, error) {
content, err := readPdf(FileToParse) // Read local pdf file
return content, err
}
func readPdf(path string) (string, error) {
f, r, err := pdf.Open(path)
if err != nil {
return "", err
}
// remember close file
defer f.Close()
var buf bytes.Buffer
b, err := r.GetPlainText()
if err != nil {
return "", err
}
buf.ReadFrom(b)
//Fixed issue with horrible patch
oldBytes := []byte("�")
newBytes := []byte(" ")
replacedBytes := bytes.Replace(buf.Bytes(), oldBytes, newBytes, -1)
return strings.Trim(string(replacedBytes), " "), nil
}
func PPTXFileParseToString(FileToParse string) (string, error) {
// extract text from an PPTX file
file, err := os.Open(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
stat, _ := file.Stat()
pptx, err := PPTX2Text(file, stat.Size())
return pptx, err
}
func DOCFileParseToString(FileToParse string) (string, error) {
file, err := os.Open(FileToParse)
if err != nil {
return "", fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
doc, err := doctotext.DocToText(file, false)
return doc, err
}
//Metadata
// See: https://www.lazy-tech.net/project/pdf_metadata_parsing_golang
func PDFFileMetadata(FileToParse string) (*pdf_parser.PdfInfo, error) {
pdf_parsed, errors := pdf_parser.ParsePdf(FileToParse)
return pdf_parsed, errors
}
// See for return: https://stackoverflow.com/questions/50697914/return-nil-for-a-struct-in-go
func OpenOfficeFileMetadata(FileToParse string) (*metagopenoffice.OpenOfficeXML, error) {
file, err := os.Open(FileToParse)
if err != nil {
return nil, fmt.Errorf("error opening file: %s", err)
}
file.Close()
content, err := metagopenoffice.GetMetada(file)
return &content, err
}
func OfficeFileMetadata(FileToParse string) (*metagoffice.XMLContent, error) {
file, err := os.Open(FileToParse)
if err != nil {
return nil, fmt.Errorf("error opening file: %s", err)
}
file.Close()
content, err := metagoffice.GetContent(file)
return &content, err
}