Skip to content

Commit

Permalink
Add images from bytes for presentation and workbook (unidoc#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
mec07 authored and gunnsth committed Jul 2, 2019
1 parent a3c3e9b commit c6ab0e9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
27 changes: 27 additions & 0 deletions common/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package common

import (
"archive/zip"
"fmt"
"strings"

"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/zippkg"
)

// AddImageToZip adds an image (either from bytes or from disk) and adds it to the zip file.
func AddImageToZip(z *zip.Writer, img ImageRef, imageNum int, dt unioffice.DocType) error {
filename := unioffice.AbsoluteImageFilename(dt, imageNum, strings.ToLower(img.Format()))
if img.Data() != nil && len(*img.Data()) > 0 {
if err := zippkg.AddFileFromBytes(z, filename, *img.Data()); err != nil {
return err
}
} else if img.Path() != "" {
if err := zippkg.AddFileFromDisk(z, filename, img.Path()); err != nil {
return err
}
} else {
return fmt.Errorf("unsupported image source: %+v", img)
}
return nil
}
13 changes: 2 additions & 11 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,8 @@ func (d *Document) Save(w io.Writer) error {
}

for i, img := range d.Images {
fn := fmt.Sprintf("word/media/image%d.%s", i+1, strings.ToLower(img.Format()))
if img.Data() != nil {
if err := zippkg.AddFileFromBytes(z, fn, *img.Data()); err != nil {
return err
}
} else if img.Path() != "" {
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
return err
}
} else {
unioffice.Log("unsupported image source: %+v", img)
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypeDocument); err != nil {
return err
}
}

Expand Down
8 changes: 8 additions & 0 deletions filenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ func AbsoluteFilename(dt DocType, typ string, index int) string {
}
return ""
}

// AbsoluteImageFilename returns the full path to an image from the root of the
// zip container.
func AbsoluteImageFilename(dt DocType, index int, fileExtension string) string {
filename := AbsoluteFilename(dt, ImageType, index)
// replace "png" with the actual file extension
return filename[0:len(filename)-3] + fileExtension
}
11 changes: 2 additions & 9 deletions presentation/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"log"
"os"
"path"
"strings"

"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/common"
Expand Down Expand Up @@ -443,14 +442,8 @@ func (p *Presentation) Save(w io.Writer) error {
}

for i, img := range p.Images {
fn := unioffice.AbsoluteFilename(unioffice.DocTypePresentation, unioffice.ImageType, i+1)
fn = fn[0:len(fn)-3] + strings.ToLower(img.Format())
if img.Path() != "" {
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
return err
}
} else {
unioffice.Log("unsupported image source: %+v", img)
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypePresentation); err != nil {
return err
}
}

Expand Down
9 changes: 2 additions & 7 deletions spreadsheet/workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,8 @@ func (wb *Workbook) Save(w io.Writer) error {
}

for i, img := range wb.Images {
fn := fmt.Sprintf("xl/media/image%d.%s", i+1, img.Format())
if img.Path() != "" {
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
return err
}
} else {
unioffice.Log("unsupported image source: %+v", img)
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypeSpreadsheet); err != nil {
return err
}
}

Expand Down

0 comments on commit c6ab0e9

Please sign in to comment.