From aca04ecf57cd17efbb7d720e8508fa494c6b27aa Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Wed, 4 Sep 2024 14:47:02 +0300 Subject: [PATCH] This closes #1987, support absolute paths for pictures (#1988) --- picture.go | 26 ++++++++++++++++++++------ picture_test.go | 13 +++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/picture.go b/picture.go index a9a17bc..42be183 100644 --- a/picture.go +++ b/picture.go @@ -497,8 +497,7 @@ func (f *File) GetPictures(sheet, cell string) ([]Picture, error) { target := f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID) drawingXML := strings.TrimPrefix(strings.ReplaceAll(target, "..", "xl"), "/") drawingRelationships := strings.ReplaceAll( - strings.ReplaceAll(target, "../drawings", "xl/drawings/_rels"), ".xml", ".xml.rels") - + strings.ReplaceAll(drawingXML, "xl/drawings", "xl/drawings/_rels"), ".xml", ".xml.rels") imgs, err := f.getCellImages(sheet, cell) if err != nil { return nil, err @@ -526,7 +525,8 @@ func (f *File) GetPictureCells(sheet string) ([]string, error) { target := f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID) drawingXML := strings.TrimPrefix(strings.ReplaceAll(target, "..", "xl"), "/") drawingRelationships := strings.ReplaceAll( - strings.ReplaceAll(target, "../drawings", "xl/drawings/_rels"), ".xml", ".xml.rels") + strings.ReplaceAll(drawingXML, "xl/drawings", "xl/drawings/_rels"), ".xml", ".xml.rels") + embeddedImageCells, err := f.getImageCells(sheet) if err != nil { return nil, err @@ -609,8 +609,15 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) } } cb2 := func(a *decodeCellAnchor, r *xlsxRelationship) { - pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceOverCells} - if buffer, _ := f.Pkg.Load(filepath.ToSlash(filepath.Clean("xl/drawings/" + r.Target))); buffer != nil { + var target string + if strings.HasPrefix(r.Target, "/") { + target = strings.TrimPrefix(r.Target, "/") + } else { + target = filepath.ToSlash(filepath.Clean("xl/drawings/" + r.Target)) + } + + pic := Picture{Extension: filepath.Ext(target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceOverCells} + if buffer, _ := f.Pkg.Load(target); buffer != nil { pic.File = buffer.([]byte) pic.Format.AltText = a.Pic.NvPicPr.CNvPr.Descr pics = append(pics, pic) @@ -770,7 +777,14 @@ func (f *File) getPictureCells(drawingXML, drawingRelationships string) ([]strin } } cb2 := func(a *decodeCellAnchor, r *xlsxRelationship) { - if _, ok := f.Pkg.Load(filepath.ToSlash(filepath.Clean("xl/drawings/" + r.Target))); ok { + var target string + if strings.HasPrefix(r.Target, "/") { + target = strings.TrimPrefix(r.Target, "/") + } else { + target = filepath.ToSlash(filepath.Clean("xl/drawings/" + r.Target)) + } + + if _, ok := f.Pkg.Load(target); ok { if cell, err := CoordinatesToCellName(a.From.Col+1, a.From.Row+1); err == nil && inStrSlice(cells, cell, true) == -1 { cells = append(cells, cell) } diff --git a/picture_test.go b/picture_test.go index aec0b5c..9da63f9 100644 --- a/picture_test.go +++ b/picture_test.go @@ -219,6 +219,19 @@ func TestGetPicture(t *testing.T) { cells, err := f.GetPictureCells("Sheet2") assert.NoError(t, err) assert.Equal(t, []string{"K16"}, cells) + + // Try to get picture cells with absolute target path in the drawing relationship + rels, err := f.relsReader("xl/drawings/_rels/drawing2.xml.rels") + assert.NoError(t, err) + rels.Relationships[0].Target = "/xl/media/image2.jpeg" + cells, err = f.GetPictureCells("Sheet2") + assert.NoError(t, err) + assert.Equal(t, []string{"K16"}, cells) + // Try to get pictures with absolute target path in the drawing relationship + pics, err = f.GetPictures("Sheet2", "K16") + assert.NoError(t, err) + assert.Len(t, pics, 1) + assert.NoError(t, f.Close()) // Test get picture from none drawing worksheet