-
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(audio): added bot integration web api audio endpoints
Added integration with GET /api/v1/audio/{id} endpoint to downloading audio file from server. Also bot can save files local as cache
- Loading branch information
Showing
18 changed files
with
270 additions
and
80 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 |
---|---|---|
|
@@ -24,4 +24,9 @@ build/ | |
|
||
.vscode/ | ||
result/ | ||
assets | ||
assets | ||
|
||
# Web files | ||
*.mp3 | ||
*.mp4 | ||
*.yml |
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,59 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"github.com/wittano/komputer/bot/internal/voice" | ||
"io" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
type WebClient struct { | ||
baseURL string | ||
client http.Client | ||
} | ||
|
||
func (c WebClient) DownloadAudio(id string) (path string, err error) { | ||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/v1/audio/%s", c.baseURL, id), nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
res, err := c.client.Do(req) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != 200 { | ||
body, err := io.ReadAll(res.Body) | ||
if err == nil { | ||
err = fmt.Errorf("failed download audio. Response: %s", body) | ||
} | ||
|
||
return "", err | ||
} | ||
|
||
dest := voice.Path(id) | ||
f, err := os.Create(dest) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
if _, err = io.Copy(f, res.Body); err != nil { | ||
return "", err | ||
} | ||
|
||
return dest, nil | ||
} | ||
|
||
func NewClient(url string) *WebClient { | ||
return &WebClient{ | ||
baseURL: url, | ||
client: http.Client{ | ||
Timeout: time.Second * 5, | ||
}, | ||
} | ||
} |
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,25 @@ | ||
package voice | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
const ( | ||
defaultCacheDirAudio = "assets" | ||
CacheDirAudioKey = "CACHE_AUDIO_DIR" | ||
) | ||
|
||
func CheckIfAudioIsDownloaded(id string) (err error) { | ||
_, err = os.Stat(Path(id)) | ||
return | ||
} | ||
|
||
func Path(id string) string { | ||
cachePath := defaultCacheDirAudio | ||
if cacheDir, ok := os.LookupEnv(CacheDirAudioKey); ok && cacheDir != "" { | ||
cachePath = cacheDir | ||
} | ||
|
||
return filepath.Join(cachePath, id+".mp3") | ||
} |
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
Oops, something went wrong.