-
Notifications
You must be signed in to change notification settings - Fork 63
/
files_common.go
87 lines (76 loc) · 2.45 KB
/
files_common.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
package pubnub
// PNPublishMessage is the part of the message struct used in Publish File
type PNPublishMessage struct {
Text string `json:"text"`
}
// PNFileInfoForPublish is the part of the message struct used in Publish File
type PNFileInfoForPublish struct {
Name string `json:"name"`
ID string `json:"id"`
}
// PNPublishFileMessage is the message struct used in Publish File
type PNPublishFileMessage struct {
PNMessage *PNPublishMessage `json:"message"`
PNFile *PNFileInfoForPublish `json:"file"`
}
// PNFileInfo is the File Upload API struct returned on for each file.
type PNFileInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Size int `json:"size"`
Created string `json:"created"`
}
// PNFileData is used in the responses to show File ID
type PNFileData struct {
ID string `json:"id"`
}
// PNFileUploadRequest is used to store the info related to file upload to S3
type PNFileUploadRequest struct {
URL string `json:"url"`
Method string `json:"method"`
FormFields []PNFormField `json:"form_fields"`
}
// PNFormField is part of the struct used in file upload to S3
type PNFormField struct {
Key string `json:"key"`
Value string `json:"value"`
}
// PNFileDetails is used in the responses to show File Info
type PNFileDetails struct {
Name string `json:"name"`
ID string `json:"id"`
URL string
}
// PNFileMessageAndDetails is used to store the file message and file info
type PNFileMessageAndDetails struct {
PNMessage PNPublishMessage `json:"message"`
PNFile PNFileDetails `json:"file"`
}
// ParseFileInfo is a function extract file info and add to the struct PNFileMessageAndDetails
func ParseFileInfo(filesPayload map[string]interface{}) (PNFileDetails, PNPublishMessage) {
var data map[string]interface{}
resp := &PNFileMessageAndDetails{}
resp.PNMessage = PNPublishMessage{}
resp.PNFile = PNFileDetails{}
//"message":{"text":"test file"},"file":{"name":"test_file_upload_name_32899","id":"9076246e-5036-42af-b3a3-767b514c93c8"}}
if o, ok := filesPayload["file"]; ok {
if o != nil {
data = o.(map[string]interface{})
if d, ok := data["id"]; ok {
resp.PNFile.ID = d.(string)
}
if d, ok := data["name"]; ok {
resp.PNFile.Name = d.(string)
}
}
}
if m, ok := filesPayload["message"]; ok {
if m != nil {
data = m.(map[string]interface{})
if d, ok := data["text"]; ok {
resp.PNMessage.Text = d.(string)
}
}
}
return resp.PNFile, resp.PNMessage
}