diff --git a/internal/pkg/api/api.go b/internal/pkg/api/api.go index 40eca92..9b37abf 100644 --- a/internal/pkg/api/api.go +++ b/internal/pkg/api/api.go @@ -8,6 +8,7 @@ import ( "msw-open-music/internal/pkg/database" "net/http" "os" + "os/exec" "strconv" ) @@ -283,6 +284,46 @@ type GetFileRequest struct { ID int64 `json:"id"` } +func (api *API) HandleGetFileStream(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + ids := q["id"] + if len(ids) == 0 { + api.HandleErrorString(w, r, `parameter "id" can't be empty`) + return + } + id, err := strconv.Atoi(ids[0]) + if err != nil { + api.HandleErrorString(w, r, `parameter "id" should be an integer`) + return + } + file, err := api.Db.GetFile(int64(id)) + if err != nil { + api.HandleError(w, r, err) + return + } + + path, err := file.Path() + if err != nil { + api.HandleError(w, r, err) + return + } + + cmd := exec.Command("ffmpeg", + "-i", path, + "-c:a", "libopus", + "-ab", "128k", + "-vn", + "-f", "matroska", + "-", + ) + cmd.Stdout = w + err = cmd.Run() + if err != nil { + api.HandleError(w, r, err) + return + } +} + func (api *API) HandleGetFileDirect(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() ids := q["id"] @@ -375,6 +416,7 @@ func NewAPI(dbName string, Addr string) (*API, error) { apiMux.HandleFunc("/search_folders", api.HandleSearchFolders) apiMux.HandleFunc("/get_files_in_folder", api.HandleGetFilesInFolder) apiMux.HandleFunc("/get_random_files", api.HandleGetRandomFiles) + apiMux.HandleFunc("/get_file_stream", api.HandleGetFileStream) // below needs token apiMux.HandleFunc("/walk", api.HandleWalk) apiMux.HandleFunc("/reset", api.HandleReset) diff --git a/web/index.js b/web/index.js index 9933200..553ffd9 100644 --- a/web/index.js +++ b/web/index.js @@ -199,6 +199,7 @@ const component_file = { + `, data() { @@ -208,8 +209,13 @@ const component_file = { } }, methods: { + emit_stream_audio() { + this.file.play_back_type = 'stream', + this.$emit("play_audio", this.file) + }, emit_play_audio() { console.log("pressed button") + this.file.play_back_type = 'raw' this.$emit("play_audio", this.file) }, download_file(file) { @@ -274,7 +280,11 @@ const component_audio_player = { `, computed: { computed_playing_audio_file_url() { - return '/api/v1/get_file_direct?id=' + this.file.id + if (this.file.play_back_type === 'raw') { + return '/api/v1/get_file_direct?id=' + this.file.id + } else if (this.file.play_back_type === 'stream') { + return '/api/v1/get_file_stream?id=' + this.file.id + } }, computed_show() { return this.file.id ? true : false