-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
102 lines (93 loc) · 2.83 KB
/
post.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
package dicomweb
import (
"bytes"
"errors"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"time"
"github.com/suyashkumar/dicom"
)
func WriteMultipart(dcm_slice []*dicom.Dataset) (*[]byte, string, error) {
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
for _, dcm := range dcm_slice {
part, err := writer.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/dicom"}})
if err != nil {
return &[]byte{}, "", err
}
err = dicom.Write(part, *dcm, dicom.DefaultMissingTransferSyntax(), dicom.SkipVRVerification(), dicom.SkipValueTypeVerification())
if err != nil {
return &[]byte{}, "", err
}
}
params := make(map[string]string)
params["boundary"] = writer.Boundary()
params["type"] = "application/dicom"
content_type := mime.FormatMediaType("multipart/related", params)
b := buf.Bytes()
return &b, content_type, nil
}
func PostMultipart(url string, data *[]byte, headers map[string]string, timeout int) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewReader(*data))
if err != nil {
return &http.Response{}, err
}
for key, element := range headers {
req.Header.Set(key, element)
}
client := &http.Client{Timeout: time.Duration(timeout * 1e9)}
resp, err := client.Do(req)
if err != nil {
return &http.Response{}, err
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
return &http.Response{}, &RequestError{StatusCode: resp.StatusCode, Err: errors.New(resp.Status)}
}
return resp, nil
}
func Stow(url string, dcm_slice []*dicom.Dataset, headers map[string]string, timeout int) (*http.Response, error) {
b, content_type, err := WriteMultipart(dcm_slice)
if err != nil {
return &http.Response{}, err
}
headers["Content-Type"] = content_type
return PostMultipart(url, b, headers, timeout)
}
func WriteMultipartFromFile(dcm_path_slice []string) (*[]byte, string, error) {
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
for _, dcm_path := range dcm_path_slice {
data, err := os.Open(dcm_path)
if err != nil {
return &[]byte{}, "", err
}
defer data.Close()
part, err := writer.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/dicom"}})
if err != nil {
return &[]byte{}, "", err
}
_, err = io.Copy(part, data)
if err != nil {
return &[]byte{}, "", err
}
}
params := make(map[string]string)
params["boundary"] = writer.Boundary()
params["type"] = "application/dicom"
content_type := mime.FormatMediaType("multipart/related", params)
b := buf.Bytes()
return &b, content_type, nil
}
func StowFromFile(url string, dcm_path_slice []string, headers map[string]string, timeout int) (*http.Response, error) {
b, content_type, err := WriteMultipartFromFile(dcm_path_slice)
if err != nil {
return &http.Response{}, err
}
headers["Content-Type"] = content_type
return PostMultipart(url, b, headers, timeout)
}