Refactoring: clean architecture project structure
This commit is contained in:
117
internal/controller/http/transcribe.go
Normal file
117
internal/controller/http/transcribe.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
"git.vakhrushev.me/av/transcriber/internal/repo"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type TranscribeHandler struct {
|
||||
jobRepo repo.TranscriptJobRepository
|
||||
fileRepo repo.FileRepository
|
||||
}
|
||||
|
||||
func NewTranscribeHandler(jobRepo repo.TranscriptJobRepository, fileRepo repo.FileRepository) *TranscribeHandler {
|
||||
return &TranscribeHandler{jobRepo: jobRepo, fileRepo: fileRepo}
|
||||
}
|
||||
|
||||
type TranscribeResponse struct {
|
||||
JobID string `json:"job_id"`
|
||||
State string `json:"status"`
|
||||
}
|
||||
|
||||
func (h *TranscribeHandler) CreateTranscribeJob(c *gin.Context) {
|
||||
// Получаем файл из формы
|
||||
file, header, err := c.Request.FormFile("audio")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No audio file provided"})
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Генерируем UUID для файла
|
||||
fileId := uuid.New().String()
|
||||
|
||||
// Определяем расширение файла
|
||||
ext := filepath.Ext(header.Filename)
|
||||
if ext == "" {
|
||||
ext = ".audio" // fallback если расширение не определено
|
||||
}
|
||||
|
||||
// Создаем путь для сохранения файла
|
||||
fileName := fmt.Sprintf("%s%s", fileId, ext)
|
||||
filePath := filepath.Join("data", "files", fileName)
|
||||
|
||||
// Создаем файл на диске
|
||||
dst, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"})
|
||||
return
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
// Копируем содержимое загруженного файла
|
||||
size, err := io.Copy(dst, file)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
|
||||
return
|
||||
}
|
||||
|
||||
// Создаем запись в таблице files
|
||||
fileRecord := &entity.File{
|
||||
Id: fileId,
|
||||
Storage: entity.StorageLocal,
|
||||
Size: size,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := h.fileRepo.Create(fileRecord); err != nil {
|
||||
// Удаляем файл если не удалось создать запись в БД
|
||||
os.Remove(filePath)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file record"})
|
||||
return
|
||||
}
|
||||
|
||||
// Создаем запись в таблице transcribe_jobs
|
||||
jobId := uuid.New().String()
|
||||
job := &entity.TranscribeJob{
|
||||
Id: jobId,
|
||||
State: entity.StateCreated,
|
||||
FileID: &fileId,
|
||||
IsError: false,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := h.jobRepo.Create(job); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create transcribe job"})
|
||||
return
|
||||
}
|
||||
|
||||
// Возвращаем успешный ответ
|
||||
response := TranscribeResponse{
|
||||
JobID: job.Id,
|
||||
State: job.State,
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
func (h *TranscribeHandler) GetTranscribeJobStatus(c *gin.Context) {
|
||||
jobID := c.Param("id")
|
||||
|
||||
job, err := h.jobRepo.GetByID(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Job not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, job)
|
||||
}
|
Reference in New Issue
Block a user