Skip to content

Commit

Permalink
fix: add ext check in upload files (#803)
Browse files Browse the repository at this point in the history
* fix: add ext check in upload files

* feat: change put method to  post for unsecuring http method

* feat: change put method to  post for unsecuring http method
  • Loading branch information
xigongdaEricyang authored Oct 15, 2024
1 parent d9b1b8f commit 235d477
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/config/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const service = {
return post('/api/files/update')(params, config);
},
uploadFiles: (params?, config?) => {
return put('/api/files')(params, { ...config, headers: { 'Content-Type': 'multipart/form-data' } });
return post('/api/files')(params, { ...config, headers: { 'Content-Type': 'multipart/form-data' } });
},
initSketch: (params, config?) => {
return post(`/api/sketches/sketch`)(params, config);
Expand Down
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
2 changes: 1 addition & 1 deletion server/api/studio/restapi/file.api
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type (
service studio-api {
@doc "Upload File"
@handler FileUpload
put /api/files
post /api/files
@doc "delete file"
@handler FileDestroy
delete /api/files(FileDestroyRequest)
Expand Down

0 comments on commit 235d477

Please sign in to comment.