This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
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.
feat(server): added audio services (getting info, upload and download…
… audio)
- Loading branch information
Showing
14 changed files
with
255 additions
and
20 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
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
File renamed without changes.
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
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
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
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,102 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/google/uuid" | ||
komputer "github.com/wittano/komputer/api/proto" | ||
"github.com/wittano/komputer/audio" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"os" | ||
"sync" | ||
) | ||
|
||
type audioServer struct { | ||
komputer.UnimplementedAudioServiceServer | ||
} | ||
|
||
func (a audioServer) List(pagination *komputer.Pagination, server komputer.AudioService_ListServer) (err error) { | ||
page := paginationOrDefault(pagination) | ||
|
||
paths, err := audio.PathsWithPagination(page.Page, page.Size) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, path := range paths { | ||
err = errors.Join(err, server.Send(&komputer.AudioInfo{Name: path, Type: komputer.FileFormat_MP3})) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (a audioServer) Add(server komputer.AudioService_AddServer) error { | ||
for { | ||
au, err := server.Recv() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
path := audio.Path(fmt.Sprintf("%s-%s.%s", au.Info.Name, uuid.NewString(), au.Info.Type.String())) | ||
|
||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
f.Close() | ||
|
||
m := sync.Mutex{} | ||
|
||
m.Lock() | ||
if _, err := f.Write(au.Chunk); err != nil { | ||
m.Unlock() | ||
return err | ||
} | ||
m.Unlock() | ||
} | ||
} | ||
|
||
func (a audioServer) Remove(ctx context.Context, request *komputer.NameOrIdAudioRequest) (e *emptypb.Empty, err error) { | ||
e = &emptypb.Empty{} | ||
if request == nil || len(request.Query) == 0 { | ||
return e, nil | ||
} | ||
|
||
var wg sync.WaitGroup | ||
for _, query := range request.Query { | ||
if query == nil { | ||
continue | ||
} | ||
|
||
wg.Add(1) | ||
go func(q *komputer.FileQuery) { | ||
err = remove(ctx, &wg, q) | ||
}(query) | ||
} | ||
wg.Wait() | ||
|
||
return | ||
} | ||
|
||
func remove(ctx context.Context, wg *sync.WaitGroup, query *komputer.FileQuery) error { | ||
defer wg.Done() | ||
select { | ||
case <-ctx.Done(): | ||
return context.Canceled | ||
default: | ||
} | ||
|
||
uid, name := query.GetUuid(), query.GetName() | ||
|
||
var path string | ||
if uid != nil && name != "" { | ||
path = audio.Path(fmt.Sprintf("%s-%s", name, uid.Uuid)) | ||
} | ||
|
||
if path == "" { | ||
return os.ErrNotExist | ||
} | ||
|
||
return os.Remove(path) | ||
} |
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,69 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
komputer "github.com/wittano/komputer/api/proto" | ||
"github.com/wittano/komputer/audio" | ||
"io" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type fileServer struct { | ||
komputer.UnimplementedAudioFileServiceServer | ||
} | ||
|
||
func (fs fileServer) Download(request *komputer.DownloadRequest, server komputer.AudioFileService_DownloadServer) error { | ||
if request == nil { | ||
return errors.New("download: missing request data") | ||
} | ||
|
||
path := audio.Path(filename(request)) | ||
f, err := os.Open(path) | ||
if err != nil { | ||
slog.Error("failed find f "+path, err) | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
ctx := server.Context() | ||
|
||
buf := make([]byte, 1024) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return context.Canceled | ||
default: | ||
} | ||
|
||
n, err := f.Read(buf) | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
if err = server.Send(&komputer.FileBuffer{Content: buf, Size: uint64(n)}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func filename(request *komputer.DownloadRequest) (name string) { | ||
if request == nil { | ||
return | ||
} | ||
|
||
var uuid *komputer.UUID | ||
uuid, name = request.GetUuid(), request.GetName() | ||
|
||
if uuid == nil { | ||
return | ||
} | ||
|
||
return fmt.Sprintf("%s-%s", name, uuid) | ||
} |
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,13 @@ | ||
package server | ||
|
||
import komputer "github.com/wittano/komputer/api/proto" | ||
|
||
func paginationOrDefault(p *komputer.Pagination) *komputer.Pagination { | ||
if p == nil { | ||
return &komputer.Pagination{ | ||
Size: 10, | ||
} | ||
} | ||
|
||
return p | ||
} |
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