This closes #1503, case-insensitive for the file extension name

This commit is contained in:
playGitboy 2023-03-23 00:06:10 +08:00 committed by GitHub
parent a34c81e1cc
commit 5878fbd282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 7 deletions

View File

@ -18,6 +18,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
) )
@ -73,7 +74,7 @@ func (f *File) SaveAs(name string, opts ...Options) error {
return ErrMaxFilePathLength return ErrMaxFilePathLength
} }
f.Path = name f.Path = name
if _, ok := supportedContentTypes[filepath.Ext(f.Path)]; !ok { if _, ok := supportedContentTypes[strings.ToLower(filepath.Ext(f.Path))]; !ok {
return ErrWorkbookFileFormat return ErrWorkbookFileFormat
} }
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)
@ -116,7 +117,7 @@ func (f *File) WriteTo(w io.Writer, opts ...Options) (int64, error) {
f.options = &opts[i] f.options = &opts[i]
} }
if len(f.Path) != 0 { if len(f.Path) != 0 {
contentType, ok := supportedContentTypes[filepath.Ext(f.Path)] contentType, ok := supportedContentTypes[strings.ToLower(filepath.Ext(f.Path))]
if !ok { if !ok {
return 0, ErrWorkbookFileFormat return 0, ErrWorkbookFileFormat
} }

View File

@ -151,7 +151,7 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error
if _, err = os.Stat(name); os.IsNotExist(err) { if _, err = os.Stat(name); os.IsNotExist(err) {
return err return err
} }
ext, ok := supportedImageTypes[path.Ext(name)] ext, ok := supportedImageTypes[strings.ToLower(path.Ext(name))]
if !ok { if !ok {
return ErrImgExt 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 { func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
var drawingHyperlinkRID int var drawingHyperlinkRID int
var hyperlinkType string var hyperlinkType string
ext, ok := supportedImageTypes[pic.Extension] ext, ok := supportedImageTypes[strings.ToLower(pic.Extension)]
if !ok { if !ok {
return ErrImgExt 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 err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil {
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row { if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed) 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{}} pic := Picture{Extension: filepath.Ext(drawRel.Target), Format: &GraphicOptions{}}
if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil {
pic.File = buffer.([]byte) 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 anchor.From.Col == col && anchor.From.Row == row {
if drawRel = f.getDrawingRelationships(drawingRelationships, if drawRel = f.getDrawingRelationships(drawingRelationships,
anchor.Pic.BlipFill.Blip.Embed); drawRel != nil { 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{}} pic := Picture{Extension: filepath.Ext(drawRel.Target), Format: &GraphicOptions{}}
if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil { if buffer, _ := f.Pkg.Load(strings.ReplaceAll(drawRel.Target, "..", "xl")); buffer != nil {
pic.File = buffer.([]byte) pic.File = buffer.([]byte)

View File

@ -525,7 +525,7 @@ func (f *File) SetSheetBackgroundFromBytes(sheet, extension string, picture []by
// setSheetBackground provides a function to set background picture by given // setSheetBackground provides a function to set background picture by given
// worksheet name, file name extension and image data. // worksheet name, file name extension and image data.
func (f *File) setSheetBackground(sheet, extension string, file []byte) error { func (f *File) setSheetBackground(sheet, extension string, file []byte) error {
imageType, ok := supportedImageTypes[extension] imageType, ok := supportedImageTypes[strings.ToLower(extension)]
if !ok { if !ok {
return ErrImgExt return ErrImgExt
} }