MCPcopy
hub / github.com/ChineseSubFinder/ChineseSubFinder / GetFileName

Function GetFileName

pkg/util.go:159–178  ·  view source on GitHub ↗

GetFileName 获取下载文件的文件名

(l *logrus.Logger, resp *http.Response)

Source from the content-addressed store, hash-verified

157
158// GetFileName 获取下载文件的文件名
159func GetFileName(l *logrus.Logger, resp *http.Response) string {
160 contentDisposition := resp.Header.Get("Content-Disposition")
161 if len(contentDisposition) == 0 {
162 m := regexp.MustCompile(`^(.*/)?(?:$|(.+?)(?:(\.[^.]*$)|$))`).FindStringSubmatch(resp.Request.URL.String())
163
164 if m == nil || len(m) < 4 {
165 l.Warningln("GetFileName.regexp.MustCompile.FindStringSubmatch", resp.Request.URL.String())
166 return ""
167 }
168
169 return m[2] + m[3]
170 }
171 re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
172 matched := re.FindStringSubmatch(contentDisposition)
173 if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
174 l.Errorln("GetFileName.Content-Disposition", contentDisposition)
175 return ""
176 }
177 return matched[1]
178}
179
180// AddBaseUrl 判断传入的 url 是否需要拼接 baseUrl
181func AddBaseUrl(baseUrl, url string) string {

Callers 1

DownFileFunction · 0.85

Calls 2

GetMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected