-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (141 loc) · 4.8 KB
/
Copy pathmain.go
File metadata and controls
171 lines (141 loc) · 4.8 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
const (
numChannels = 1 // Mono audio
sampleRate = 16000
bitsPerSample = 16 // 16 bits per sample
)
// CreateWAVHeader generates a WAV header for the given data length
func createWAVHeader(dataLength int) []byte {
byteRate := sampleRate * numChannels * bitsPerSample / 8
blockAlign := numChannels * bitsPerSample / 8
header := make([]byte, 44)
copy(header[0:4], []byte("RIFF"))
binary.LittleEndian.PutUint32(header[4:8], uint32(36+dataLength))
copy(header[8:12], []byte("WAVE"))
copy(header[12:16], []byte("fmt "))
binary.LittleEndian.PutUint32(header[16:20], 16)
binary.LittleEndian.PutUint16(header[20:22], 1)
binary.LittleEndian.PutUint16(header[22:24], uint16(numChannels))
binary.LittleEndian.PutUint32(header[24:28], uint32(sampleRate))
binary.LittleEndian.PutUint32(header[28:32], uint32(byteRate))
binary.LittleEndian.PutUint16(header[32:34], uint16(blockAlign))
binary.LittleEndian.PutUint16(header[34:36], bitsPerSample)
copy(header[36:40], []byte("data"))
binary.LittleEndian.PutUint32(header[40:44], uint32(dataLength))
return header
}
func uploadFileToGCS(bucketName string, fileName string, filePath string) error {
ctx := context.Background()
// Create a storage client using the service account credentials from the environment variable
credsEnv := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
if credsEnv == "" {
return fmt.Errorf("GOOGLE_APPLICATION_CREDENTIALS_JSON environment variable is not set")
}
// Decode the base64 encoded credentials
creds, err := base64.StdEncoding.DecodeString(credsEnv)
if err != nil {
return fmt.Errorf("failed to decode credentials: %v", err)
}
// Create a temporary file for the credentials
credsFile, err := os.CreateTemp("", "gcs-creds-*.json")
if err != nil {
return fmt.Errorf("failed to create temp file for credentials: %v", err)
}
defer os.Remove(credsFile.Name())
if _, err := credsFile.Write(creds); err != nil {
return fmt.Errorf("failed to write credentials to temp file: %v", err)
}
credsFile.Close()
// Create a storage client
client, err := storage.NewClient(ctx, option.WithCredentialsFile(credsFile.Name()))
if err != nil {
return fmt.Errorf("failed to create storage client: %v", err)
}
defer client.Close()
bucket := client.Bucket(bucketName)
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer f.Close()
wc := bucket.Object(fileName).NewWriter(ctx)
wc.ContentType = "audio/wav"
if _, err = io.Copy(wc, f); err != nil {
return fmt.Errorf("failed to write to bucket: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("failed to close Writer: %v", err)
}
log.Printf("File %s uploaded to GCS bucket %s successfully.", fileName, bucketName)
return nil
}
func handlePostAudio(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
sampleRateParam := query.Get("sample_rate")
uid := query.Get("uid")
log.Printf("Received request from uid: %s", uid)
log.Printf("Requested sample rate: %s", sampleRateParam)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusBadRequest)
return
}
defer r.Body.Close()
currentTime := time.Now()
filename := fmt.Sprintf("%02d_%02d_%04d_%02d_%02d_%02d.wav",
currentTime.Day(),
currentTime.Month(),
currentTime.Year(),
currentTime.Hour(),
currentTime.Minute(),
currentTime.Second())
tempFilePath := filepath.Join(os.TempDir(), filename)
header := createWAVHeader(len(body))
// Write to temporary file
tempFile, err := os.Create(tempFilePath)
if err != nil {
log.Printf("Failed to create temp file: %v", err)
http.Error(w, "Failed to create temp file", http.StatusInternalServerError)
return
}
defer tempFile.Close()
// Write WAV header and audio data
tempFile.Write(header)
tempFile.Write(body)
// Get bucket name from environment variable
bucketName := os.Getenv("GCS_BUCKET_NAME")
if bucketName == "" {
log.Printf("GCS_BUCKET_NAME environment variable is not set")
http.Error(w, "GCS_BUCKET_NAME environment variable is not set", http.StatusInternalServerError)
return
}
// Upload the file to Google Cloud Storage
err = uploadFileToGCS(bucketName, filename, tempFilePath)
if err != nil {
log.Printf("Failed to upload to GCS: %v", err)
http.Error(w, "Failed to upload to Google Cloud Storage", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Audio bytes received and uploaded as %s", filename)))
}
func main() {
http.HandleFunc("/audio", handlePostAudio)
port := "8080"
log.Printf("Server starting on port %s...", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}