Files
scm/api/CertDownload.go
T
2025-07-24 15:01:04 +07:00

48 lines
1.4 KiB
Go

package api
import (
"fmt"
"net/http"
"os"
"path/filepath"
"scm/utils"
)
func CertKeyDownloadZip(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
domain := query.Get("domain")
if domain == "" {
http.Error(w, "domain parameter is required", http.StatusBadRequest)
return
}
// Пути к файлам сертификата и ключа
certPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".crt")
keyPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".key")
// Проверяем существование файлов
if _, err := os.Stat(certPath); os.IsNotExist(err) {
http.Error(w, "Certificate file not found", http.StatusNotFound)
return
}
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
http.Error(w, "Key file not found", http.StatusNotFound)
return
}
// Создаем ZIP архив
zipBuffer, err := utils.CreateDomainZip(certPath, keyPath, domain)
if err != nil {
http.Error(w, fmt.Sprintf("Error creating zip: %v", err), http.StatusInternalServerError)
return
}
// Устанавливаем заголовки для скачивания
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-cert-key.zip\"", domain))
w.Header().Set("Content-Length", fmt.Sprintf("%d", zipBuffer.Len()))
// Отправляем ZIP архив
w.Write(zipBuffer.Bytes())
}