forked from smithaitufe/go-graphql-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar.go
76 lines (71 loc) · 1.43 KB
/
scalar.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
package graphqlupload
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
)
type GraphQLUpload struct {
FileName string `json:"filename"`
MIMEType string `json:"mimetype"`
FilePath string `json:"filepath"`
}
func (_ GraphQLUpload) ImplementsGraphQLType(name string) bool { return name == "Upload" }
func (u *GraphQLUpload) UnmarshalGraphQL(input interface{}) error {
switch input := input.(type) {
case map[string]interface{}:
b, err := json.Marshal(input)
if err != nil {
u = &GraphQLUpload{}
} else {
json.Unmarshal(b, u)
}
return nil
default:
return fmt.Errorf("no implementation for the type specified")
}
}
func createReadStream(u *GraphQLUpload) (io.Reader, error) {
f, err := os.Open(u.FilePath)
if err == nil {
return bufio.NewReader(f), nil
}
return nil, err
}
func (u *GraphQLUpload) CreateReadStream() (io.Reader, error) {
return createReadStream(u)
}
func (u *GraphQLUpload) WriteFile(name string) error {
rdr, err := createReadStream(u)
if err != nil {
return err
}
fo, err := os.Create(name)
if err != nil {
return err
}
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
w := bufio.NewWriter(fo)
buf := make([]byte, 1024*1024)
for {
n, err := rdr.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := w.Write(buf[:n]); err != nil {
return err
}
}
if err = w.Flush(); err != nil {
return err
}
return nil
}