document: add support for extracting/saving document thumbnail

This commit is contained in:
Todd 2017-09-01 16:12:59 -05:00
parent 9675d9e2db
commit ec7c4ce013
2 changed files with 38 additions and 4 deletions

View File

@ -17,6 +17,7 @@ const (
SettingsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"
ImageType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
CommentsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
ThumbnailType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
ExtendedPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
CorePropertiesType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"

View File

@ -11,6 +11,8 @@ import (
"archive/zip"
"errors"
"fmt"
"image"
"image/jpeg"
"io"
"io/ioutil"
"log"
@ -29,9 +31,12 @@ import (
type Document struct {
common.DocBase
x *wml.Document
Settings Settings
Numbering Numbering
Styles Styles
Settings Settings // document settings
Numbering Numbering // numbering styles within the doucment
Styles Styles // styles that are use and can be used within the document
Thumbnail image.Image // thumbnail preview of the document
headers []*wml.Hdr
footers []*wml.Ftr
docRels common.Relationships
@ -137,6 +142,15 @@ func (d *Document) Save(w io.Writer) error {
if err := zippkg.MarshalXML(z, "docProps/core.xml", d.CoreProperties.X()); err != nil {
return err
}
if d.Thumbnail != nil {
tn, err := z.Create("docProps/thumbnail.jpeg")
if err != nil {
return err
}
if err := jpeg.Encode(tn, d.Thumbnail, nil); err != nil {
return err
}
}
if err := zippkg.MarshalXML(z, "word/settings.xml", d.Settings.X()); err != nil {
return err
}
@ -337,6 +351,25 @@ func Read(r io.ReaderAt, size int64) (*Document, error) {
decMap[r.Target()] = doc.CoreProperties.X()
case common.ExtendedPropertiesType:
decMap[r.Target()] = doc.AppProperties.X()
case common.ThumbnailType:
// read our thumbnail
for i, f := range files {
if f == nil {
continue
}
if f.Name == r.Target() {
rc, err := f.Open()
if err != nil {
return nil, fmt.Errorf("error reading thumbnail: %s", err)
}
doc.Thumbnail, _, err = image.Decode(rc)
rc.Close()
if err != nil {
return nil, fmt.Errorf("error decoding thumbnail: %s", err)
}
files[i] = nil
}
}
default:
log.Printf("unsupported type: %s", r.Type())
}