Skip to content

Commit 8e4cc4a

Browse files
committed
external smb support
1 parent 2a3d272 commit 8e4cc4a

File tree

5 files changed

+108
-50
lines changed

5 files changed

+108
-50
lines changed

.github/workflows/update-server.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- "main"
99
- "dev_wrx"
10-
- "nats"
10+
- "smb"
1111
tags:
1212
- 'v*'
1313

@@ -47,7 +47,7 @@ jobs:
4747
uses: "WyriHaximus/github-action-get-previous-tag@v1"
4848
id: get-latest-tag
4949
with:
50-
fallback: nats-test
50+
fallback: smb-test
5151

5252
# - name: Build
5353
# run: cd packages/backend/;go mod tidy;export CGO_ENABLED=0;make build-backend;ls

packages/backend/files/file.go

+72-16
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type Response struct {
9090
}
9191

9292
type DiskInfo struct {
93+
Type string `json:"type"`
9394
Path string `json:"path"`
9495
Fstype string `json:"fstype"`
9596
Total int64 `json:"total"`
@@ -138,40 +139,94 @@ func FetchDiskInfo(url string, header http.Header) ([]DiskInfo, error) {
138139
return response.Data, nil
139140
}
140141

141-
func GetExternalType(filePath string, usbData []DiskInfo, hddData []DiskInfo) string {
142+
func GetExternalType(filePath string, mountedData []DiskInfo) string {
142143
fileName := strings.TrimPrefix(strings.TrimSuffix(filePath, "/"), "/")
143144
lastSlashIndex := strings.LastIndex(fileName, "/")
144145
if lastSlashIndex != -1 {
145146
fileName = fileName[lastSlashIndex+1:]
146147
}
147148

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
151152
}
152153
}
153154

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)
158165
}
159166

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
161204
}
162205

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)
165219

166220
headers := r.Header.Clone()
167221
headers.Set("Content-Type", "application/json")
168222
headers.Set("X-Signature", "temp_signature")
169223

170-
mountPath := strings.TrimPrefix(strings.TrimSuffix(usbPath, "/"), "/")
224+
mountPath := strings.TrimPrefix(strings.TrimSuffix(path, "/"), "/")
171225
lastSlashIndex := strings.LastIndex(mountPath, "/")
172226
if lastSlashIndex != -1 {
173227
mountPath = mountPath[lastSlashIndex+1:]
174228
}
229+
fmt.Println("mountPath:", mountPath)
175230

