Switch file to io stream in recognizer interface
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package recognizer
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MemoryAudioRecognizer struct{}
|
||||
|
||||
func (r *MemoryAudioRecognizer) RecognizeFile(filePath string) (operationID string, err error) {
|
||||
func (r *MemoryAudioRecognizer) Recognize(file io.Reader, fileName string) (operationID string, err error) {
|
||||
return uuid.NewString(), nil
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@ package yandex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"io"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
)
|
||||
@@ -54,10 +54,9 @@ func (s *YandexAudioRecognizerService) Close() error {
|
||||
return s.sttService.Close()
|
||||
}
|
||||
|
||||
func (s *YandexAudioRecognizerService) RecognizeFile(filePath string) (string, error) {
|
||||
fileName := filepath.Base(filePath)
|
||||
func (s *YandexAudioRecognizerService) Recognize(file io.Reader, fileName string) (string, error) {
|
||||
|
||||
err := s.s3Sevice.uploadFile(filePath, fileName)
|
||||
err := s.s3Sevice.uploadFile(file, fileName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ package yandex
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
@@ -65,14 +65,8 @@ func newYandexS3Service(cfg s3Config) (*yandexS3Service, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *yandexS3Service) uploadFile(filePath, fileName string) error {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %s: %w", filePath, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = s.uploader.Upload(context.Background(), &s3.PutObjectInput{
|
||||
func (s *yandexS3Service) uploadFile(file io.Reader, fileName string) error {
|
||||
_, err := s.uploader.Upload(context.Background(), &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucketName),
|
||||
Key: aws.String(fileName),
|
||||
Body: file,
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"git.vakhrushev.me/av/transcriber/internal/entity"
|
||||
)
|
||||
|
||||
@@ -8,13 +10,8 @@ type AudioFileConverter interface {
|
||||
Convert(src, dest string) error
|
||||
}
|
||||
|
||||
type YandexS3Uploader interface {
|
||||
UploadFile(filePath, fileName string) error
|
||||
FileUrl(fileName string) string
|
||||
}
|
||||
|
||||
type AudioRecognizer interface {
|
||||
RecognizeFile(filePath string) (operationID string, err error)
|
||||
Recognize(file io.Reader, fileName string) (operationID string, err error)
|
||||
GetRecognitionText(operationID string) (string, error)
|
||||
CheckRecognitionStatus(operationID string) (*entity.RecognitionResult, error)
|
||||
}
|
||||
|
@@ -175,11 +175,17 @@ func (s *TranscribeService) FindAndRunTranscribeJob() error {
|
||||
|
||||
filePath := filepath.Join(baseStorageDir, fileRecord.FileName)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
destFileId := uuid.NewString()
|
||||
destFileRecord := fileRecord.CopyWithStorage(destFileId, entity.StorageS3)
|
||||
|
||||
// Запускаем асинхронное распознавание
|
||||
operationID, err := s.recognizer.RecognizeFile(filePath)
|
||||
operationID, err := s.recognizer.Recognize(file, destFileRecord.FileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user