summaryrefslogtreecommitdiff
path: root/vendor/github.com/playwright-community/playwright-go/input_files_helper.go
blob: 3318cb5b8c5406fb585248107e2e94a50b4a77a5 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package playwright

import (
	"encoding/base64"
	"errors"
	"fmt"
	"os"
	"path/filepath"
)

const fileSizeLimitInBytes = 50 * 1024 * 1024

var ErrInputFilesSizeExceeded = errors.New("Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead.")

type inputFiles struct {
	Selector        *string             `json:"selector,omitempty"`
	Streams         []*channel          `json:"streams,omitempty"` // writableStream
	LocalPaths      []string            `json:"localPaths,omitempty"`
	Payloads        []map[string]string `json:"payloads,omitempty"`
	LocalDirectory  *string             `json:"localDirectory,omitempty"`
	DirectoryStream *channel            `json:"directoryStream,omitempty"`
}

type fileItem struct {
	LastModifiedMs *int64 `json:"lastModifiedMs,omitempty"`
	Name           string `json:"name"`
}

// convertInputFiles converts files to proper format for Playwright
//
//   - files should be one of: string, []string, InputFile, []InputFile,
//     string: local file path
func convertInputFiles(files interface{}, context *browserContextImpl) (*inputFiles, error) {
	var (
		converted = &inputFiles{}
		paths     []string
	)
	switch items := files.(type) {
	case InputFile:
		if sizeOfInputFiles([]InputFile{items}) > fileSizeLimitInBytes {
			return nil, ErrInputFilesSizeExceeded
		}
		converted.Payloads = normalizeFilePayloads([]InputFile{items})
	case []InputFile:
		if sizeOfInputFiles(items) > fileSizeLimitInBytes {
			return nil, ErrInputFilesSizeExceeded
		}
		converted.Payloads = normalizeFilePayloads(items)
	case string: // local file path
		paths = []string{items}
	case []string:
		paths = items
	default:
		return nil, errors.New("files should be one of: string, []string, InputFile, []InputFile")
	}

	localPaths, localDir, err := resolvePathsAndDirectoryForInputFiles(paths)
	if err != nil {
		return nil, err
	}

	if !context.connection.isRemote {
		converted.LocalPaths = localPaths
		converted.LocalDirectory = localDir
		return converted, nil
	}

	// remote
	params := map[string]interface{}{
		"items": []fileItem{},
	}
	allFiles := localPaths
	if localDir != nil {
		params["rootDirName"] = filepath.Base(*localDir)
		allFiles, err = listFiles(*localDir)
		if err != nil {
			return nil, err
		}
	}
	for _, file := range allFiles {
		lastModifiedMs, err := getFileLastModifiedMs(file)
		if err != nil {
			return nil, fmt.Errorf("failed to get last modified time of %s: %w", file, err)
		}
		filename := filepath.Base(file)
		if localDir != nil {
			var err error
			filename, err = filepath.Rel(*localDir, file)
			if err != nil {
				return nil, err
			}
		}
		params["items"] = append(params["items"].([]fileItem), fileItem{
			LastModifiedMs: &lastModifiedMs,
			Name:           filename,
		})
	}

	ret, err := context.connection.WrapAPICall(func() (interface{}, error) {
		return context.channel.SendReturnAsDict("createTempFiles", params)
	}, true)
	if err != nil {
		return nil, err
	}
	result := ret.(map[string]interface{})

	streams := make([]*channel, 0)
	items := result["writableStreams"].([]interface{})
	for i := 0; i < len(allFiles); i++ {
		stream := fromChannel(items[i]).(*writableStream)
		if err := stream.Copy(allFiles[i]); err != nil {
			return nil, err
		}
		streams = append(streams, stream.channel)
	}

	if result["rootDir"] != nil {
		converted.DirectoryStream = result["rootDir"].(*channel)
	} else {
		converted.Streams = streams
	}

	return converted, nil
}

func getFileLastModifiedMs(path string) (int64, error) {
	info, err := os.Stat(path)
	if err != nil {
		return 0, err
	}
	if info.IsDir() {
		return 0, fmt.Errorf("%s is a directory", path)
	}
	return info.ModTime().UnixMilli(), nil
}

func sizeOfInputFiles(files []InputFile) int {
	size := 0
	for _, file := range files {
		size += len(file.Buffer)
	}
	return size
}

func normalizeFilePayloads(files []InputFile) []map[string]string {
	out := make([]map[string]string, 0)
	for _, file := range files {
		out = append(out, map[string]string{
			"name":     file.Name,
			"mimeType": file.MimeType,
			"buffer":   base64.StdEncoding.EncodeToString(file.Buffer),
		})
	}
	return out
}

func resolvePathsAndDirectoryForInputFiles(items []string) (localPaths []string, localDirectory *string, e error) {
	for _, item := range items {
		abspath, err := filepath.Abs(item)
		if err != nil {
			e = err
			return
		}
		// if the path is a directory
		if info, err := os.Stat(abspath); err == nil {
			if info.IsDir() {
				if localDirectory != nil {
					e = errors.New("Multiple directories are not supported")
					return
				}
				localDirectory = &abspath
			} else {
				if localPaths == nil {
					localPaths = []string{abspath}
				} else {
					localPaths = append(localPaths, abspath)
				}
			}
		} else {
			e = err
			return
		}
	}
	if localPaths != nil && localDirectory != nil {
		e = errors.New("File paths must be all files or a single directory")
	}
	return
}

func listFiles(dir string) ([]string, error) {
	var files []string
	err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() {
			files = append(files, path)
		}
		return nil
	})
	return files, err
}