176231
bodyData := map[string]string{
177232
"path": mountPath,
@@ -203,6 +258,7 @@ func UnmountUSBIncluster(r *http.Request, usbPath string) (map[string]interface{
203258
if err != nil {
204259
return nil, err
205260
}
261+
fmt.Println("responseMap:", responseMap)
206262

207263
return responseMap, nil
208264
}
@@ -237,7 +293,7 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
237293
return file, err
238294
}
239295

240-
func NewFileInfoWithDiskInfo(opts FileOptions, usbData, hddData []DiskInfo) (*FileInfo, error) {
296+
func NewFileInfoWithDiskInfo(opts FileOptions, mountedData []DiskInfo) (*FileInfo, error) {
241297
if !opts.Checker.Check(opts.Path) {
242298
return nil, os.ErrPermission
243299
}
@@ -249,7 +305,7 @@ func NewFileInfoWithDiskInfo(opts FileOptions, usbData, hddData []DiskInfo) (*Fi
249305

250306
if opts.Expand {
251307
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
253309
return nil, err
254310
}
255311
return file, nil
@@ -628,7 +684,7 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
628684
return nil
629685
}
630686

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 {
632688
afs := &afero.Afero{Fs: i.Fs}
633689
dir, err := afs.ReadDir(i.Path)
634690
if err != nil {
@@ -677,7 +733,7 @@ func (i *FileInfo) readListingWithDiskInfo(checker rules.Checker, readHeader boo
677733

678734
if file.IsDir {
679735
if CheckPath(file.Path, ExternalPrefix, "/") {
680-
file.ExternalType = GetExternalType(file.Path, usbData, hddData)
736+
file.ExternalType = GetExternalType(file.Path, mountedData)
681737
}
682738
// err := file.readListing(checker, readHeader)
683739
// if err != nil {

packages/backend/http/http.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func NewHandler(
6666
api.PathPrefix("/resources").Handler(monkey(resourcePostHandler(fileCache), "/api/resources")).Methods("POST")
6767
api.PathPrefix("/resources").Handler(monkey(resourcePutHandler, "/api/resources")).Methods("PUT")
6868
api.PathPrefix("/resources").Handler(monkey(resourcePatchHandler(fileCache), "/api/resources")).Methods("PATCH")
69+
api.PathPrefix("/mount").Handler(monkey(resourceMountHandler(fileCache), "/api/mount")).Methods("POST")
6970
api.PathPrefix("/unmount").Handler(monkey(resourceUnmountHandler(fileCache), "/api/unmount")).Methods("DELETE")
7071
// Because /api/resources/AppData is proxied under current arch, new api must be of a different prefix,
7172
// and try to access /api/resources/AppData in the handle func.

packages/backend/http/resource.go

+28-27
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,9 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
160160
xBflUser := r.Header.Get("X-Bfl-User")
161161
fmt.Println("X-Bfl-User: ", xBflUser)
162162

163-
var usbData []files.DiskInfo = nil
164-
var hddData []files.DiskInfo = nil
163+
var mountedData []files.DiskInfo = nil
165164
if files.TerminusdHost != "" {
166-
urls := []string{
167-
"http://" + files.TerminusdHost + "/system/mounted-usb-incluster",
168-
"http://" + files.TerminusdHost + "/system/mounted-hdd-incluster",
169-
}
165+
url := "http://" + files.TerminusdHost + "/system/mounted-path-incluster"
170166

171167
headers := r.Header.Clone()
172168
headers.Set("Content-Type", "application/json")
@@ -175,27 +171,16 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
175171
// "X-Signature": "temp_signature",
176172
//}
177173

178-
for _, url := range urls {
179-
data, err := files.FetchDiskInfo(url, headers)
180-
if err != nil {
181-
log.Printf("Failed to fetch data from %s: %v", url, err)
182-
continue
183-
}
184-
185-
if url == urls[0] {
186-
usbData = data
187-
} else if url == urls[1] {
188-
hddData = data
189-
}
174+
mountedData, err = files.FetchDiskInfo(url, headers)
175+
if err != nil {
176+
log.Printf("Failed to fetch data from %s: %v", url, err)
190177
}
191178

192-
fmt.Println("USB Data:", usbData)
193-
fmt.Println("HDD Data:", hddData)
179+
fmt.Println("Mounted Data:", mountedData)
194180
}
195181

196182
var file *files.FileInfo
197-
//var err error
198-
if usbData != nil || hddData != nil {
183+
if mountedData != nil {
199184
file, err = files.NewFileInfoWithDiskInfo(files.FileOptions{
200185
Fs: d.user.Fs,
201186
Path: r.URL.Path,
@@ -204,7 +189,7 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
204189
ReadHeader: d.server.TypeDetectionByHeader,
205190
Checker: d,
206191
Content: true,
207-
}, usbData, hddData)
192+
}, mountedData)
208193
} else {
209194
file, err = files.NewFileInfo(files.FileOptions{
210195
Fs: d.user.Fs,
@@ -254,13 +239,13 @@ var resourceGetHandler = withUser(func(w http.ResponseWriter, r *http.Request, d
254239
// file.Size = file.Listing.Size
255240
// recursiveSize(file)
256241
if files.CheckPath(file.Path, files.ExternalPrefix, "/") {
257-
file.ExternalType = files.GetExternalType(file.Path, usbData, hddData)
242+
file.ExternalType = files.GetExternalType(file.Path, mountedData)
258243
}
259244
file.Listing.Sorting = d.user.Sorting
260245
file.Listing.ApplySort()
261246
if stream == 1 {
262247
//return streamJSON(w, r, file)
263-
streamListingItems(w, r, file.Listing, d, usbData, hddData)
248+
streamListingItems(w, r, file.Listing, d, mountedData)
264249
elapsed := time.Since(start)
265250
fmt.Printf("Function resourceGetHandler execution time: %v\n", elapsed)
266251
return 0, nil
@@ -387,9 +372,25 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
387372
})
388373
}
389374

375+
func resourceMountHandler(fileCache FileCache) handleFunc {
376+
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
377+
if !d.user.Perm.Create {
378+
return http.StatusForbidden, nil
379+
}
380+
381+
respJson, err := files.MountPathIncluster(r)
382+
if err != nil {
383+
return errToStatus(err), err
384+
}
385+
386+
return renderJSON(w, r, respJson)
387+
//return http.StatusOK, nil
388+
})
389+
}
390+
390391
func resourceUnmountHandler(fileCache FileCache) handleFunc {
391392
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
392-
if r.URL.Path == "/" || !d.user.Perm.Delete {
393+
if !d.user.Perm.Delete {
393394
return http.StatusForbidden, nil
394395
}
395396

@@ -415,7 +416,7 @@ func resourceUnmountHandler(fileCache FileCache) handleFunc {
415416
// return d.user.Fs.RemoveAll(r.URL.Path)
416417
//}, "delete", r.URL.Path, "", d.user)
417418

418-
respJson, err := files.UnmountUSBIncluster(r, file.Path)
419+
respJson, err := files.UnmountPathIncluster(r, file.Path)
419420
if err != nil {
420421
return errToStatus(err), err
421422
}

packages/backend/http/utils.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func renderJSON(w http.ResponseWriter, _ *http.Request, data interface{}) (int,
6363
// }
6464
//}
6565

66-
func generateListingData(listing *files.Listing, stopChan <-chan struct{}, dataChan chan<- string, d *data, usbData, hddData []files.DiskInfo) {
66+
func generateListingData(listing *files.Listing, stopChan <-chan struct{}, dataChan chan<- string, d *data, mountedData []files.DiskInfo) {
6767
defer close(dataChan)
6868

6969
var A []*files.FileInfo
@@ -79,7 +79,7 @@ func generateListingData(listing *files.Listing, stopChan <-chan struct{}, dataC
7979
if firstItem.IsDir {
8080
var file *files.FileInfo
8181
var err error
82-
if usbData != nil || hddData != nil {
82+
if mountedData != nil {
8383
file, err = files.NewFileInfoWithDiskInfo(files.FileOptions{
8484
Fs: d.user.Fs,
8585
Path: firstItem.Path, //r.URL.Path,
@@ -88,7 +88,7 @@ func generateListingData(listing *files.Listing, stopChan <-chan struct{}, dataC
8888
ReadHeader: d.server.TypeDetectionByHeader,
8989
Checker: d,
9090
Content: true,
91-
}, usbData, hddData)
91+
}, mountedData)
9292
} else {
9393
file, err = files.NewFileInfo(files.FileOptions{
9494
Fs: d.user.Fs,
@@ -132,15 +132,15 @@ func formatSSEvent(data interface{}) string {
132132
return fmt.Sprintf("data: %s\n\n", jsonData)
133133
}
134134

135-
func streamListingItems(w http.ResponseWriter, r *http.Request, listing *files.Listing, d *data, usbData, hddData []files.DiskInfo) {
135+
func streamListingItems(w http.ResponseWriter, r *http.Request, listing *files.Listing, d *data, mountedData []files.DiskInfo) {
136136
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
137137
w.Header().Set("Cache-Control", "no-cache")
138138
w.Header().Set("Connection", "keep-alive")
139139

140140
stopChan := make(chan struct{})
141141
dataChan := make(chan string)
142142

143-
go generateListingData(listing, stopChan, dataChan, d, usbData, hddData)
143+
go generateListingData(listing, stopChan, dataChan, d, mountedData)
144144

145145
flusher, ok := w.(http.Flusher)
146146
if !ok {

0 commit comments

Comments
 (0)