日志方法增加异步与同步的方法
This commit is contained in:
@@ -155,18 +155,17 @@ func NewProxyHandler(storage Storage) *ProxyHandler {
|
||||
|
||||
// ServeHTTP 处理文件查看请求
|
||||
// URL参数: key (对象键)
|
||||
// 注意:此方法直接返回文件内容(二进制),错误时返回标准HTTP错误状态码
|
||||
func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
handler := commonhttp.NewHandler(w, r)
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
handler.Error(4001, "Method not allowed")
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取对象键
|
||||
objectKey := handler.GetQuery("key", "")
|
||||
objectKey := r.URL.Query().Get("key")
|
||||
if objectKey == "" {
|
||||
handler.Error(4004, "Missing parameter: key")
|
||||
http.Error(w, "Missing parameter: key", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -174,19 +173,19 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
exists, err := h.storage.Exists(ctx, objectKey)
|
||||
if err != nil {
|
||||
handler.SystemError(fmt.Sprintf("Failed to check file existence: %v", err))
|
||||
http.Error(w, fmt.Sprintf("Failed to check file existence: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
handler.Error(4005, "File not found")
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取文件内容
|
||||
reader, err := h.storage.GetObject(ctx, objectKey)
|
||||
if err != nil {
|
||||
handler.SystemError(fmt.Sprintf("Failed to get file: %v", err))
|
||||
http.Error(w, fmt.Sprintf("Failed to get file: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer reader.Close()
|
||||
@@ -210,7 +209,8 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// 复制文件内容到响应
|
||||
_, err = io.Copy(w, reader)
|
||||
if err != nil {
|
||||
handler.SystemError(fmt.Sprintf("Failed to write response: %v", err))
|
||||
// 如果已经开始写入响应,无法再设置错误状态码
|
||||
// 这里只能记录错误,无法返回错误响应
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user