@@ -11,6 +11,7 @@ import (
11
11
"net/http"
12
12
"regexp"
13
13
"strings"
14
+ "sync"
14
15
)
15
16
16
17
type ScanOptions struct {
@@ -98,77 +99,54 @@ func StartFileScanner(ctx context.Context, c chan string, repo IFileRepository)
98
99
99
100
func singleFileHandler (ctx context.Context , file string , repo IFileRepository ) {
100
101
log .Default ().Printf ("handling file %s" , file )
101
- // insert into database
102
- fileType , fileGroup , fileDescription , err := FileInfer (ctx , file )
103
- if err != nil {
104
- log .Default ().Printf ("error getting file type: %v" , err )
105
- return
106
- }
107
102
_file := NewFile (file )
108
- _file . SetFileType ( fileType , fileGroup , fileDescription )
109
- // 如果为图片文件,对图像进行标注
110
- if fileGroup == "image" {
111
- var labels [] string
112
- labels , err = ImageLabel (ctx , file )
103
+ wg := sync. WaitGroup {}
104
+ wg . Add ( 2 )
105
+ go func () {
106
+ defer wg . Done ()
107
+ result , err := understanding (ctx , file )
113
108
if err != nil {
114
- log .Default ().Printf ("error getting image label : %v" , err )
109
+ log .Default ().Printf ("error getting file type : %v" , err )
115
110
return
116
111
}
117
- if len (labels ) > 0 {
118
- _file .Tags = strings .Join (labels , "," )
119
- }
120
- }
121
- err = repo .CreateOrUpdateFile (ctx , _file )
112
+ _file .SetFileTypeFromUnderstanding (result )
113
+ }()
114
+ go func () {
115
+ defer wg .Done ()
116
+ _file .Checksum = utils .Sha256 (file )
117
+ }()
118
+ // insert into database
119
+ wg .Wait ()
120
+ err := repo .CreateOrUpdateFile (ctx , _file )
122
121
if err != nil {
123
122
log .Default ().Printf ("error inserting file %s: %v" , file , err )
124
123
}
125
124
}
126
125
127
- // FileInfer is a function to determine the file type
128
- func FileInfer (ctx context.Context , file string ) (t string , g string , d string , err error ) {
126
+ // understanding is a function to determine the file type, and tring to label and caption this image
127
+ func understanding (ctx context.Context , file string ) (r understandingResult , err error ) {
129
128
data := bytes .NewBuffer ([]byte (fmt .Sprintf (`{"path": "%s"}` , file )))
130
- request , _ := http .NewRequest (http .MethodPost , "http://localhost :8081/api/v1/file/interfer " , data )
129
+ request , _ := http .NewRequest (http .MethodPost , "http://192.168.163.65 :8081/api/v1/file/understanding " , data )
131
130
request .Header .Set ("Content-Type" , "application/json" )
132
131
resp , err := http .DefaultClient .Do (request )
133
132
if err != nil {
134
133
log .Default ().Printf ("error getting file type: %v" , err )
135
134
return
136
135
}
137
136
defer resp .Body .Close ()
138
- type response struct {
139
- Type string `json:"type"`
140
- Group string `json:"group"`
141
- Description string `json:"description"`
142
- }
137
+
143
138
bts , err := io .ReadAll (resp .Body )
144
139
if err != nil {
145
140
log .Default ().Printf ("error reading response body: %v" , err )
146
141
return
147
142
}
148
- var r response
149
143
json .Unmarshal (bts , & r )
150
- return r . Type , r . Group , r . Description , nil
144
+ return
151
145
}
152
146
153
- func ImageLabel (ctx context.Context , file string ) (labels []string , err error ) {
154
- data := bytes .NewBuffer ([]byte (fmt .Sprintf (`{"path": "%s"}` , file )))
155
- request , _ := http .NewRequest (http .MethodPost , "http://localhost:8081/api/v1/file/image_label" , data )
156
- request .Header .Set ("Content-Type" , "application/json" )
157
- resp , err := http .DefaultClient .Do (request )
158
- if err != nil {
159
- log .Default ().Printf ("error getting file type: %v" , err )
160
- return
161
- }
162
- defer resp .Body .Close ()
163
- type response struct {
164
- Label string `json:"label"`
165
- Confidence string `json:"confidence"`
166
- }
167
- var r []response
168
- bts , err := io .ReadAll (resp .Body )
169
- json .Unmarshal (bts , & r )
170
- for _ , l := range r {
171
- labels = append (labels , l .Label )
172
- }
173
- return
147
+ type understandingResult struct {
148
+ Label string `json:"label"`
149
+ Group string `json:"group"`
150
+ Description string `json:"description"`
151
+ Extension any `json:"extension"`
174
152
}
0 commit comments