From 9f8623047d2fc38e12c3b214475710d25ec88c55 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 20 Jun 2019 00:00:40 +0800 Subject: Optimize code, fix golint issues --- picture.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index 01c2ae2..7804bce 100644 --- a/picture.go +++ b/picture.go @@ -499,26 +499,33 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string { func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { col, row, err := CellNameToCoordinates(cell) if err != nil { - return "", []byte{}, err + return "", nil, err } col-- row-- xlsx, err := f.workSheetReader(sheet) if err != nil { - return "", []byte{}, err + return "", nil, err } if xlsx.Drawing == nil { - return "", []byte{}, err + return "", nil, err } - target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID) drawingXML := strings.Replace(target, "..", "xl", -1) - + _, ok := f.XLSX[drawingXML] + if !ok { + return "", nil, err + } drawingRelationships := strings.Replace( strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1) - wsDr, _ := f.drawingParser(drawingXML) + return f.getPicture(row, col, drawingXML, drawingRelationships) +} +// 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 { if anchor.From != nil && anchor.Pic != nil { if anchor.From.Col == col && anchor.From.Row == row { @@ -528,16 +535,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { if ok { return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, - "..", "xl", -1)]), err + "..", "xl", -1)]), nil } } } } - _, ok := f.XLSX[drawingXML] - if !ok { - return "", nil, err - } decodeWsDr := decodeWsDr{} _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr) for _, anchor := range decodeWsDr.TwoCellAnchor { @@ -548,12 +551,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed) _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)] if ok { - return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), err + return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), nil } } } } - return "", []byte{}, err + return "", nil, nil } // getDrawingRelationships provides a function to get drawing relationships -- cgit v1.2.1