diff options
author | xuri <xuri.me@gmail.com> | 2019-12-20 01:00:15 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2019-12-20 01:00:15 +0800 |
commit | 4249dad8eaf8a6dee93fafeffe0bbbd794581ffe (patch) | |
tree | c9f32214bd8b5e60d7987b8fb7bac43e5fbae703 /picture.go | |
parent | a526e90404913f5d649d29a7aeee29f5ac9ff590 (diff) | |
parent | b1b3c0d15158abc71267da5893de020f047c3872 (diff) |
Resolve #539 Merge branch 'v2'
# Conflicts:
# rows.go
# sheet.go
Diffstat (limited to 'picture.go')
-rw-r--r-- | picture.go | 62 |
1 files changed, 40 insertions, 22 deletions
@@ -14,7 +14,9 @@ import ( "encoding/json" "encoding/xml" "errors" + "fmt" "image" + "io" "io/ioutil" "os" "path" @@ -471,39 +473,55 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { // getPicture provides a function to get picture base name and raw content // embed in XLSX by given coordinates and drawing relationships. -func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) (string, []byte, error) { - wsDr, _ := f.drawingParser(drawingXML) - for _, anchor := range wsDr.TwoCellAnchor { +func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) (ret string, buf []byte, err error) { + var ( + wsDr *xlsxWsDr + ok bool + anchor *xdrCellAnchor + deWsDr *decodeWsDr + drawRel *xlsxRelationship + deTwoCellAnchor *decodeTwoCellAnchor + ) + + wsDr, _ = f.drawingParser(drawingXML) + for _, anchor = range wsDr.TwoCellAnchor { if anchor.From != nil && anchor.Pic != nil { if anchor.From.Col == col && anchor.From.Row == row { - xlsxRelationship := f.getDrawingRelationships(drawingRelationships, + drawRel = f.getDrawingRelationships(drawingRelationships, anchor.Pic.BlipFill.Blip.Embed) - _, ok := supportImageTypes[filepath.Ext(xlsxRelationship.Target)] - if ok { - return filepath.Base(xlsxRelationship.Target), - []byte(f.XLSX[strings.Replace(xlsxRelationship.Target, - "..", "xl", -1)]), nil + if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok { + ret, buf = filepath.Base(drawRel.Target), []byte(f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)]) + return } } } } - - decodeWsDr := decodeWsDr{} - _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr) - for _, anchor := range decodeWsDr.TwoCellAnchor { - decodeTwoCellAnchor := decodeTwoCellAnchor{} - _ = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+anchor.Content+"</decodeTwoCellAnchor>"), &decodeTwoCellAnchor) - if decodeTwoCellAnchor.From != nil && decodeTwoCellAnchor.Pic != nil { - if decodeTwoCellAnchor.From.Col == col && decodeTwoCellAnchor.From.Row == row { - xlsxRelationship := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed) - _, ok := supportImageTypes[filepath.Ext(xlsxRelationship.Target)] - if ok { - return filepath.Base(xlsxRelationship.Target), []byte(f.XLSX[strings.Replace(xlsxRelationship.Target, "..", "xl", -1)]), nil + deWsDr = new(decodeWsDr) + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(drawingXML)))). + Decode(deWsDr); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return + } + err = nil + for _, anchor := range deWsDr.TwoCellAnchor { + deTwoCellAnchor = new(decodeTwoCellAnchor) + if err = f.xmlNewDecoder(bytes.NewReader([]byte("<decodeTwoCellAnchor>" + anchor.Content + "</decodeTwoCellAnchor>"))). + Decode(deTwoCellAnchor); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return + } + if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil { + if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row { + drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed) + if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok { + ret, buf = filepath.Base(drawRel.Target), []byte(f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)]) + return } } } } - return "", nil, nil + + return } // getDrawingRelationships provides a function to get drawing relationships |