Add images from bytes for presentation and workbook (#295)

This commit is contained in:
Marc Coury 2019-07-02 16:54:55 +02:00 committed by Gunnsteinn Hall
parent a3c3e9ba5b
commit c6ab0e98df
5 changed files with 41 additions and 27 deletions

27
common/helpers.go Normal file
View File

@ -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
}

View File

@ -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
}
}

View File

@ -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
}

View File

@ -19,7 +19,6 @@ import (
"log"
"os"
"path"
"strings"
"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/common"
@ -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
}
}

View File

@ -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
}
}