-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
temp.go
49 lines (42 loc) · 1.11 KB
/
temp.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
package transcoder
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func (h *Handler) createTempFile(w http.ResponseWriter, r *http.Request, parts []string) (string, error) {
streamid := parts[0]
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Error reading body", err)
w.WriteHeader(http.StatusInternalServerError)
return "", err
}
// Create temporary file
file, err := ioutil.TempFile(h.c.TempDir, streamid+"-govod-temp-")
if err != nil {
log.Println("Error creating temp file", err)
w.WriteHeader(http.StatusInternalServerError)
return "", err
}
defer file.Close()
// Write data to file
if _, err := file.Write(body); err != nil {
log.Println("Error writing to temp file", err)
w.WriteHeader(http.StatusInternalServerError)
return "", err
}
// Return full path to file in JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"path": file.Name()})
// Return path to file
return file.Name(), nil
}
func freeIfTemp(path string) {
if strings.Contains(path, "-govod-temp-") {
os.Remove(path)
}
}