Replace all infra services with interfaces
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/adapter/speechkit"
|
||||
"git.vakhrushev.me/av/transcriber/internal/contract"
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
"github.com/google/uuid"
|
||||
@@ -17,23 +16,23 @@ import (
|
||||
const baseStorageDir = "data/files"
|
||||
|
||||
type TranscribeService struct {
|
||||
jobRepo contract.TranscriptJobRepository
|
||||
fileRepo contract.FileRepository
|
||||
converter contract.AudioFileConverter
|
||||
s3Service contract.YandexS3Uploader
|
||||
jobRepo contract.TranscriptJobRepository
|
||||
fileRepo contract.FileRepository
|
||||
converter contract.AudioFileConverter
|
||||
recognizer contract.AudioRecognizer
|
||||
}
|
||||
|
||||
func NewTranscribeService(
|
||||
jobRepo contract.TranscriptJobRepository,
|
||||
fileRepo contract.FileRepository,
|
||||
converter contract.AudioFileConverter,
|
||||
s3Service contract.YandexS3Uploader,
|
||||
recognizer contract.AudioRecognizer,
|
||||
) *TranscribeService {
|
||||
return &TranscribeService{
|
||||
jobRepo: jobRepo,
|
||||
fileRepo: fileRepo,
|
||||
converter: converter,
|
||||
s3Service: s3Service,
|
||||
jobRepo: jobRepo,
|
||||
fileRepo: fileRepo,
|
||||
converter: converter,
|
||||
recognizer: recognizer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,23 +178,8 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
destFileId := uuid.NewString()
|
||||
destFileRecord := fileRecord.CopyWithStorage(destFileId, entity.StorageS3)
|
||||
|
||||
// Загружаем файл на S3
|
||||
err = s.s3Service.UploadFile(filePath, destFileRecord.FileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Создаем SpeechKit сервис
|
||||
speechKitService, err := speechkit.NewSpeechKitService()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Формируем S3 URI для файла
|
||||
s3URI := s.s3Service.FileUrl(destFileRecord.FileName)
|
||||
|
||||
// Запускаем асинхронное распознавание
|
||||
operationID, err := speechKitService.RecognizeFileFromS3(s3URI)
|
||||
operationID, err := s.recognizer.RecognizeFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -235,23 +219,16 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
return fmt.Errorf("recogniton opId not found for job: %s", job.Id)
|
||||
}
|
||||
|
||||
// Создаем SpeechKit сервис
|
||||
speechKitService, err := speechkit.NewSpeechKitService()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer speechKitService.Close()
|
||||
|
||||
opId := *job.RecognitionOpID
|
||||
|
||||
// Проверяем статус операции
|
||||
log.Printf("Check operation status: id %s\n", opId)
|
||||
operation, err := speechKitService.CheckOperationStatus(opId)
|
||||
recResult, err := s.recognizer.CheckRecognitionStatus(opId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !operation.Done {
|
||||
if recResult.IsInProgress() {
|
||||
// Операция еще не завершена, оставляем в статусе обработки
|
||||
log.Printf("Operation in progress: id %s\n", opId)
|
||||
delayTime := time.Now().Add(10 * time.Second)
|
||||
@@ -263,8 +240,8 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if opErr := operation.GetError(); opErr != nil {
|
||||
errorText := fmt.Sprintf("operation failed: code %d, message: %s", opErr.Code, opErr.Message)
|
||||
if recResult.IsFailed() {
|
||||
errorText := recResult.GetError()
|
||||
log.Printf("Operation failed: id %s, message %s\n", opId, errorText)
|
||||
job.Fail(errorText)
|
||||
err := s.jobRepo.Save(job)
|
||||
@@ -275,7 +252,7 @@ func (s *TranscribeService) FindAndRunTranscribeCheckJob() error {
|
||||
}
|
||||
|
||||
// Операция завершена, получаем результат
|
||||
transcriptionText, err := speechKitService.GetRecognitionText(*job.RecognitionOpID)
|
||||
transcriptionText, err := s.recognizer.GetRecognitionText(opId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user