-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(audio): moved audio logic to new business services
- Loading branch information
Showing
17 changed files
with
348 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"github.com/wittano/komputer/db" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
// TestMongodbService This service is mock. It shouldn't use in production code | ||
type TestMongodbService struct { | ||
client *mongo.Client | ||
ctx context.Context | ||
} | ||
|
||
func (t TestMongodbService) Close() error { | ||
return t.client.Disconnect(t.ctx) | ||
} | ||
|
||
func (t TestMongodbService) Client(_ context.Context) (*mongo.Client, error) { | ||
return t.client, nil | ||
} | ||
|
||
func NewMockedMognodbService(ctx context.Context, client *mongo.Client) db.MongodbService { | ||
return &TestMongodbService{client, ctx} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"mime/multipart" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func CreateTempAudioFiles(t *testing.T) (string, error) { | ||
dir := t.TempDir() | ||
f, err := os.CreateTemp(dir, "test.*.mp3") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write([]byte{0xff, 0xfb}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return f.Name(), nil | ||
} | ||
|
||
func CreateMultipartFileHeader(path string) (*multipart.FileHeader, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
var buf bytes.Buffer | ||
|
||
formWriter := multipart.NewWriter(&buf) | ||
filename := filepath.Base(path) | ||
formPart, err := formWriter.CreateFormFile(filename, filepath.Base(path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err = io.Copy(formPart, f); err != nil { | ||
return nil, err | ||
} | ||
|
||
err = formWriter.Close() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader := bytes.NewReader(buf.Bytes()) | ||
formReader := multipart.NewReader(reader, formWriter.Boundary()) | ||
|
||
multipartForm, err := formReader.ReadForm(1 << 20) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if file, ok := multipartForm.File[filename]; !ok || len(file) <= 0 { | ||
return nil, errors.New("failed create multipart audio") | ||
} else { | ||
return file[0], nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/wittano/komputer/web/settings" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func LoadDefaultConfig(t *testing.T) error { | ||
const defaultConfigFileName = "config.yml" | ||
configFile := filepath.Join(t.TempDir(), defaultConfigFileName) | ||
|
||
if err := settings.Load(configFile); err != nil { | ||
return err | ||
} | ||
|
||
return settings.Config.Update(settings.Settings{AssetDir: filepath.Join(t.TempDir(), "assets")}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.