Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add ext check in upload files #803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion server/api/studio/internal/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ func (f *fileService) FileUpload() error {
return ecode.WithErrorMessage(ecode.ErrInternalServer, err, "upload failed")
}
for _, file := range files {
// 检查文件后缀
ext := strings.ToLower(filepath.Ext(file.Filename))
if ext != ".txt" && ext != ".csv" {
return ecode.WithErrorMessage(ecode.ErrInvalidParameter, fmt.Errorf("unsupported file type: %s", ext), "Only .txt and .csv files are supported")
}
if file.Size == 0 || file.Header.Get("Content-Type") != "text/csv" {
continue
}
//csv file charset check for importer

charSet, err := checkCharset(file)
if err != nil {
logx.Infof("upload file error, check charset fail:%v", err)
Expand All @@ -216,6 +221,7 @@ func (f *fileService) FileUpload() error {
if charSet == "UTF-8" {
continue
}

path := filepath.Join(dir, file.Filename)
if err = changeFileCharset2UTF8(path, charSet); err != nil {
logx.Infof("upload file error:%v", err)
Expand Down
1 change: 1 addition & 0 deletions server/api/studio/pkg/ecode/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
ErrUnauthorized = newErrCode(CCUnauthorized, PlatformCode, 0, "ErrUnauthorized") // 40104000
ErrSession = newErrCode(CCUnauthorized, PlatformCode, 1, "ErrSession") // 40104001
ErrForbidden = newErrCode(CCForbidden, PlatformCode, 0, "ErrForbidden") // 40304000
ErrInvalidParameter = newErrCode(CCForbidden, PlatformCode, 1, "ErrInvalidParameter") // 40304001
ErrNotFound = newErrCode(CCNotFound, PlatformCode, 0, "ErrNotFound") // 40404000
ErrInternalServer = newErrCode(CCInternalServer, PlatformCode, 0, "ErrInternalServer") // 50004000
ErrInternalDatabase = newErrCode(CCInternalServer, PlatformCode, 1, "ErrInternalDatabase") // 50004001
Expand Down