-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
47 lines (40 loc) · 1.03 KB
/
Copy pathutil.go
File metadata and controls
47 lines (40 loc) · 1.03 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
package assemble
import (
"context"
"encoding/json"
"net/http"
"os"
)
type progressResponse struct {
CurrentChunks int64 `json:"have"`
ExpectedChunks int64 `json:"want"`
RejectedError *string `json:"error,omitempty"`
}
type errorResponse struct {
Error string `json:"error"`
}
func badRequest(w http.ResponseWriter, err error) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(errorResponse{
Error: err.Error(),
})
}
type contextKey string
func GetFileMetadata(r *http.Request) map[string]interface{} {
m := r.Context().Value(contextKey("metadata"))
return m.(map[string]interface{})
}
func RejectFile(r *http.Request, status int, reason string) {
ctx := r.Context()
ctx = context.WithValue(ctx, contextKey("error-code"), status)
ctx = context.WithValue(ctx, contextKey("error-message"), reason)
*r = *r.WithContext(ctx)
}
func getFileSize(path string) (int64, error) {
f, err := os.Stat(path)
if err != nil {
return 0, err
}
return f.Size(), nil
}