-
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.
- Loading branch information
Showing
34 changed files
with
301 additions
and
1,330 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,17 @@ | ||
package audio | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
func Upload(ctx context.Context, r io.ReadCloser) (int, error) { | ||
defer r.Close() | ||
select { | ||
case <-ctx.Done(): | ||
return 0, context.Canceled | ||
default: | ||
} | ||
|
||
return 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,147 @@ | ||
package audio | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
const ( | ||
defaultCacheDirAudio = "assets" | ||
assetsDirKey = "ASSETS_DIR" | ||
) | ||
|
||
func Path(name string) (path string, err error) { | ||
assertDir := AssertDir() | ||
path = filepath.Join(assertDir, name) | ||
_, err = os.Stat(path) | ||
if err != nil { | ||
path, err = searchPathByNameOrUUID(name) | ||
} | ||
|
||
return | ||
} | ||
|
||
func searchPathByNameOrUUID(prefix string) (p string, err error) { | ||
var paths []string | ||
paths, err = Paths() | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, p = range paths { | ||
base := filepath.Base(p) | ||
if strings.HasPrefix(base, prefix) { | ||
return | ||
} else { | ||
split := strings.Split(base, "-") | ||
if len(split) < 2 { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(strings.Join(split[1:], "-"), prefix) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("path with prefix %s wasn't found", prefix) | ||
} | ||
|
||
func AssertDir() (path string) { | ||
path = defaultCacheDirAudio | ||
if cacheDir, ok := os.LookupEnv(assetsDirKey); ok && cacheDir != "" { | ||
path = cacheDir | ||
} | ||
|
||
return | ||
} | ||
|
||
func Paths() (paths []string, err error) { | ||
assertDir := AssertDir() | ||
dirs, err := os.ReadDir(assertDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(dirs) <= 0 { | ||
return nil, errors.New("assert directory is empty") | ||
} | ||
|
||
paths = make([]string, 0, len(dirs)) | ||
for _, dir := range dirs { | ||
if dir.Type() != os.ModeDir { | ||
paths = append(paths, filepath.Join(assertDir, dir.Name())) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
// PathsWithPagination get fixed-sized list of audio paths from assert dictionary | ||
func PathsWithPagination(page uint32, size uint32) (paths []string, err error) { | ||
dirs, err := os.ReadDir(AssertDir()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
skipFiles := int(page * size) | ||
if len(dirs) < skipFiles { | ||
return []string{}, nil | ||
} else if len(dirs) <= 0 { | ||
return nil, errors.New("assert directory is empty") | ||
} | ||
|
||
paths = make([]string, 0, size) | ||
for _, dir := range dirs[skipFiles:] { | ||
if dir.Type() != os.ModeDir { | ||
paths = append(paths, dir.Name()) | ||
} | ||
|
||
if len(paths) >= int(size) { | ||
break | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func RandomAudioName() (string, error) { | ||
paths, err := Paths() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return paths[rand.Int()%len(paths)], nil | ||
} | ||
|
||
func Duration(path string) (duration time.Duration, err error) { | ||
cmd := exec.Command("ffprobe", "-i", path, "-show_entries", "format=duration", "-v", "quiet", "-of", "csv='p=0'") | ||
cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
Setpgid: true, | ||
} | ||
|
||
if err = cmd.Start(); err != nil { | ||
return | ||
} | ||
|
||
out, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return | ||
} | ||
defer out.Close() | ||
|
||
rawTime, err := io.ReadAll(out) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return time.ParseDuration(string(rawTime) + "s") | ||
} |
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,117 @@ | ||
package audio | ||
|
||
import ( | ||
"errors" | ||
"github.com/wittano/komputer/internal/test" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestAudioIDs_AssetDirHasEmptyDirs(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
for i := 0; i < 5; i++ { | ||
os.Mkdir(filepath.Join(dir, strconv.Itoa(i)), 0700) | ||
} | ||
|
||
if err := os.Setenv(assetsDirKey, dir); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
paths, err := Paths() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(paths) != 0 { | ||
t.Fatal("something was found in empty directory") | ||
} | ||
} | ||
|
||
func TestAudioIDs(t *testing.T) { | ||
const expectedFilesNumber = 5 | ||
if err := test.CreateAssertDir(t, expectedFilesNumber); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
paths, err := Paths() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(paths) != expectedFilesNumber { | ||
t.Fatalf("missing audios IDs. Expected '%d', Result: '%d'", expectedFilesNumber, len(paths)) | ||
} | ||
|
||
for i, id := range paths { | ||
fileID := filepath.Base(strings.Split(id, ".")[0]) | ||
|
||
if fileID != "test-"+strconv.Itoa(i) { | ||
t.Fatalf("invalid ID. Expected: '%d', Result: '%s'", i, fileID) | ||
} | ||
} | ||
} | ||
|
||
func TestPathsWithPagination_ButEmptyDictionary(t *testing.T) { | ||
if err := os.Setenv(assetsDirKey, t.TempDir()); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
res, err := PathsWithPagination(0, 10) | ||
if err == nil { | ||
t.Fatalf("Assert dictionary was found: %s", os.Getenv(assetsDirKey)) | ||
} | ||
|
||
if len(res) != 0 { | ||
t.Fatalf("Something was found in assert dictionary, but it doesn't expect. %v", res) | ||
} | ||
} | ||
|
||
func TestPathsWithPagination_PageIsOverANumberOfFiles(t *testing.T) { | ||
const expectedFilesNumber = 5 | ||
if err := test.CreateAssertDir(t, expectedFilesNumber); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
res, err := PathsWithPagination(10, 10) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(res) != 0 { | ||
t.Fatalf("Something was added to list, but page and size are over number of files. %v", res) | ||
} | ||
} | ||
|
||
func TestPathsWithPagination(t *testing.T) { | ||
const expectedFilesNumber = 50 | ||
if err := test.CreateAssertDir(t, expectedFilesNumber); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
res, err := PathsWithPagination(2, 10) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(res) != 10 { | ||
t.Fatalf("Files wasn't found but shoule be") | ||
} | ||
|
||
const startFileSuffix = 20 | ||
for i := startFileSuffix; i < 30; i++ { | ||
f := res[i-startFileSuffix] | ||
if _, err = os.Stat(f); !errors.Is(err, os.ErrNotExist) { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func TestPathsWithPagination_AssertDirNotFound(t *testing.T) { | ||
if _, err := PathsWithPagination(0, 10); err == nil { | ||
t.Fatalf("Assert dictionary was found") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.