feat(profile): implement protected skin upload endpoint

This commit is contained in:
2025-06-16 07:20:09 +03:00
parent 9082b21a5d
commit 9c7940a70a
7 changed files with 186 additions and 9 deletions

View File

@@ -8,10 +8,14 @@ import (
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/hex" // Для преобразования хеша в строку
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"image/png" // Для валидации PNG
"io"
"mime/multipart"
"os"
"strings"
"time"
@@ -117,3 +121,53 @@ func (s *ProfileService) GetSignedProfile(ctx context.Context, playerUUID uuid.U
return response, nil
}
// UpdateUserSkin обрабатывает загрузку, валидацию и сохранение файла скина.
func (s *ProfileService) UpdateUserSkin(ctx context.Context, userID int, file multipart.File, header *multipart.FileHeader) error {
// 1. Читаем файл в память
fileBytes, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
// Возвращаем указатель файла в начало, чтобы его можно было прочитать снова
file.Seek(0, 0)
// 2. Валидация PNG 64x64
config, err := png.DecodeConfig(file)
if err != nil {
return errors.New("invalid PNG file")
}
if config.Width != 64 || config.Height != 64 {
return errors.New("skin must be 64x64 pixels")
}
// 3. Вычисляем SHA1 хеш
hasher := sha1.New()
hasher.Write(fileBytes)
hash := hex.EncodeToString(hasher.Sum(nil))
// 4. Сохраняем файл на диск
// Путь к хранилищу текстур должен быть конфигурируемым
storagePath := os.Getenv("TEXTURES_STORAGE_PATH")
if storagePath == "" {
return errors.New("textures storage path not configured")
}
filePath := fmt.Sprintf("%s/%s", storagePath, hash)
// Создаем файл только если его еще нет
if _, err := os.Stat(filePath); os.IsNotExist(err) {
outFile, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create skin file: %w", err)
}
defer outFile.Close()
_, err = outFile.Write(fileBytes)
if err != nil {
return fmt.Errorf("failed to write skin file: %w", err)
}
}
// 5. Сохраняем хеш в БД
return s.UserRepo.UpdateSkinHash(ctx, userID, hash)
}