From 3ee3c38f9c63de3782fad21aae9c05ee0530fc32 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 23 Jan 2022 00:32:34 +0800 Subject: Fix file corrupted in some cases, check file extension and format code Fix file corrupted when save as in XLAM / XLSM / XLTM / XLTX extension in some case New exported error ErrWorkbookExt has been added, and check file extension on save the workbook Format source code with `gofumpt` --- file.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'file.go') diff --git a/file.go b/file.go index 1f2b772..43d94b2 100644 --- a/file.go +++ b/file.go @@ -70,6 +70,17 @@ func (f *File) SaveAs(name string, opt ...Options) error { return ErrMaxFileNameLength } f.Path = name + contentType, ok := map[string]string{ + ".xlam": ContentTypeAddinMacro, + ".xlsm": ContentTypeMacro, + ".xlsx": ContentTypeSheetML, + ".xltm": ContentTypeTemplateMacro, + ".xltx": ContentTypeTemplate, + }[filepath.Ext(f.Path)] + if !ok { + return ErrWorkbookExt + } + f.setContentTypePartProjectExtensions(contentType) file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600) if err != nil { return err -- cgit v1.2.1