diff --git a/common/schemas.go b/common/schemas.go index bba6447b..f69aaffc 100644 --- a/common/schemas.go +++ b/common/schemas.go @@ -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" diff --git a/document/document.go b/document/document.go index 72124722..f916cf74 100644 --- a/document/document.go +++ b/document/document.go @@ -11,6 +11,8 @@ import ( "archive/zip" "errors" "fmt" + "image" + "image/jpeg" "io" "io/ioutil" "log" @@ -28,10 +30,13 @@ import ( // Document is a text document that can be written out in the OOXML .docx format. type Document struct { common.DocBase - x *wml.Document - Settings Settings - Numbering Numbering - Styles Styles + x *wml.Document + + 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()) }