mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 06:56:36 +08:00
代码质量优化
This commit is contained in:
@@ -82,7 +82,7 @@ func ServeFileByID(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Stream from CDN/S3
|
||||
defer obj.Body.Close()
|
||||
defer func() { _ = obj.Body.Close() }()
|
||||
|
||||
// Respond with the file content
|
||||
c.DataFromReader(http.StatusOK, obj.ContentLength, obj.ContentType, obj.Body, nil)
|
||||
|
||||
@@ -88,7 +88,7 @@ func UploadFile(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, util.Err(ErrOpenFileFailed))
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
// 校验大小
|
||||
if header.Size > maxUploadSize {
|
||||
@@ -318,7 +318,7 @@ func DownloadFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
defer obj.Body.Close()
|
||||
defer func() { _ = obj.Body.Close() }()
|
||||
_, _ = io.Copy(c.Writer, obj.Body)
|
||||
}
|
||||
|
||||
@@ -372,7 +372,7 @@ func BatchDownloadFiles(c *gin.Context) {
|
||||
|
||||
// 开启实时 ZIP 压缩器并直接输出给 Response Writer
|
||||
zipWriter := zip.NewWriter(c.Writer)
|
||||
defer zipWriter.Close()
|
||||
defer func() { _ = zipWriter.Close() }()
|
||||
|
||||
// 用于解决 ZIP 内部文件名称发生碰撞冲突的问题
|
||||
usedNames := make(map[string]int)
|
||||
|
||||
@@ -95,7 +95,7 @@ func createMultipartRequest(t *testing.T, fieldName, fileName string, fileConten
|
||||
func TestUploadFile(t *testing.T) {
|
||||
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
|
||||
defer cleanup()
|
||||
defer os.RemoveAll("uploads") // Clean up local files created during tests
|
||||
defer func() { _ = os.RemoveAll("uploads") }() // Clean up local files created during tests
|
||||
|
||||
authUser := &model.User{ID: 1001, Username: "test_user"}
|
||||
router := setupTestRouter(authUser)
|
||||
@@ -211,7 +211,7 @@ func TestUploadFile(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp testResponse
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp.ErrorMsg == "" || !strings.Contains(resp.ErrorMsg, ErrUnsupportedFormat) {
|
||||
t.Errorf("expected unsupported format error, got: %v", resp)
|
||||
}
|
||||
@@ -247,7 +247,7 @@ func TestUploadFile(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp2 testResponse
|
||||
json.Unmarshal(w2.Body.Bytes(), &resp2)
|
||||
_ = json.Unmarshal(w2.Body.Bytes(), &resp2)
|
||||
|
||||
if resp2.ErrorMsg != "" {
|
||||
t.Fatalf("second upload was unsuccessful: %s", resp2.ErrorMsg)
|
||||
@@ -304,7 +304,7 @@ func TestUploadFile(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp testResponse
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
|
||||
if resp.ErrorMsg != "" {
|
||||
t.Fatalf("local upload failed: %s", resp.ErrorMsg)
|
||||
@@ -334,7 +334,7 @@ func TestUploadFile(t *testing.T) {
|
||||
func TestDownloadFile(t *testing.T) {
|
||||
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
|
||||
defer cleanup()
|
||||
defer os.RemoveAll("uploads")
|
||||
defer func() { _ = os.RemoveAll("uploads") }()
|
||||
|
||||
authUser := &model.User{ID: 1001, Username: "test_user"}
|
||||
router := setupTestRouter(authUser)
|
||||
@@ -403,7 +403,7 @@ func TestDownloadFile(t *testing.T) {
|
||||
func TestBatchDownloadFiles(t *testing.T) {
|
||||
dbConn, _, cleanup := testhelper.SetupTestEnvironment(t)
|
||||
defer cleanup()
|
||||
defer os.RemoveAll("uploads")
|
||||
defer func() { _ = os.RemoveAll("uploads") }()
|
||||
|
||||
authUser := &model.User{ID: 1001, Username: "test_user"}
|
||||
router := setupTestRouter(authUser)
|
||||
@@ -495,7 +495,7 @@ func TestBatchDownloadFiles(t *testing.T) {
|
||||
t.Fatalf("failed to open zip file entry %s: %v", f.Name, err)
|
||||
}
|
||||
content, _ := io.ReadAll(rc)
|
||||
rc.Close()
|
||||
_ = rc.Close()
|
||||
extracted[f.Name] = string(content)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ limitations under the License.
|
||||
package upload
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
@@ -27,7 +28,7 @@ const maxS3KeyLength = 1024
|
||||
// ValidateS3Key validates an S3 object key for safety.
|
||||
func ValidateS3Key(key string) error {
|
||||
if key == "" {
|
||||
return fmt.Errorf(ErrS3KeyRequired)
|
||||
return errors.New(ErrS3KeyRequired)
|
||||
}
|
||||
|
||||
if len(key) > maxS3KeyLength {
|
||||
@@ -35,11 +36,11 @@ func ValidateS3Key(key string) error {
|
||||
}
|
||||
|
||||
if strings.HasPrefix(key, "/") {
|
||||
return fmt.Errorf(ErrS3KeyStartsWithSlash)
|
||||
return errors.New(ErrS3KeyStartsWithSlash)
|
||||
}
|
||||
|
||||
if strings.Contains(key, "\x00") {
|
||||
return fmt.Errorf(ErrS3KeyContainsNullBytes)
|
||||
return errors.New(ErrS3KeyContainsNullBytes)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user