/
extract_content.go
96 lines (83 loc) · 2.75 KB
/
extract_content.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/shared/mlog"
)
var ExtractContentCmd = &cobra.Command{
Use: "extract-documents-content",
Short: "Extracts the documents content",
Long: "Extracts the documents content and stores it in the database for document search",
Example: "extract-documents-content --from=12345",
RunE: extractContentCmdF,
}
var ignoredFiles map[string]bool
func init() {
ignoredFiles = map[string]bool{
"png": true, "jpg": true, "jpeg": true, "gif": true, "wmv": true,
"mpg": true, "mpeg": true, "mp3": true, "mp4": true, "ogg": true,
"ogv": true, "mov": true, "apk": true, "svg": true, "webm": true,
"mkv": true,
}
ExtractContentCmd.Flags().Int64("from", 0, "The timestamp of the earliest file to extract, expressed in seconds since the unix epoch.")
ExtractContentCmd.Flags().Int64("to", model.GetMillis()/1000, "The timestamp of the latest file to extract, expressed in seconds since the unix epoch.")
RootCmd.AddCommand(ExtractContentCmd)
}
func extractContentCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command)
if err != nil {
return err
}
defer a.Srv().Shutdown()
if !*a.Config().FileSettings.ExtractContent {
return errors.New("ERROR: Document extraction is not enabled")
}
startTime, err := command.Flags().GetInt64("from")
if err != nil {
return errors.New("\"from\" flag error")
}
if startTime < 0 {
return errors.New("\"from\" must be a positive integer")
}
endTime, err := command.Flags().GetInt64("to")
if err != nil {
return errors.New("\"to\" flag error")
}
if endTime < startTime {
return errors.New("\"to\" must be greater than from")
}
since := startTime * 1000
for {
opts := model.GetFileInfosOptions{
Since: since,
SortBy: model.FILEINFO_SORT_BY_CREATED,
IncludeDeleted: false,
}
fileInfos, err := a.Srv().Store.FileInfo().GetWithOptions(0, 1000, &opts)
if err != nil {
return fmt.Errorf("ERROR: Document extraction failed %v", err.Error())
}
if len(fileInfos) == 0 {
break
}
for _, fileInfo := range fileInfos {
if !ignoredFiles[fileInfo.Extension] {
fmt.Println("extracting file", fileInfo.Name, fileInfo.Path)
err = a.ExtractContentFromFileInfo(fileInfo)
if err != nil {
mlog.Error("Failed to extract file content", mlog.Err(err), mlog.String("fileInfoId", fileInfo.Id))
}
}
}
lastFileInfo := fileInfos[len(fileInfos)-1]
if lastFileInfo.CreateAt > endTime*1000 {
break
}
since = lastFileInfo.CreateAt + 1
}
return nil
}