-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
158 lines (141 loc) · 4.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"database/sql"
"fmt"
"io"
"net/http"
"os"
"path"
"strconv"
"strings"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/voiceip/oreka-api/oreka"
)
var DB *sql.DB
var err error
const recordBasePath = "/var/log/orkaudio/audio/"
func getByCallID(callID string) (*oreka.OrkTape, error) {
var tape oreka.OrkTape
if callID == "" {
return nil, nil
}
results, err := DB.Query("select filename, duration, localParty, remoteParty, timestamp, nativeCallId, state from orktape where `nativeCallId` = ?", callID)
if err != nil {
return nil, err
}
defer results.Close()
for results.Next() {
err := results.Scan(&tape.Filename, &tape.Duration, &tape.LocalParty, &tape.RemoteParty, &tape.Timestamp, &tape.NativeCallID, &tape.CallState)
if err != nil {
return nil, err
}
return &tape, nil
}
return nil, nil
}
func setupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
r := gin.Default()
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
"root": "root",
"admin": "admin",
}))
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
r.GET("/calls/:id", func(c *gin.Context) {
callId := c.Params.ByName("id")
tape, err := getByCallID(callId)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Internal Error", "reason": err.Error()})
} else if tape != nil {
if _, err := os.Stat(recordBasePath + tape.Filename); os.IsNotExist(err) {
c.JSON(http.StatusGone, tape)
} else {
c.JSON(http.StatusOK, tape)
}
} else {
c.JSON(http.StatusNotFound, gin.H{"message": "Not Found", "callId": callId})
}
})
authorized.GET("/play/:id", func(c *gin.Context) {
user := c.MustGet(gin.AuthUserKey).(string)
callID := c.Params.ByName("id")
format := c.DefaultQuery("format", "")
tape, err := getByCallID(callID)
if err == nil && tape != nil {
err = convertMediaFileIfRequired(tape)
}
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"message": "Internal Error", "reason": err.Error()})
} else if tape != nil {
sourceMediaFile := recordBasePath + tape.Filename
serveMediaFile(c, callID, format, sourceMediaFile)
} else {
c.JSON(http.StatusNotFound, gin.H{"user": user, "message": "Not Found", "callId": callID})
}
})
return r
}
func convertMediaFileIfRequired(tape *oreka.OrkTape) error {
sourceMediaFile := recordBasePath + tape.Filename
_, err := os.Stat(sourceMediaFile)
if os.IsNotExist(err) && tape.CallState == "STOP" {
//check if mcfFile is available
mcfMediaFile := sourceMediaFile[0:strings.LastIndex(sourceMediaFile, ".")] + ".mcf"
if _, err := os.Stat(mcfMediaFile); err == nil {
fmt.Printf("Running Transcode on NativeCallID %s, MCF : %s \n", tape.NativeCallID, mcfMediaFile)
err = oreka.OrkaudioTranscode(mcfMediaFile)
if err != nil {
return err
}
}
}
return nil
}
func serveMediaFile(c *gin.Context, callId string, format string, mediaFile string) {
if _, err := os.Stat(mediaFile); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{"message": "Raw file not found.", "callId": callId})
} else {
fileExtension := strings.TrimLeft(path.Ext(mediaFile), ".")
switch format {
case "mp3":
if fileExtension == format {
c.File(mediaFile)
} else {
mp := oreka.MediaProcessor{FileName: mediaFile, ID: &callId}
stream, err := mp.ToMP3()
defer stream.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Internal Error", "error": err.Error()})
} else {
//c.Header("Transfer-Encoding", "chunked")
c.Header("Content-Length", strconv.FormatInt(stream.Size(), 10))
c.Header("Content-Disposition", `inline; filename="`+callId+`.mp3"`)
c.Header("Content-Type", "audio/mp3")
io.Copy(c.Writer, stream)
}
}
case "wav", "ogg", "opus":
if fileExtension == format {
c.File(mediaFile)
} else {
c.JSON(http.StatusUnsupportedMediaType, gin.H{"message": "Unsupported Format"})
}
default:
c.File(mediaFile)
}
}
}
func main() {
DB, err = oreka.SetupDatabase()
if err != nil {
oreka.Die("Unable to Connect to Database", err)
}
r := setupRouter()
r.Run(":9090")
}