/
session.go
269 lines (219 loc) · 7.36 KB
/
session.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package fbvideo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path"
"strconv"
"strings"
)
//PrivacyValue Determines the privacy settings of the video.
// If not supplied, this defaults to the privacy level granted to the app in the Login Dialog.
// This field cannot be used to set a more open privacy setting than the one granted.
type PrivacyValue string
const (
PrivacyEveryOne PrivacyValue = "EVERYONE"
PrivacyAllFriends PrivacyValue = "ALL_FRIENDS"
PrivacyFriendsOfFriends PrivacyValue = "FRIENDS_OF_FRIENDS"
PrivacyCustom PrivacyValue = "CUSTOM"
PrivacySelf PrivacyValue = "SELF"
)
type Privacy struct {
Value PrivacyValue `json:"value"`
Allow string `json:"allow"`
Deny string `json:"deny"`
}
func (p *Privacy) JSON() string {
d, _ := json.Marshal(p)
return string(d)
}
type Option struct {
Privacy *Privacy
}
// UploadSession facebook upload session struct
type UploadSession struct {
// ID of a fb resource, possible value are user, page, event, group.
ID string
// AccessToken the token has permission to upload video to the fb resource.
AccessToken string
// Endpoint url to upload video
Endpoint string
Transport http.RoundTripper
// FilePath location of the file in disk
FilePath string
fileName string
fileExt string
file *os.File
fileSize int64
fileChunkFolder string
fileChunkNumber uint
uploadSessionID string
}
// NewUploadSession create a new fb upload session.
func NewUploadSession(filePath string, fbResourceID string, accessToken string) *UploadSession {
uploadSession := &UploadSession{
ID: fbResourceID,
AccessToken: accessToken,
Endpoint: fmt.Sprintf("https://graph-video.facebook.com/v2.6/%s/videos", fbResourceID),
Transport: http.DefaultTransport,
FilePath: filePath,
}
file, err := os.Open(uploadSession.FilePath)
if err != nil {
panic(err)
}
fileInfo, err := file.Stat()
if err != nil {
panic(err)
}
uploadSession.file = file
uploadSession.fileExt = path.Ext(uploadSession.FilePath)
uploadSession.fileName = strings.TrimSuffix(path.Base(uploadSession.FilePath), uploadSession.fileExt)
uploadSession.fileSize = fileInfo.Size()
return uploadSession
}
// Upload upload the file to fb server
func (uploadSession *UploadSession) Upload(option Option) (string, error) {
defer uploadSession.file.Close()
session, err := uploadSession.initialize(option)
if err != nil {
return "", err
}
uploadSession.fileChunkFolder = os.TempDir() + string(os.PathSeparator) + "fb" + session.UploadSessionID
if err := os.MkdirAll(uploadSession.fileChunkFolder, 0777); err != nil {
return "", err
}
defer os.RemoveAll(uploadSession.fileChunkFolder)
if err := uploadSession.uploadChunk(session.UploadSessionID, session.ChunkOffset); err != nil {
return "", err
}
if err := uploadSession.finish(session.UploadSessionID, option); err != nil {
return "", err
}
return session.VideoID, nil
}
// Initialize Create a new upload session
func (uploadSession *UploadSession) initialize(option Option) (*SessionInfo, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
writer.WriteField("access_token", uploadSession.AccessToken)
writer.WriteField("upload_phase", "start")
writer.WriteField("file_size", fmt.Sprintf("%d", uploadSession.fileSize))
if option.Privacy != nil {
writer.WriteField("privacy", option.Privacy.JSON())
}
writer.Close()
req, _ := http.NewRequest(http.MethodPost, uploadSession.Endpoint, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
res, err := uploadSession.Transport.RoundTrip(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("Can not create new upload session, got error %s", NewErrorFromBody(res.Body).Struct.Message)
}
var sessionInfo SessionInfo
if err := json.NewDecoder(res.Body).Decode(&sessionInfo); err != nil {
return nil, err
}
return &sessionInfo, nil
}
// uploadChunk transfer chunk file to facebook server
func (uploadSession *UploadSession) uploadChunk(uploadSessionID string, chunkOffset *ChunkOffset) error {
chunkInfo, err := uploadSession.createNewChunk(uploadSessionID, chunkOffset)
if err != nil {
return err
}
defer os.Remove(chunkInfo.Path)
req, _ := http.NewRequest(http.MethodPost, uploadSession.Endpoint, chunkInfo.Body)
req.Header.Add("Content-Type", chunkInfo.ContentType)
req.Header.Add("Content-Length", fmt.Sprintf("%d", uploadSession.fileSize))
res, err := uploadSession.Transport.RoundTrip(req)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("Can not upload chunk file, got error %s", NewErrorFromBody(res.Body).Struct.Message)
}
var newChunkOffset ChunkOffset
if err := json.NewDecoder(res.Body).Decode(&newChunkOffset); err != nil {
return err
}
if newChunkOffset.StartOffset != newChunkOffset.EndOffset {
os.Remove(chunkInfo.Path)
return uploadSession.uploadChunk(uploadSessionID, &newChunkOffset)
}
return nil
}
// Finish finish the upload session and post the uploaded video to fb resource.
func (uploadSession *UploadSession) finish(uploadSessionID string, option Option) error {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
writer.WriteField("access_token", uploadSession.AccessToken)
writer.WriteField("upload_phase", "finish")
writer.WriteField("upload_session_id", uploadSessionID)
if option.Privacy != nil {
writer.WriteField("privacy", option.Privacy.JSON())
}
writer.Close()
req, _ := http.NewRequest(http.MethodPost, uploadSession.Endpoint, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
res, err := uploadSession.Transport.RoundTrip(req)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("Can not post uploaded video, got error %s", NewErrorFromBody(res.Body).Struct.Message)
}
return nil
}
func (session *UploadSession) createNewChunk(uploadSessionID string, chunkOffset *ChunkOffset) (*ChunkInfo, error) {
session.fileChunkNumber++
startOffset, _ := strconv.ParseInt(chunkOffset.StartOffset, 10, 64)
endOffset, _ := strconv.ParseInt(chunkOffset.EndOffset, 10, 64)
if endOffset > session.fileSize {
endOffset = session.fileSize
}
fileChunkName := fmt.Sprintf("@chunk%d%s", session.fileChunkNumber, session.fileExt)
fileChunkPath := session.fileChunkFolder + "/" + fileChunkName
fileChunkBytes := make([]byte, endOffset-startOffset)
if _, err := session.file.Read(fileChunkBytes); err != nil && err != io.EOF {
return nil, err
}
fileChunk, err := os.Create(fileChunkPath)
if err != nil {
return nil, err
}
if _, err := fileChunk.Write(fileChunkBytes); err != nil {
fileChunk.Close()
return nil, err
}
fileChunk.Close()
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("video_file_chunk", fileChunkName)
if err != nil {
return nil, err
}
if _, err := part.Write(fileChunkBytes); err != nil {
return nil, err
}
writer.WriteField("access_token", session.AccessToken)
writer.WriteField("upload_session_id", uploadSessionID)
writer.WriteField("upload_phase", "transfer")
writer.WriteField("start_offset", chunkOffset.StartOffset)
if err = writer.Close(); err != nil {
fmt.Println(err)
return nil, err
}
chunkInfo := &ChunkInfo{
Path: fileChunkPath,
Body: body,
ContentType: writer.FormDataContentType(),
}
return chunkInfo, nil
}