@@ -90,6 +90,7 @@ type Response struct {
90
90
}
91
91
92
92
type DiskInfo struct {
93
+ Type string `json:"type"`
93
94
Path string `json:"path"`
94
95
Fstype string `json:"fstype"`
95
96
Total int64 `json:"total"`
@@ -138,40 +139,94 @@ func FetchDiskInfo(url string, header http.Header) ([]DiskInfo, error) {
138
139
return response .Data , nil
139
140
}
140
141
141
- func GetExternalType (filePath string , usbData [] DiskInfo , hddData []DiskInfo ) string {
142
+ func GetExternalType (filePath string , mountedData []DiskInfo ) string {
142
143
fileName := strings .TrimPrefix (strings .TrimSuffix (filePath , "/" ), "/" )
143
144
lastSlashIndex := strings .LastIndex (fileName , "/" )
144
145
if lastSlashIndex != - 1 {
145
146
fileName = fileName [lastSlashIndex + 1 :]
146
147
}
147
148
148
- for _ , usb := range usbData {
149
- if usb .Path == fileName {
150
- return "mountable"
149
+ for _ , mounted := range mountedData {
150
+ if mounted .Path == fileName {
151
+ return mounted . Type
151
152
}
152
153
}
153
154
154
- for _ , hdd := range hddData {
155
- if hdd .Path == fileName {
156
- return "hdd"
157
- }
155
+ return "others"
156
+ }
157
+
158
+ func MountPathIncluster (r * http.Request ) (map [string ]interface {}, error ) {
159
+ externalType := r .URL .Query ().Get ("external_type" )
160
+ var url = ""
161
+ if externalType == "smb" {
162
+ url = "http://" + TerminusdHost + "/command/mount-samba"
163
+ } else {
164
+ return nil , fmt .Errorf ("Unsupported external type: %s" , externalType )
158
165
}
159
166
160
- return "others"
167
+ bodyBytes , err := ioutil .ReadAll (r .Body )
168
+ if err != nil {
169
+ return nil , err
170
+ }
171
+ r .Body = ioutil .NopCloser (bytes .NewBuffer (bodyBytes ))
172
+
173
+ headers := r .Header .Clone ()
174
+ headers .Set ("Content-Type" , "application/json" )
175
+ headers .Set ("X-Signature" , "temp_signature" )
176
+
177
+ client := & http.Client {}
178
+ req , err := http .NewRequest ("POST" , url , bytes .NewReader (bodyBytes ))
179
+ if err != nil {
180
+ return nil , err
181
+ }
182
+ req .Header = headers
183
+
184
+ resp , err := client .Do (req )
185
+ if err != nil {
186
+ return nil , err
187
+ }
188
+ if resp != nil {
189
+ defer resp .Body .Close ()
190
+ }
191
+
192
+ respBody , err := ioutil .ReadAll (resp .Body )
193
+ if err != nil {
194
+ return nil , err
195
+ }
196
+
197
+ var responseMap map [string ]interface {}
198
+ err = json .Unmarshal (respBody , & responseMap )
199
+ if err != nil {
200
+ return nil , err
201
+ }
202
+
203
+ return responseMap , nil
161
204
}
162
205
163
- func UnmountUSBIncluster (r * http.Request , usbPath string ) (map [string ]interface {}, error ) {
164
- url := "http://" + TerminusdHost + "/command/umount-usb-incluster"
206
+ func UnmountPathIncluster (r * http.Request , path string ) (map [string ]interface {}, error ) {
207
+ externalType := r .URL .Query ().Get ("external_type" )
208
+ var url = ""
209
+ if externalType == "usb" {
210
+ url = "http://" + TerminusdHost + "/command/umount-usb-incluster"
211
+ } else if externalType == "smb" {
212
+ url = "http://" + TerminusdHost + "/command/umount-samba-incluster"
213
+ } else {
214
+ return nil , fmt .Errorf ("Unsupported external type: %s" , externalType )
215
+ }
216
+ fmt .Println ("path:" , path )
217
+ fmt .Println ("externalTYpe:" , externalType )
218
+ fmt .Println ("url:" , url )
165
219
166
220
headers := r .Header .Clone ()
167
221
headers .Set ("Content-Type" , "application/json" )
168
222
headers .Set ("X-Signature" , "temp_signature" )
169
223
170
- mountPath := strings .TrimPrefix (strings .TrimSuffix (usbPath , "/" ), "/" )
224
+ mountPath := strings .TrimPrefix (strings .TrimSuffix (path , "/" ), "/" )
171
225
lastSlashIndex := strings .LastIndex (mountPath , "/" )
172
226
if lastSlashIndex != - 1 {
173
227
mountPath = mountPath [lastSlashIndex + 1 :]
174
228
}
229
+ fmt .Println ("mountPath:" , mountPath )
175
230
176
231
bodyData := map [string ]string {
177
232
"path" : mountPath ,
@@ -203,6 +258,7 @@ func UnmountUSBIncluster(r *http.Request, usbPath string) (map[string]interface{
203
258
if err != nil {
204
259
return nil , err
205
260
}
261
+ fmt .Println ("responseMap:" , responseMap )
206
262
207
263
return responseMap , nil
208
264
}
@@ -237,7 +293,7 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
237
293
return file , err
238
294
}
239
295
240
- func NewFileInfoWithDiskInfo (opts FileOptions , usbData , hddData []DiskInfo ) (* FileInfo , error ) {
296
+ func NewFileInfoWithDiskInfo (opts FileOptions , mountedData []DiskInfo ) (* FileInfo , error ) {
241
297
if ! opts .Checker .Check (opts .Path ) {
242
298
return nil , os .ErrPermission
243
299
}
@@ -249,7 +305,7 @@ func NewFileInfoWithDiskInfo(opts FileOptions, usbData, hddData []DiskInfo) (*Fi
249
305
250
306
if opts .Expand {
251
307
if file .IsDir {
252
- if err := file .readListingWithDiskInfo (opts .Checker , opts .ReadHeader , usbData , hddData ); err != nil { //nolint:govet
308
+ if err := file .readListingWithDiskInfo (opts .Checker , opts .ReadHeader , mountedData ); err != nil { //nolint:govet
253
309
return nil , err
254
310
}
255
311
return file , nil
@@ -628,7 +684,7 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
628
684
return nil
629
685
}
630
686
631
- func (i * FileInfo ) readListingWithDiskInfo (checker rules.Checker , readHeader bool , usbData , hddData []DiskInfo ) error {
687
+ func (i * FileInfo ) readListingWithDiskInfo (checker rules.Checker , readHeader bool , mountedData []DiskInfo ) error {
632
688
afs := & afero.Afero {Fs : i .Fs }
633
689
dir , err := afs .ReadDir (i .Path )
634
690
if err != nil {
@@ -677,7 +733,7 @@ func (i *FileInfo) readListingWithDiskInfo(checker rules.Checker, readHeader boo
677
733
678
734
if file .IsDir {
679
735
if CheckPath (file .Path , ExternalPrefix , "/" ) {
680
- file .ExternalType = GetExternalType (file .Path , usbData , hddData )
736
+ file .ExternalType = GetExternalType (file .Path , mountedData )
681
737
}
682
738
// err := file.readListing(checker, readHeader)
683
739
// if err != nil {
0 commit comments