r11_urpm-repo/api.go

58 lines
1.9 KiB
Go
Raw Permalink Normal View History

2025-01-13 19:23:44 +03:00
package main
import (
"net/http"
2025-01-31 22:35:47 +03:00
"strings"
2025-01-31 23:52:08 +03:00
"github.com/go-chi/chi/v5"
2025-01-31 22:35:47 +03:00
"github.com/spf13/viper"
2025-01-13 19:23:44 +03:00
)
func (r *Repository) UploadHandler(w http.ResponseWriter, req *http.Request) {
2025-01-31 22:35:47 +03:00
authHeader := req.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
logger.Error("Authorization header missing")
return
}
parts := strings.Split(authHeader, " ")
if len(parts) != 2 || parts[0] != "Bearer" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
logger.Error("Invalid Authorization header format")
return
}
token := parts[1]
if token != viper.GetString("auth.token") {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
logger.Error("Invalid token")
return
}
2025-01-13 19:23:44 +03:00
arch := chi.URLParam(req, "arch")
if arch == "" {
http.Error(w, "Invalid architecture", http.StatusBadRequest)
logger.Error("Invalid architecture provided")
return
}
file, handler, err := req.FormFile("package")
if err != nil {
http.Error(w, "Error retrieving file", http.StatusInternalServerError)
logger.Errorf("Error retrieving file: %v", err)
return
}
defer file.Close()
logger.Infof("Uploading package %s for architecture %s", handler.Filename, arch)
2025-01-31 23:52:08 +03:00
// Передаем управление в Repository для обработки файла
if err := r.ProcessPackage(arch, handler.Filename, file); err != nil {
http.Error(w, "Error processing package", http.StatusInternalServerError)
logger.Errorf("Error processing package: %v", err)
2025-01-13 19:23:44 +03:00
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Package uploaded successfully"))
logger.Infof("Package %s uploaded successfully for architecture %s", handler.Filename, arch)
2025-01-31 23:52:08 +03:00
}