Skip to content

Commit 235d477

Browse files
fix: add ext check in upload files (#803)
* 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
1 parent d9b1b8f commit 235d477

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

app/config/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const service = {
6262
return post('/api/files/update')(params, config);
6363
},
6464
uploadFiles: (params?, config?) => {
65-
return put('/api/files')(params, { ...config, headers: { 'Content-Type': 'multipart/form-data' } });
65+
return post('/api/files')(params, { ...config, headers: { 'Content-Type': 'multipart/form-data' } });
6666
},
6767
initSketch: (params, config?) => {
6868
return post(`/api/sketches/sketch`)(params, config);

server/api/studio/internal/service/file.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@ func (f *fileService) FileUpload() error {
204204
return ecode.WithErrorMessage(ecode.ErrInternalServer, err, "upload failed")
205205
}
206206
for _, file := range files {
207+
// 检查文件后缀
208+
ext := strings.ToLower(filepath.Ext(file.Filename))
209+
if ext != ".txt" && ext != ".csv" {
210+
return ecode.WithErrorMessage(ecode.ErrInvalidParameter, fmt.Errorf("unsupported file type: %s", ext), "Only .txt and .csv files are supported")
211+
}
207212
if file.Size == 0 || file.Header.Get("Content-Type") != "text/csv" {
208213
continue
209214
}
210-
//csv file charset check for importer
215+
211216
charSet, err := checkCharset(file)
212217
if err != nil {
213218
logx.Infof("upload file error, check charset fail:%v", err)
@@ -216,6 +221,7 @@ func (f *fileService) FileUpload() error {
216221
if charSet == "UTF-8" {
217222
continue
218223
}
224+
219225
path := filepath.Join(dir, file.Filename)
220226
if err = changeFileCharset2UTF8(path, charSet); err != nil {
221227
logx.Infof("upload file error:%v", err)

server/api/studio/pkg/ecode/codes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
ErrUnauthorized = newErrCode(CCUnauthorized, PlatformCode, 0, "ErrUnauthorized") // 40104000
1818
ErrSession = newErrCode(CCUnauthorized, PlatformCode, 1, "ErrSession") // 40104001
1919
ErrForbidden = newErrCode(CCForbidden, PlatformCode, 0, "ErrForbidden") // 40304000
20+
ErrInvalidParameter = newErrCode(CCForbidden, PlatformCode, 1, "ErrInvalidParameter") // 40304001
2021
ErrNotFound = newErrCode(CCNotFound, PlatformCode, 0, "ErrNotFound") // 40404000
2122
ErrInternalServer = newErrCode(CCInternalServer, PlatformCode, 0, "ErrInternalServer") // 50004000
2223
ErrInternalDatabase = newErrCode(CCInternalServer, PlatformCode, 1, "ErrInternalDatabase") // 50004001

server/api/studio/restapi/file.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type (
3030
service studio-api {
3131
@doc "Upload File"
3232
@handler FileUpload
33-
put /api/files
33+
post /api/files
3434
@doc "delete file"
3535
@handler FileDestroy
3636
delete /api/files(FileDestroyRequest)

0 commit comments

Comments
 (0)