make workbook open filed exception message clear
- New exported constant `ErrWorkbookPassword` - Rename exported constant `ErrWorkbookExt` to `ErrWorkbookFileFormat`
This commit is contained in:
parent
8f16a76781
commit
63adac2589
|
@ -19,6 +19,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEncrypt(t *testing.T) {
|
func TestEncrypt(t *testing.T) {
|
||||||
|
_, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "passwd"})
|
||||||
|
assert.EqualError(t, err, ErrWorkbookPassword.Error())
|
||||||
|
|
||||||
f, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "password"})
|
f, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "password"})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), ErrEncrypt.Error())
|
assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), ErrEncrypt.Error())
|
||||||
|
|
|
@ -106,9 +106,9 @@ var (
|
||||||
// ErrImgExt defined the error message on receive an unsupported image
|
// ErrImgExt defined the error message on receive an unsupported image
|
||||||
// extension.
|
// extension.
|
||||||
ErrImgExt = errors.New("unsupported image extension")
|
ErrImgExt = errors.New("unsupported image extension")
|
||||||
// ErrWorkbookExt defined the error message on receive an unsupported
|
// ErrWorkbookFileFormat defined the error message on receive an
|
||||||
// workbook extension.
|
// unsupported workbook file format.
|
||||||
ErrWorkbookExt = errors.New("unsupported workbook extension")
|
ErrWorkbookFileFormat = errors.New("unsupported workbook file format")
|
||||||
// ErrMaxFilePathLength defined the error message on receive the file path
|
// ErrMaxFilePathLength defined the error message on receive the file path
|
||||||
// length overflow.
|
// length overflow.
|
||||||
ErrMaxFilePathLength = errors.New("file path length exceeds maximum limit")
|
ErrMaxFilePathLength = errors.New("file path length exceeds maximum limit")
|
||||||
|
@ -191,4 +191,7 @@ var (
|
||||||
// ErrSparklineStyle defined the error message on receive the invalid
|
// ErrSparklineStyle defined the error message on receive the invalid
|
||||||
// sparkline Style parameters.
|
// sparkline Style parameters.
|
||||||
ErrSparklineStyle = errors.New("parameter 'Style' must between 0-35")
|
ErrSparklineStyle = errors.New("parameter 'Style' must between 0-35")
|
||||||
|
// ErrWorkbookPassword defined the error message on receiving the incorrect
|
||||||
|
// workbook password.
|
||||||
|
ErrWorkbookPassword = errors.New("the supplied open workbook password is not correct")
|
||||||
)
|
)
|
||||||
|
|
|
@ -158,13 +158,15 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) {
|
||||||
return nil, ErrOptionsUnzipSizeLimit
|
return nil, ErrOptionsUnzipSizeLimit
|
||||||
}
|
}
|
||||||
if bytes.Contains(b, oleIdentifier) {
|
if bytes.Contains(b, oleIdentifier) {
|
||||||
b, err = Decrypt(b, f.options)
|
if b, err = Decrypt(b, f.options); err != nil {
|
||||||
if err != nil {
|
return nil, ErrWorkbookFileFormat
|
||||||
return nil, fmt.Errorf("decrypted file failed")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
|
zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if len(f.options.Password) > 0 {
|
||||||
|
return nil, ErrWorkbookPassword
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
file, sheetCount, err := f.ReadZipReader(zr)
|
file, sheetCount, err := f.ReadZipReader(zr)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package excelize
|
package excelize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
@ -179,7 +180,7 @@ func TestSaveFile(t *testing.T) {
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
assert.EqualError(t, f.SaveAs(filepath.Join("test", "TestSaveFile.xlsb")), ErrWorkbookExt.Error())
|
assert.EqualError(t, f.SaveAs(filepath.Join("test", "TestSaveFile.xlsb")), ErrWorkbookFileFormat.Error())
|
||||||
for _, ext := range []string{".xlam", ".xlsm", ".xlsx", ".xltm", ".xltx"} {
|
for _, ext := range []string{".xlam", ".xlsm", ".xlsx", ".xltm", ".xltx"} {
|
||||||
assert.NoError(t, f.SaveAs(filepath.Join("test", fmt.Sprintf("TestSaveFile%s", ext))))
|
assert.NoError(t, f.SaveAs(filepath.Join("test", fmt.Sprintf("TestSaveFile%s", ext))))
|
||||||
}
|
}
|
||||||
|
@ -207,9 +208,9 @@ func TestCharsetTranscoder(t *testing.T) {
|
||||||
|
|
||||||
func TestOpenReader(t *testing.T) {
|
func TestOpenReader(t *testing.T) {
|
||||||
_, err := OpenReader(strings.NewReader(""))
|
_, err := OpenReader(strings.NewReader(""))
|
||||||
assert.EqualError(t, err, "zip: not a valid zip file")
|
assert.EqualError(t, err, zip.ErrFormat.Error())
|
||||||
_, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1})
|
_, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1})
|
||||||
assert.EqualError(t, err, "decrypted file failed")
|
assert.EqualError(t, err, ErrWorkbookFileFormat.Error())
|
||||||
|
|
||||||
// Test open spreadsheet with unzip size limit.
|
// Test open spreadsheet with unzip size limit.
|
||||||
_, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100})
|
_, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100})
|
||||||
|
@ -261,7 +262,7 @@ func TestOpenReader(t *testing.T) {
|
||||||
0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00, 0x5d, 0x00,
|
0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00, 0x5d, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00,
|
0x00, 0x00, 0x00, 0x00,
|
||||||
}))
|
}))
|
||||||
assert.EqualError(t, err, "zip: unsupported compression algorithm")
|
assert.EqualError(t, err, zip.ErrAlgorithm.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBrokenFile(t *testing.T) {
|
func TestBrokenFile(t *testing.T) {
|
||||||
|
|
2
file.go
2
file.go
|
@ -78,7 +78,7 @@ func (f *File) SaveAs(name string, opt ...Options) error {
|
||||||
".xltx": ContentTypeTemplate,
|
".xltx": ContentTypeTemplate,
|
||||||
}[filepath.Ext(f.Path)]
|
}[filepath.Ext(f.Path)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrWorkbookExt
|
return ErrWorkbookFileFormat
|
||||||
}
|
}
|
||||||
f.setContentTypePartProjectExtensions(contentType)
|
f.setContentTypePartProjectExtensions(contentType)
|
||||||
file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||||
|
|
Loading…
Reference in New Issue