@@ -229,7 +229,7 @@ func (f *Fetcher) SlurpImages(src, dir string, n []nodes.Node, images map[string
229229 for _ , imageNode := range imageNodes {
230230 go func (imageNode * nodes.ImageNode ) {
231231 url := imageNode .Src
232- file , err := f .slurpBytes (src , dir , url )
232+ file , err := f .slurpBytes (src , dir , url , imageNode . Bytes )
233233 if err == nil {
234234 imageNode .Src = filepath .Join (util .ImgDirname , file )
235235 }
@@ -251,40 +251,52 @@ func (f *Fetcher) SlurpImages(src, dir string, n []nodes.Node, images map[string
251251 return nil
252252}
253253
254- func (f * Fetcher ) slurpBytes (codelabSrc , dir , imgURL string ) (string , error ) {
255- // images can be local in Markdown cases or remote.
254+ func (f * Fetcher ) slurpBytes (codelabSrc , dir , imgURL string , imgBytes [] byte ) (string , error ) {
255+ // images can be data URLs, local in Markdown cases or remote.
256256 // Only proceed a simple copy on local reference.
257257 var b []byte
258258 var ext string
259- u , err := url .Parse (imgURL )
260- if err != nil {
261- return "" , err
262- }
263-
264- // If the codelab source is being downloaded from the network, then we should interpret
265- // the image URL in the same way.
266- srcUrl , err := url .Parse (codelabSrc )
267- if err == nil && srcUrl .Host != "" {
268- u = srcUrl .ResolveReference (u )
269- }
259+ var err error
270260
271- if u .Host == "" {
272- if imgURL , err = restrictPathToParent (imgURL , filepath .Dir (codelabSrc )); err != nil {
273- return "" , err
261+ if len (imgBytes ) > 0 {
262+ // Slurp bytes from image URL data.
263+ b = imgBytes
264+ if ext , err = imgExtFromBytes (b ); err != nil {
265+ return "" , fmt .Errorf ("Error reading image type: %v" , err )
274266 }
275- if b , err = ioutil .ReadFile (imgURL ); err != nil {
267+ } else {
268+ // Slurp bytes from local or remote URL.
269+ u , err := url .Parse (imgURL )
270+ if err != nil {
276271 return "" , err
277272 }
278- ext = filepath .Ext (imgURL )
279- } else {
280- if b , err = f .slurpRemoteBytes (u .String (), 5 ); err != nil {
281- return "" , fmt .Errorf ("Error downloading image at %s: %v" , u .String (), err )
273+
274+ // If the codelab source is being downloaded from the network, then we should interpret
275+ // the image URL in the same way.
276+ srcURL , err := url .Parse (codelabSrc )
277+ if err == nil && srcURL .Host != "" {
278+ u = srcURL .ResolveReference (u )
282279 }
283- if ext , err = imgExtFromBytes (b ); err != nil {
284- return "" , fmt .Errorf ("Error reading image type at %s: %v" , u .String (), err )
280+
281+ if u .Host == "" {
282+ if imgURL , err = restrictPathToParent (imgURL , filepath .Dir (codelabSrc )); err != nil {
283+ return "" , err
284+ }
285+ if b , err = ioutil .ReadFile (imgURL ); err != nil {
286+ return "" , err
287+ }
288+ ext = filepath .Ext (imgURL )
289+ } else {
290+ if b , err = f .slurpRemoteBytes (u .String (), 5 ); err != nil {
291+ return "" , fmt .Errorf ("Error downloading image at %s: %v" , u .String (), err )
292+ }
293+ if ext , err = imgExtFromBytes (b ); err != nil {
294+ return "" , fmt .Errorf ("Error reading image type at %s: %v" , u .String (), err )
295+ }
285296 }
286297 }
287298
299+ // Generate image file from slurped bytes.
288300 crc := crc64 .Checksum (b , f .crcTable )
289301 file := fmt .Sprintf ("%x%s" , crc , ext )
290302 dst := filepath .Join (dir , file )
0 commit comments