mirror of
https://tvoygit.ru/Djam/r11_urpm-repo.git
synced 2025-02-23 18:22:47 +00:00
58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"github.com/go-chi/chi/v5"
|
|
"strings"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func (r *Repository) UploadHandler(w http.ResponseWriter, req *http.Request) {
|
|
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
|
|
}
|
|
|
|
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)
|
|
|
|
if err := r.SavePackage(arch, handler.Filename, file); err != nil {
|
|
http.Error(w, "Error saving package", http.StatusInternalServerError)
|
|
logger.Errorf("Error saving package: %v", err)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("Package uploaded successfully"))
|
|
logger.Infof("Package %s uploaded successfully for architecture %s", handler.Filename, arch)
|
|
}
|