-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
138 lines (119 loc) · 2.72 KB
/
Copy pathhttp.go
File metadata and controls
138 lines (119 loc) · 2.72 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
//
// http.go
//
// Created by Frederic DELBOS - fred@hyperboloide.com on Feb 8 2015.
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
//
package sprocess
import (
"errors"
"github.com/dchest/uniuri"
"io"
"net/http"
)
type HTTP struct {
initialized bool
Encoders []Encoder
Decoders []Decoder
Input Inputer
Output Outputer
Delete Deleter
}
func GenId() string {
return uniuri.New()
}
func badRequest(w http.ResponseWriter) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
func internalError(w http.ResponseWriter) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
func (h *HTTP) Encode(w http.ResponseWriter, r *http.Request, id string) (map[string]interface{}, error) {
if h.Output == nil {
internalError(w)
return nil, errors.New("No Output provided")
}
mr, err := r.MultipartReader()
if err != nil {
badRequest(w)
return nil, err
}
for {
part, err := mr.NextPart()
if err == io.EOF {
badRequest(w)
return nil, err
}
filename := part.FileName()
if filename == "" {
continue
} else {
data := NewData()
data.Set("filename", filename)
service := &Service{
EncodingPipe: &EncodingPipeline{
Encoders: h.Encoders,
Output: h.Output,
},
}
if err = service.Encode(id, part, data); err != nil {
internalError(w)
return nil, err
}
w.WriteHeader(http.StatusCreated)
return data.Export(), nil
}
}
badRequest(w)
return nil, errors.New("No file in request")
}
func (h *HTTP) Decode(w http.ResponseWriter, r *http.Request, dataMap map[string]interface{}) error {
if h.Input == nil {
internalError(w)
return errors.New("No Input provided")
}
service := &Service{
DecodingPipe: &DecodingPipeline{
Decoders: h.Decoders,
Input: h.Input,
},
}
data := NewDataFrom(dataMap)
idIf, err := data.Get("identifier")
if err != nil {
internalError(w)
return err
}
id, ok := idIf.(string)
if ok == false {
internalError(w)
return errors.New("Key 'identifier' is not a string")
}
pr, pw := io.Pipe()
go io.Copy(w, pr)
if err := service.Decode(id, pw, data); err != nil {
internalError(w)
return err
}
return nil
}
func (h *HTTP) Remove(w http.ResponseWriter, r *http.Request, dataMap map[string]interface{}) error {
data := NewDataFrom(dataMap)
idIf, err := data.Get("identifier")
if err != nil {
internalError(w)
return err
}
id, ok := idIf.(string)
if ok == false {
internalError(w)
return errors.New("Key 'identifier' is not a string")
}
if err := h.Delete.Delete(id, data); err != nil {
internalError(w)
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}