GetFileName 获取下载文件的文件名
(l *logrus.Logger, resp *http.Response)
| 157 | |
| 158 | // GetFileName 获取下载文件的文件名 |
| 159 | func 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 |
| 181 | func AddBaseUrl(baseUrl, url string) string { |