重构项目的实现,优化使用方法与使用逻辑
This commit is contained in:
@@ -39,37 +39,32 @@ func NewUploadHandler(cfg UploadHandlerConfig) *UploadHandler {
|
||||
}
|
||||
|
||||
// ServeHTTP 处理文件上传请求
|
||||
// 请求方式: POST
|
||||
// 表单字段: file (文件)
|
||||
// 可选字段: prefix (对象键前缀,会覆盖配置中的前缀)
|
||||
func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
handler := commonhttp.NewHandler(w, r)
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
commonhttp.Error(w, 4001, "Method not allowed")
|
||||
handler.Error("common.method_not_allowed")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析multipart表单
|
||||
err := r.ParseMultipartForm(h.maxFileSize)
|
||||
if err != nil {
|
||||
commonhttp.Error(w, 4002, fmt.Sprintf("Failed to parse form: %v", err))
|
||||
handler.Error("common.invalid_request")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取文件
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
commonhttp.Error(w, 4003, fmt.Sprintf("Failed to get file: %v", err))
|
||||
handler.Error("common.invalid_request")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 检查文件大小
|
||||
if h.maxFileSize > 0 && header.Size > h.maxFileSize {
|
||||
commonhttp.Error(w, 1001, fmt.Sprintf("File size exceeds limit: %d bytes", h.maxFileSize))
|
||||
handler.Error("storage.file_too_large")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查文件扩展名
|
||||
if len(h.allowedExts) > 0 {
|
||||
ext := strings.ToLower(filepath.Ext(header.Filename))
|
||||
allowed := false
|
||||
@@ -80,22 +75,19 @@ func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if !allowed {
|
||||
commonhttp.Error(w, 1002, fmt.Sprintf("File extension not allowed. Allowed: %v", h.allowedExts))
|
||||
handler.Error("storage.invalid_extension")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 生成对象键
|
||||
prefix := h.objectPrefix
|
||||
if r.FormValue("prefix") != "" {
|
||||
prefix = r.FormValue("prefix")
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
filename := generateUniqueFilename(header.Filename)
|
||||
objectKey := GenerateObjectKey(prefix, filename)
|
||||
|
||||
// 获取文件类型
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = mime.TypeByExtension(filepath.Ext(header.Filename))
|
||||
@@ -104,22 +96,18 @@ func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
ctx := r.Context()
|
||||
err = h.storage.Upload(ctx, objectKey, file, contentType)
|
||||
if err != nil {
|
||||
commonhttp.SystemError(w, fmt.Sprintf("Failed to upload file: %v", err))
|
||||
if err = h.storage.Upload(ctx, objectKey, file, contentType); err != nil {
|
||||
handler.Error("system.internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// 获取文件URL
|
||||
fileURL, err := h.storage.GetURL(objectKey, 0)
|
||||
if err != nil {
|
||||
commonhttp.SystemError(w, fmt.Sprintf("Failed to get file URL: %v", err))
|
||||
handler.Error("system.internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// 返回结果
|
||||
result := UploadResult{
|
||||
ObjectKey: objectKey,
|
||||
URL: fileURL,
|
||||
@@ -127,8 +115,7 @@ func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ContentType: contentType,
|
||||
UploadTime: time.Now(),
|
||||
}
|
||||
|
||||
commonhttp.Success(w, result, "Upload successful")
|
||||
handler.Success(result)
|
||||
}
|
||||
|
||||
// generateUniqueFilename 生成唯一文件名
|
||||
|
||||
Reference in New Issue
Block a user