将工厂改成黑盒模式,降低用户使用成本

This commit is contained in:
2025-11-30 15:54:27 +08:00
parent 6323b49517
commit d454d8e143
18 changed files with 2075 additions and 1046 deletions

View File

@@ -43,29 +43,31 @@ func NewUploadHandler(cfg UploadHandlerConfig) *UploadHandler {
// 表单字段: file (文件)
// 可选字段: prefix (对象键前缀,会覆盖配置中的前缀)
func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler := commonhttp.NewHandler(w, r)
if r.Method != http.MethodPost {
commonhttp.NotFound(w, "Method not allowed")
handler.Error(4001, "Method not allowed")
return
}
// 解析multipart表单
err := r.ParseMultipartForm(h.maxFileSize)
if err != nil {
commonhttp.BadRequest(w, fmt.Sprintf("Failed to parse form: %v", err))
handler.Error(4002, fmt.Sprintf("Failed to parse form: %v", err))
return
}
// 获取文件
file, header, err := r.FormFile("file")
if err != nil {
commonhttp.BadRequest(w, fmt.Sprintf("Failed to get file: %v", err))
handler.Error(4003, fmt.Sprintf("Failed to get file: %v", err))
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(1001, fmt.Sprintf("File size exceeds limit: %d bytes", h.maxFileSize))
return
}
@@ -80,7 +82,7 @@ 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(1002, fmt.Sprintf("File extension not allowed. Allowed: %v", h.allowedExts))
return
}
}
@@ -108,14 +110,14 @@ 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))
handler.SystemError(fmt.Sprintf("Failed to upload file: %v", err))
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.SystemError(fmt.Sprintf("Failed to get file URL: %v", err))
return
}
@@ -128,7 +130,7 @@ func (h *UploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
UploadTime: time.Now(),
}
commonhttp.SuccessWithMessage(w, "Upload successful", result)
handler.SuccessWithMessage("Upload successful", result)
}
// generateUniqueFilename 生成唯一文件名
@@ -154,15 +156,17 @@ func NewProxyHandler(storage Storage) *ProxyHandler {
// ServeHTTP 处理文件查看请求
// URL参数: key (对象键)
func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler := commonhttp.NewHandler(w, r)
if r.Method != http.MethodGet {
commonhttp.NotFound(w, "Method not allowed")
handler.Error(4001, "Method not allowed")
return
}
// 获取对象键
objectKey := r.URL.Query().Get("key")
objectKey := handler.GetQuery("key", "")
if objectKey == "" {
commonhttp.BadRequest(w, "Missing parameter: key")
handler.Error(4004, "Missing parameter: key")
return
}
@@ -170,19 +174,19 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
exists, err := h.storage.Exists(ctx, objectKey)
if err != nil {
commonhttp.SystemError(w, fmt.Sprintf("Failed to check file existence: %v", err))
handler.SystemError(fmt.Sprintf("Failed to check file existence: %v", err))
return
}
if !exists {
commonhttp.NotFound(w, "File not found")
handler.Error(4005, "File not found")
return
}
// 获取文件内容
reader, err := h.storage.GetObject(ctx, objectKey)
if err != nil {
commonhttp.SystemError(w, fmt.Sprintf("Failed to get file: %v", err))
handler.SystemError(fmt.Sprintf("Failed to get file: %v", err))
return
}
defer reader.Close()
@@ -206,7 +210,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 复制文件内容到响应
_, err = io.Copy(w, reader)
if err != nil {
commonhttp.SystemError(w, fmt.Sprintf("Failed to write response: %v", err))
handler.SystemError(fmt.Sprintf("Failed to write response: %v", err))
return
}
}