Skip to content

Commit 54def7e

Browse files
committed
Add TIF, TIFF format images and more detailed error information when open the encrypted file
1 parent 9f86230 commit 54def7e

File tree

7 files changed

+95
-10
lines changed

7 files changed

+95
-10
lines changed

excelize.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"archive/zip"
1515
"bytes"
1616
"encoding/xml"
17+
"errors"
1718
"fmt"
1819
"io"
1920
"io/ioutil"
@@ -69,6 +70,17 @@ func OpenReader(r io.Reader) (*File, error) {
6970

7071
zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
7172
if err != nil {
73+
identifier := []byte{
74+
// checking protect workbook by [MS-OFFCRYPTO] - v20181211 3.1 FeatureIdentifier
75+
0x3c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00,
76+
0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00,
77+
0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x44, 0x00, 0x61, 0x00,
78+
0x74, 0x00, 0x61, 0x00, 0x53, 0x00, 0x70, 0x00, 0x61, 0x00, 0x63, 0x00, 0x65, 0x00, 0x73, 0x00,
79+
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
80+
}
81+
if bytes.Contains(b, identifier) {
82+
return nil, errors.New("not support encrypted file currently")
83+
}
7284
return nil, err
7385
}
7486

excelize_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package excelize
22

33
import (
4+
"bytes"
45
"fmt"
56
"image/color"
67
_ "image/gif"
@@ -185,6 +186,14 @@ func TestSaveAsWrongPath(t *testing.T) {
185186
func TestOpenReader(t *testing.T) {
186187
_, err := OpenReader(strings.NewReader(""))
187188
assert.EqualError(t, err, "zip: not a valid zip file")
189+
_, err = OpenReader(bytes.NewReader([]byte{
190+
0x3c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00,
191+
0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00,
192+
0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x44, 0x00, 0x61, 0x00,
193+
0x74, 0x00, 0x61, 0x00, 0x53, 0x00, 0x70, 0x00, 0x61, 0x00, 0x63, 0x00, 0x65, 0x00, 0x73, 0x00,
194+
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
195+
}))
196+
assert.EqualError(t, err, "not support encrypted file currently")
188197
}
189198

190199
func TestBrokenFile(t *testing.T) {

picture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func (f *File) addMedia(file []byte, ext string) string {
385385
// setContentTypePartImageExtensions provides a function to set the content
386386
// type for relationship parts and the Main Document part.
387387
func (f *File) setContentTypePartImageExtensions() {
388-
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
388+
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
389389
content := f.contentTypesReader()
390390
for _, v := range content.Defaults {
391391
_, ok := imageTypes[v.Extension]

picture_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package excelize
22

33
import (
4-
"fmt"
4+
_ "image/gif"
5+
_ "image/jpeg"
56
_ "image/png"
7+
8+
_ "golang.org/x/image/tiff"
9+
10+
"fmt"
611
"io/ioutil"
712
"os"
813
"path/filepath"
@@ -25,37 +30,41 @@ func BenchmarkAddPictureFromBytes(b *testing.B) {
2530
}
2631

2732
func TestAddPicture(t *testing.T) {
28-
xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
33+
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
2934
if !assert.NoError(t, err) {
3035
t.FailNow()
3136
}
3237

3338
// Test add picture to worksheet with offset and location hyperlink.
34-
err = xlsx.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
39+
err = f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
3540
`{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
3641
if !assert.NoError(t, err) {
3742
t.FailNow()
3843
}
3944

4045
// Test add picture to worksheet with offset, external hyperlink and positioning.
41-
err = xlsx.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
46+
err = f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
4247
`{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`)
4348
if !assert.NoError(t, err) {
4449
t.FailNow()
4550
}
4651

47-
file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.jpg"))
52+
file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
4853
if !assert.NoError(t, err) {
4954
t.FailNow()
5055
}
5156

5257
// Test add picture to worksheet from bytes.
53-
assert.NoError(t, xlsx.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".jpg", file))
58+
assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
5459
// Test add picture to worksheet from bytes with illegal cell coordinates.
55-
assert.EqualError(t, xlsx.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".jpg", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
60+
assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
61+
62+
assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
63+
assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
64+
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
5665

5766
// Test write file to given path.
58-
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
67+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
5968
}
6069

6170
func TestAddPictureErrors(t *testing.T) {

test/images/excel.tif

26.4 KB
Binary file not shown.

xmlApp.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
2+
// this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
//
5+
// Package excelize providing a set of functions that allow you to write to
6+
// and read from XLSX files. Support reads and writes XLSX file generated by
7+
// Microsoft Excel™ 2007 and later. Support save file without losing original
8+
// charts of XLSX. This library needs Go version 1.8 or later.
9+
10+
package excelize
11+
12+
import "encoding/xml"
13+
14+
type xlsxProperties struct {
15+
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties Properties"`
16+
Template string
17+
Manager string
18+
Company string
19+
Pages int
20+
Words int
21+
Characters int
22+
PresentationFormat string
23+
Lines int
24+
Paragraphs int
25+
Slides int
26+
Notes int
27+
TotalTime int
28+
HiddenSlides int
29+
MMClips int
30+
ScaleCrop bool
31+
HeadingPairs *xlsxVectorVariant
32+
TitlesOfParts *xlsxVectorLpstr
33+
LinksUpToDate bool
34+
CharactersWithSpaces int
35+
SharedDoc bool
36+
HyperlinkBase string
37+
HLinks *xlsxVectorVariant
38+
HyperlinksChanged bool
39+
DigSig *xlsxDigSig
40+
Application string
41+
AppVersion string
42+
DocSecurity int
43+
}
44+
45+
type xlsxVectorVariant struct {
46+
Content string `xml:",innerxml"`
47+
}
48+
49+
type xlsxVectorLpstr struct {
50+
Content string `xml:",innerxml"`
51+
}
52+
53+
type xlsxDigSig struct {
54+
Content string `xml:",innerxml"`
55+
}

xmlDrawing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242
NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/"
4343
)
4444

45-
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}
45+
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff"}
4646

4747
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
4848
// element specifies non-visual canvas properties. This allows for additional

0 commit comments

Comments
 (0)