parseDataURL tries to parse the specified string as a data URL with the format: data:[ ][;base64], and if successfull returns true and updates the specified pointer with the parsed fields.
(url string, du *dataURL)
| 1188 | // data:[<mediatype>][;base64],<data> |
| 1189 | // and if successfull returns true and updates the specified pointer with the parsed fields. |
| 1190 | func parseDataURL(url string, du *dataURL) error { |
| 1191 | |
| 1192 | // Check prefix |
| 1193 | if !isDataURL(url) { |
| 1194 | return fmt.Errorf("specified string is not a data URL") |
| 1195 | } |
| 1196 | |
| 1197 | // Separate header from data |
| 1198 | body := url[len(dataURLprefix):] |
| 1199 | parts := strings.Split(body, ",") |
| 1200 | if len(parts) != 2 { |
| 1201 | return fmt.Errorf("data URI contains more than one ','") |
| 1202 | } |
| 1203 | du.Data = parts[1] |
| 1204 | |
| 1205 | // Separate media type from optional encoding |
| 1206 | res := strings.Split(parts[0], ";") |
| 1207 | du.MediaType = res[0] |
| 1208 | if len(res) < 2 { |
| 1209 | return nil |
| 1210 | } |
| 1211 | if len(res) >= 2 { |
| 1212 | du.Encoding = res[1] |
| 1213 | } |
| 1214 | return nil |
| 1215 | } |
no test coverage detected