-
Notifications
You must be signed in to change notification settings - Fork 1
/
serviceUpload.go
199 lines (179 loc) · 4.99 KB
/
serviceUpload.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"github.com/graphql-go/graphql"
handler "github.com/jpascal/graphql-upload"
uuid "github.com/satori/go.uuid"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
)
var UploadType = graphql.NewScalar(graphql.ScalarConfig{
Name: "Upload",
Description: "Scalar upload object",
})
type FileWrapper struct {
File *os.File
Name string
}
var File = graphql.NewObject(graphql.ObjectConfig{
Name: "File",
Description: "File object",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
file := params.Source.(*FileWrapper)
name := path.Base(file.Name)
return name, nil
},
},
"hash": &graphql.Field{
Type: graphql.String,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
file := params.Source.(*FileWrapper)
if data, err := ioutil.ReadAll(file.File); err == nil {
fileHash := sha1.Sum(data)
return hex.EncodeToString(fileHash[:]), nil
} else {
return nil, err
}
},
},
"size": &graphql.Field{
Type: graphql.Int,
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
file := params.Source.(*FileWrapper)
if info, err := file.File.Stat(); err != nil {
return nil, err
} else {
return info.Size(), nil
}
},
},
},
})
func StartServiceUpload() {
schema, err := graphql.NewSchema(
graphql.SchemaConfig{
Query: graphql.NewObject(
graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"file": &graphql.Field{
Type: File,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
if fileId, ok := params.Args["id"].(string); ok {
fileUuid, err := uuid.FromString(fileId)
if err != nil {
return nil, err
}
file, err := os.Open("tmp/" + fileUuid.String())
if err != nil {
return nil, err
}
return &FileWrapper{File: file, Name: fileUuid.String()}, nil
} else {
return nil, errors.New("file id is not provided")
}
},
},
},
}),
Mutation: graphql.NewObject(
graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"upload": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Args: graphql.FieldConfigArgument{
"file": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(UploadType),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
upload, uploadPresent := params.Args["file"].(handler.File)
if uploadPresent {
id := uuid.NewV4().String()
targetFile, err := os.Create("tmp/" + id)
if err != nil {
return nil, err
}
defer targetFile.Close()
nBytes, err := io.Copy(targetFile, upload.File)
if err != nil {
return nil, err
}
log.Println("File saved nBytes: ", nBytes)
return id, nil
} else {
return nil, errors.New("no file found in request")
}
},
},
"uploadMulti": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(graphql.String))),
Args: graphql.FieldConfigArgument{
"files": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(UploadType))),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
uploads, uploadPresent := params.Args["files"].([]interface{})
if uploadPresent {
var result []string
for i, uploadItem := range uploads {
upload, ok := uploadItem.(handler.File)
if !ok {
return nil, errors.New(fmt.Sprintf("type of file %d is wrong", i))
}
id := uuid.NewV4().String()
targetFile, err := os.Create("tmp/" + id)
if err != nil {
return nil, err
}
defer targetFile.Close()
nBytes, err := io.Copy(targetFile, upload.File)
if err != nil {
return nil, err
}
log.Println("File saved nBytes: ", nBytes)
result = append(result, id)
}
return result, nil
} else {
return nil, errors.New("no file found in request")
}
},
},
},
}),
})
if err != nil {
panic(err)
}
server := &http.Server{Addr: "0.0.0.0:5000", Handler: handler.New(func(request *handler.Request) interface{} {
return graphql.Do(graphql.Params{
RequestString: request.Query,
OperationName: request.OperationName,
VariableValues: request.Variables,
Schema: schema,
Context: request.Context,
})
}, &handler.Config{
MaxBodySize: 1024,
}),
}
server.ListenAndServe()
}