summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2019-06-27 21:58:14 +0800
committerxuri <xuri.me@gmail.com>2019-06-27 21:58:14 +0800
commit54def7eaad9ee0469ca495b3661798919239384a (patch)
tree4f99eeb2e929c9baa037f31574f7b45cd3d11b81
parent9f8623047d2fc38e12c3b214475710d25ec88c55 (diff)
Add TIF, TIFF format images and more detailed error information when open the encrypted file
-rw-r--r--excelize.go12
-rw-r--r--excelize_test.go9
-rw-r--r--picture.go2
-rw-r--r--picture_test.go25
-rw-r--r--test/images/excel.tifbin0 -> 27052 bytes
-rw-r--r--xmlApp.go55
-rw-r--r--xmlDrawing.go2
7 files changed, 95 insertions, 10 deletions
diff --git a/excelize.go b/excelize.go
index 6fb98c4..f636a84 100644
--- a/excelize.go
+++ b/excelize.go
@@ -14,6 +14,7 @@ import (
"archive/zip"
"bytes"
"encoding/xml"
+ "errors"
"fmt"
"io"
"io/ioutil"
@@ -69,6 +70,17 @@ func OpenReader(r io.Reader) (*File, error) {
zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
if err != nil {
+ identifier := []byte{
+ // checking protect workbook by [MS-OFFCRYPTO] - v20181211 3.1 FeatureIdentifier
+ 0x3c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00,
+ 0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00,
+ 0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x44, 0x00, 0x61, 0x00,
+ 0x74, 0x00, 0x61, 0x00, 0x53, 0x00, 0x70, 0x00, 0x61, 0x00, 0x63, 0x00, 0x65, 0x00, 0x73, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+ }
+ if bytes.Contains(b, identifier) {
+ return nil, errors.New("not support encrypted file currently")
+ }
return nil, err
}
diff --git a/excelize_test.go b/excelize_test.go
index c7c3aec..c4a06a5 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -1,6 +1,7 @@
package excelize
import (
+ "bytes"
"fmt"
"image/color"
_ "image/gif"
@@ -185,6 +186,14 @@ func TestSaveAsWrongPath(t *testing.T) {
func TestOpenReader(t *testing.T) {
_, err := OpenReader(strings.NewReader(""))
assert.EqualError(t, err, "zip: not a valid zip file")
+ _, err = OpenReader(bytes.NewReader([]byte{
+ 0x3c, 0x00, 0x00, 0x00, 0x4d, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x73, 0x00,
+ 0x6f, 0x00, 0x66, 0x00, 0x74, 0x00, 0x2e, 0x00, 0x43, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00,
+ 0x61, 0x00, 0x69, 0x00, 0x6e, 0x00, 0x65, 0x00, 0x72, 0x00, 0x2e, 0x00, 0x44, 0x00, 0x61, 0x00,
+ 0x74, 0x00, 0x61, 0x00, 0x53, 0x00, 0x70, 0x00, 0x61, 0x00, 0x63, 0x00, 0x65, 0x00, 0x73, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+ }))
+ assert.EqualError(t, err, "not support encrypted file currently")
}
func TestBrokenFile(t *testing.T) {
diff --git a/picture.go b/picture.go
index 7804bce..812eb5c 100644
--- a/picture.go
+++ b/picture.go
@@ -385,7 +385,7 @@ func (f *File) addMedia(file []byte, ext string) string {
// setContentTypePartImageExtensions provides a function to set the content
// type for relationship parts and the Main Document part.
func (f *File) setContentTypePartImageExtensions() {
- var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
+ var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
content := f.contentTypesReader()
for _, v := range content.Defaults {
_, ok := imageTypes[v.Extension]
diff --git a/picture_test.go b/picture_test.go
index 890092e..9a2edda 100644
--- a/picture_test.go
+++ b/picture_test.go
@@ -1,8 +1,13 @@
package excelize
import (
- "fmt"
+ _ "image/gif"
+ _ "image/jpeg"
_ "image/png"
+
+ _ "golang.org/x/image/tiff"
+
+ "fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -25,37 +30,41 @@ func BenchmarkAddPictureFromBytes(b *testing.B) {
}
func TestAddPicture(t *testing.T) {
- xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
+ f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
t.FailNow()
}
// Test add picture to worksheet with offset and location hyperlink.
- err = xlsx.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
+ err = f.AddPicture("Sheet2", "I9", filepath.Join("test", "images", "excel.jpg"),
`{"x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
if !assert.NoError(t, err) {
t.FailNow()
}
// Test add picture to worksheet with offset, external hyperlink and positioning.
- err = xlsx.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
+ err = f.AddPicture("Sheet1", "F21", filepath.Join("test", "images", "excel.jpg"),
`{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`)
if !assert.NoError(t, err) {
t.FailNow()
}
- file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.jpg"))
+ file, err := ioutil.ReadFile(filepath.Join("test", "images", "excel.png"))
if !assert.NoError(t, err) {
t.FailNow()
}
// Test add picture to worksheet from bytes.
- assert.NoError(t, xlsx.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".jpg", file))
+ assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", "", "Excel Logo", ".png", file))
// Test add picture to worksheet from bytes with illegal cell coordinates.
- assert.EqualError(t, xlsx.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".jpg", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
+ assert.EqualError(t, f.AddPictureFromBytes("Sheet1", "A", "", "Excel Logo", ".png", file), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
+
+ assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), ""))
+ assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), ""))
+ assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
// Test write file to given path.
- assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
+ assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
}
func TestAddPictureErrors(t *testing.T) {
diff --git a/test/images/excel.tif b/test/images/excel.tif
new file mode 100644
index 0000000..4ce5eff
--- /dev/null
+++ b/test/images/excel.tif
Binary files differ
diff --git a/xmlApp.go b/xmlApp.go
new file mode 100644
index 0000000..ad414fa
--- /dev/null
+++ b/xmlApp.go
@@ -0,0 +1,55 @@
+// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
+// this source code is governed by a BSD-style license that can be found in
+// the LICENSE file.
+//
+// Package excelize providing a set of functions that allow you to write to
+// and read from XLSX files. Support reads and writes XLSX file generated by
+// Microsoft Excelâ„¢ 2007 and later. Support save file without losing original
+// charts of XLSX. This library needs Go version 1.8 or later.
+
+package excelize
+
+import "encoding/xml"
+
+type xlsxProperties struct {
+ XMLName xml.Name `xml:"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties Properties"`
+ Template string
+ Manager string
+ Company string
+ Pages int
+ Words int
+ Characters int
+ PresentationFormat string
+ Lines int
+ Paragraphs int
+ Slides int
+ Notes int
+ TotalTime int
+ HiddenSlides int
+ MMClips int
+ ScaleCrop bool
+ HeadingPairs *xlsxVectorVariant
+ TitlesOfParts *xlsxVectorLpstr
+ LinksUpToDate bool
+ CharactersWithSpaces int
+ SharedDoc bool
+ HyperlinkBase string
+ HLinks *xlsxVectorVariant
+ HyperlinksChanged bool
+ DigSig *xlsxDigSig
+ Application string
+ AppVersion string
+ DocSecurity int
+}
+
+type xlsxVectorVariant struct {
+ Content string `xml:",innerxml"`
+}
+
+type xlsxVectorLpstr struct {
+ Content string `xml:",innerxml"`
+}
+
+type xlsxDigSig struct {
+ Content string `xml:",innerxml"`
+}
diff --git a/xmlDrawing.go b/xmlDrawing.go
index 13e164e..bb468bc 100644
--- a/xmlDrawing.go
+++ b/xmlDrawing.go
@@ -42,7 +42,7 @@ const (
NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/"
)
-var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}
+var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff"}
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
// element specifies non-visual canvas properties. This allows for additional