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