| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | type selectedHostFile struct { |
| 16 | file *os.File |
| 17 | name string |
| 18 | size int64 |
| 19 | } |
| 20 | |
| 21 | func openSelectedHostFiles(paths []string) ([]selectedHostFile, error) { |
| 22 | if len(paths) == 0 || len(paths) > 100 { |
| 23 | return nil, errors.New("请选择 1 至 100 个文件") |
| 24 | } |
| 25 | files := make([]selectedHostFile, 0, len(paths)) |
| 26 | closeFiles := func() { |
| 27 | for _, file := range files { |
| 28 | file.file.Close() |
| 29 | } |
| 30 | } |
| 31 | for _, path := range paths { |
| 32 | if !filepath.IsAbs(path) || !browserFileName(filepath.Base(path)) { |
| 33 | closeFiles() |
| 34 | return nil, errors.New("所选文件路径或名称无效") |
| 35 | } |
| 36 | before, err := os.Lstat(path) |
| 37 | if err != nil || !before.Mode().IsRegular() { |
| 38 | closeFiles() |
| 39 | return nil, errors.New("请选择本机普通文件;文件夹、链接或已删除的文件无法上传") |
| 40 | } |
| 41 | file, err := os.Open(path) |
| 42 | if err != nil { |
| 43 | closeFiles() |
| 44 | return nil, errors.New("无法读取所选文件,请检查文件权限") |
| 45 | } |
| 46 | after, err := file.Stat() |
| 47 | if err != nil || !after.Mode().IsRegular() || !os.SameFile(before, after) || after.Size() > browserFileLimit { |
| 48 | file.Close() |
| 49 | closeFiles() |
| 50 | return nil, errors.New("所选文件已变化或超过 2 GB,请重新选择") |
| 51 | } |
| 52 | files = append(files, selectedHostFile{file: file, name: filepath.Base(path), size: after.Size()}) |
| 53 | } |
| 54 | return files, nil |
| 55 | } |
| 56 | |
| 57 | func streamSelectedHostFiles(client *http.Client, origin, target, id string, files []selectedHostFile) ([]string, error) { |
| 58 | paths := []string{} |
| 59 | for _, file := range files { |
| 60 | path := origin + "/api/input/files/" + target + "/" + id + "?" + url.Values{"name": {file.name}}.Encode() |
| 61 | request, err := http.NewRequest("POST", path, io.NewSectionReader(file.file, 0, file.size)) |
| 62 | if err != nil { |
| 63 | return nil, errors.New("文件传递请求无效") |
| 64 | } |
| 65 | request.Header.Set("Origin", origin) |
| 66 | request.Header.Set("Content-Type", "application/octet-stream") |
| 67 | request.ContentLength = file.size |
| 68 | if file.size == 0 { |
| 69 | request.Body = http.NoBody |
| 70 | } |
| 71 | response, err := client.Do(request) |
| 72 | if err != nil { |
| 73 | return nil, errors.New("文件传递中断,请重新选择文件后重试") |
| 74 | } |
| 75 | data, readErr := io.ReadAll(io.LimitReader(response.Body, 4097)) |
| 76 | response.Body.Close() |
| 77 | if response.StatusCode != http.StatusOK { |
| 78 | return nil, fmt.Errorf("文件传递失败(%d),请重新发起上传", response.StatusCode) |
| 79 | } |
| 80 | var result struct { |
| 81 | OK bool `json:"ok"` |
| 82 | Path string `json:"path"` |
| 83 | } |
| 84 | if readErr != nil || len(data) > 4096 || json.Unmarshal(data, &result) != nil || !result.OK { |
| 85 | return nil, errors.New("文件传递未返回有效结果,请重试") |
| 86 | } |
| 87 | if result.Path != "" { |
| 88 | paths = append(paths, result.Path) |
| 89 | } |
| 90 | } |
| 91 | return paths, nil |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | func UploadHostFiles(configPath, target, id string, paths []string) (map[string]any, error) { |
| 97 | return UploadHostFilesForConnection(configPath, "", target, id, paths) |
| 98 | } |
| 99 | |
| 100 | func UploadHostFilesForConnection(configPath, connectionID, target, id string, paths []string) (map[string]any, error) { |
| 101 | if (target != "browser" && target != "desktop" && target != "terminal") || !validFileTargetID(target, id) { |
| 102 | return nil, errors.New("文件上传目标无效") |
| 103 | } |
| 104 | files, err := openSelectedHostFiles(paths) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | defer func() { |
| 109 | for _, file := range files { |
| 110 | file.file.Close() |
| 111 | } |
| 112 | }() |
| 113 | client, err := connectController(filepath.Dir(configPath)) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | defer client.close() |
| 118 | entry, _ := url.Parse(client.launch.URL) |
| 119 | status, err := client.call("POST", "/api/session", map[string]string{"token": entry.Fragment}, nil) |
| 120 | if err != nil || status != http.StatusOK { |
| 121 | return nil, errors.New("无法连接文件上传会话") |
| 122 | } |
| 123 | if _, err = client.bindConnection(connectionID); err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | client.client.Timeout = 30 * time.Minute |
| 127 | pathsResult, err := streamSelectedHostFiles(client.client, client.origin, target, id, files) |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | return map[string]any{"ok": true, "count": len(files), "paths": pathsResult}, nil |
| 132 | } |