107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package http
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.vakhrushev.me/av/transcriber/internal/contract"
|
|
"git.vakhrushev.me/av/transcriber/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const baseStorageDir = "data/files"
|
|
|
|
type TranscribeHandler struct {
|
|
jobRepo contract.TranscriptJobRepository
|
|
trsService *service.TranscribeService
|
|
}
|
|
|
|
func NewTranscribeHandler(jobRepo contract.TranscriptJobRepository, trsService *service.TranscribeService) *TranscribeHandler {
|
|
return &TranscribeHandler{jobRepo: jobRepo, trsService: trsService}
|
|
}
|
|
|
|
type CreateTranscribeJobResponse struct {
|
|
JobID string `json:"job_id"`
|
|
State string `json:"status"`
|
|
}
|
|
|
|
type GetTranscribeJobResponse struct {
|
|
JobID string `json:"job_id"`
|
|
State string `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
TranscriptionText *string `json:"transcription_text,omitempty"`
|
|
}
|
|
|
|
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()
|
|
|
|
job, err := h.trsService.CreateTranscribeJob(file, header.Filename)
|
|
if err != nil {
|
|
log.Printf("Err: %v", err)
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create transcibe job"})
|
|
return
|
|
}
|
|
|
|
// Возвращаем успешный ответ
|
|
response := CreateTranscribeJobResponse{
|
|
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, GetTranscribeJobResponse{
|
|
JobID: job.Id,
|
|
State: job.State,
|
|
CreatedAt: job.CreatedAt,
|
|
TranscriptionText: job.TranscriptionText,
|
|
})
|
|
}
|
|
|
|
func (h *TranscribeHandler) RunConversionJob(c *gin.Context) {
|
|
err := h.trsService.FindAndRunConversionJob()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
func (h *TranscribeHandler) RunTranscribeJob(c *gin.Context) {
|
|
err := h.trsService.FindAndRunTranscribeJob()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
func (h *TranscribeHandler) RunRecognitionCheckJob(c *gin.Context) {
|
|
err := h.trsService.FindAndRunTranscribeCheckJob()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
}
|