2025-01-07 21:18:40 +03:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-08 00:22:46 +03:00
|
|
|
|
"crypto/sha1"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2025-01-08 00:24:00 +03:00
|
|
|
|
"tvoygit.ru/Djam/abfapi"
|
2025-01-07 21:18:40 +03:00
|
|
|
|
)
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// MockServer представляет собой тестовый HTTP-сервер с моками.
|
2025-01-07 22:17:56 +03:00
|
|
|
|
type MockServer struct {
|
2025-01-08 00:22:46 +03:00
|
|
|
|
Server *httptest.Server
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// NewMockServer создает новый тестовый сервер с моками.
|
2025-01-07 22:17:56 +03:00
|
|
|
|
func NewMockServer() *MockServer {
|
2025-01-08 00:22:46 +03:00
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Мок для /api/v1/arches.json
|
2025-01-08 00:22:46 +03:00
|
|
|
|
mux.HandleFunc("/api/v1/arches.json", func(w http.ResponseWriter, r *http.Request) {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Handling request to /api/v1/arches.json")
|
2025-01-08 00:22:46 +03:00
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response := map[string]interface{}{
|
|
|
|
|
"architectures": []string{"x86_64", "i386", "arm64"},
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
|
})
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Мок для /api/v1/upload
|
2025-01-08 00:22:46 +03:00
|
|
|
|
mux.HandleFunc("/api/v1/upload", func(w http.ResponseWriter, r *http.Request) {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Handling request to /api/v1/upload")
|
2025-01-08 00:22:46 +03:00
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if contentType := r.Header.Get("Content-Type"); !strings.Contains(contentType, "multipart/form-data") {
|
2025-01-08 14:45:29 +03:00
|
|
|
|
log.Printf("Invalid Content-Type: %s", contentType)
|
2025-01-08 00:22:46 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Парсим multipart/form-data
|
2025-01-08 00:22:46 +03:00
|
|
|
|
err := r.ParseMultipartForm(10 << 20) // 10 MB max memory
|
|
|
|
|
if err != nil {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Error parsing form: %v", err)
|
2025-01-08 00:22:46 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Получаем файл из формы
|
2025-01-08 00:22:46 +03:00
|
|
|
|
file, handler, err := r.FormFile("file_store[file]")
|
|
|
|
|
if err != nil {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Error getting file from form: %v", err)
|
2025-01-08 00:22:46 +03:00
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Читаем содержимое файла
|
2025-01-08 00:22:46 +03:00
|
|
|
|
content, err := io.ReadAll(file)
|
|
|
|
|
if err != nil {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Error reading file content: %v", err)
|
2025-01-08 00:22:46 +03:00
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Вычисляем SHA1 хеш файла
|
2025-01-08 00:22:46 +03:00
|
|
|
|
hasher := sha1.New()
|
|
|
|
|
hasher.Write(content)
|
|
|
|
|
shaHash := fmt.Sprintf("%x", hasher.Sum(nil))
|
|
|
|
|
|
|
|
|
|
response := map[string]interface{}{
|
|
|
|
|
"sha1_hash": shaHash,
|
|
|
|
|
"file_name": handler.Filename,
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
|
})
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Мок для /api/v1/file_stores/{hash}
|
2025-01-08 00:22:46 +03:00
|
|
|
|
mux.HandleFunc("/api/v1/file_stores/", func(w http.ResponseWriter, r *http.Request) {
|
2025-01-08 00:48:27 +03:00
|
|
|
|
log.Printf("Handling request to /api/v1/file_stores/")
|
2025-01-08 00:22:46 +03:00
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
parts := strings.Split(r.URL.Path, "/")
|
|
|
|
|
if len(parts) < 5 {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
shaHash := parts[4]
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Пример ответа для конкретного хеша
|
2025-01-08 00:22:46 +03:00
|
|
|
|
if shaHash == "be730b67b175ac8dc96c9006e88e4166713cb1b5" {
|
|
|
|
|
response := map[string]interface{}{
|
|
|
|
|
"sha1_hash": shaHash,
|
|
|
|
|
"file_name": "testfile.txt",
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2025-01-08 00:43:24 +03:00
|
|
|
|
json.NewEncoder(w).Encode([]interface{}{response})
|
2025-01-08 00:22:46 +03:00
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
|
return &MockServer{
|
|
|
|
|
Server: server,
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
2025-01-08 00:22:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Close останавливает тестовый сервер.
|
2025-01-08 00:22:46 +03:00
|
|
|
|
func (ms *MockServer) Close() {
|
|
|
|
|
ms.Server.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
logger := log.New(os.Stdout, "ABF: ", log.LstdFlags)
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Создаем тестовый сервер с моками
|
2025-01-08 00:22:46 +03:00
|
|
|
|
mockServer := NewMockServer()
|
|
|
|
|
defer mockServer.Close()
|
2025-01-07 22:17:56 +03:00
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Используем URL тестового сервера вместо реальных URL
|
2025-01-08 00:22:46 +03:00
|
|
|
|
abfClient, err := abfapi.NewAbfJson(mockServer.Server.URL, mockServer.Server.URL, "username", "password", logger)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
if err != nil {
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Fatalf("Failed to create ABF client: %v", err)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Пример вызова метода GetArchitectures
|
2025-01-08 00:22:46 +03:00
|
|
|
|
architectures, err := abfClient.GetArchitectures()
|
2025-01-07 22:17:56 +03:00
|
|
|
|
if err != nil {
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Fatalf("Failed to get architectures: %v", err)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Printf("Architectures: %+v", architectures)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Пример вызова метода UploadFile
|
2025-01-08 00:22:46 +03:00
|
|
|
|
shaHash, err := abfClient.UploadFile("testfile.txt", false)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
if err != nil {
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Fatalf("Failed to upload file: %v", err)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Printf("Uploaded file SHA1: %s", shaHash)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
|
2025-01-08 00:43:24 +03:00
|
|
|
|
// Пример вызова метода FetchFile
|
2025-01-08 00:22:46 +03:00
|
|
|
|
err = abfClient.FetchFile(shaHash, "downloaded_testfile.txt")
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Fatalf("Failed to fetch file: %v", err)
|
2025-01-07 22:17:56 +03:00
|
|
|
|
}
|
2025-01-08 00:22:46 +03:00
|
|
|
|
logger.Printf("File fetched successfully")
|
2025-01-08 00:43:24 +03:00
|
|
|
|
}
|