From 5878fbd282c13b3ab74cccdba0927a0fac1f1792 Mon Sep 17 00:00:00 2001 From: playGitboy <72111157+playGitboy@users.noreply.github.com> Date: Thu, 23 Mar 2023 00:06:10 +0800 Subject: [PATCH] This closes #1503, case-insensitive for the file extension name --- file.go | 5 +++-- picture.go | 8 ++++---- sheet.go | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/file.go b/file.go index 51cd320d..2896ee44 100644 --- a/file.go +++ b/file.go @@ -18,6 +18,7 @@ import ( "io" "os" "path/filepath" + "strings" "sync" ) @@ -73,7 +74,7 @@ func (f *File) SaveAs(name string, opts ...Options) error { return ErrMaxFilePathLength } f.Path = name - if _, ok := supportedContentTypes[filepath.Ext(f.Path)]; !ok { + if _, ok := supportedContentTypes[strings.ToLower(filepath.Ext(f.Path))]; !ok { return ErrWorkbookFileFormat } file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm) @@ -116,7 +117,7 @@ func (f *File) WriteTo(w io.Writer, opts ...Options) (int64, error) { f.options = &opts[i] } if len(f.Path) != 0 { - contentType, ok := supportedContentTypes[filepath.Ext(f.Path)] + contentType, ok := supportedContentTypes[strings.ToLower(filepath.Ext(f.Path))] if !ok { return 0, ErrWorkbookFileFormat } diff --git a/picture.go b/picture.go index fcc8d5fc..edf53732 100644 --- a/picture.go +++ b/picture.go @@ -151,7 +151,7 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error if _, err = os.Stat(name); os.IsNotExist(err) { return err } - ext, ok := supportedImageTypes[path.Ext(name)] + ext, ok := supportedImageTypes[strings.ToLower(path.Ext(name))] if !ok { return ErrImgExt } @@ -202,7 +202,7 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error { var drawingHyperlinkRID int var hyperlinkType string - ext, ok := supportedImageTypes[pic.Extension] + ext, ok := supportedImageTypes[strings.ToLower(pic.Extension)] if !ok { return ErrImgExt } @@ -659,7 +659,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil { if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row { drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed) - if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok { + if _, ok = supportedImageTypes[strings.ToLower(filepath.Ext(drawRel.Target))]; ok { pic := Picture{Extension: filepath.Ext(drawRel.Target), Format: &GraphicOptions{}} if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { pic.File = buffer.([]byte) @@ -690,7 +690,7 @@ func (f *File) getPicturesFromWsDr(row, col int, drawingRelationships string, ws if anchor.From.Col == col && anchor.From.Row == row { if drawRel = f.getDrawingRelationships(drawingRelationships, anchor.Pic.BlipFill.Blip.Embed); drawRel != nil { - if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok { + if _, ok = supportedImageTypes[strings.ToLower(filepath.Ext(drawRel.Target))]; ok { pic := Picture{Extension: filepath.Ext(drawRel.Target), Format: &GraphicOptions{}} if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { pic.File = buffer.([]byte) diff --git a/sheet.go b/sheet.go index 0bc68154..a6e3d032 100644 --- a/sheet.go +++ b/sheet.go @@ -525,7 +525,7 @@ func (f *File) SetSheetBackgroundFromBytes(sheet, extension string, picture []by // setSheetBackground provides a function to set background picture by given // worksheet name, file name extension and image data. func (f *File) setSheetBackground(sheet, extension string, file []byte) error { - imageType, ok := supportedImageTypes[extension] + imageType, ok := supportedImageTypes[strings.ToLower(extension)] if !ok { return ErrImgExt }