Skip to content

Commit

Permalink
This closes qax-os#1503, case-insensitive for the file extension name
Browse files Browse the repository at this point in the history
  • Loading branch information
playGitboy authored and xuri committed Jul 11, 2023
1 parent 9413b4e commit 1854cf5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 1854cf5

Please sign in to comment.