-
Notifications
You must be signed in to change notification settings - Fork 0
/
finish.go
126 lines (103 loc) · 2.71 KB
/
finish.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
// Copyright (c) Bluebeam Inc. All rights reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
package main
import (
"errors"
"fmt"
"html/template"
"net/http"
"strconv"
"time"
)
func finishPage(w http.ResponseWriter, r *http.Request) {
client := getOAuthClient(r.Context())
sessionID := r.FormValue("sessionId")
projectID := r.FormValue("projectId")
fileSessionID, _ := strconv.ParseInt(r.FormValue("fileSessionId"), 10, 32)
fileProjectID, _ := strconv.ParseInt(r.FormValue("fileProjectId"), 10, 32)
// Set Session to Finalizing to boot people
_, err := setSessionStatus(client, sessionID, "Finalizing")
if err != nil {
redirectToError(w, r, err)
return
}
// Initiate Snapshot
err = startSnapshot(client, sessionID, int(fileSessionID))
if err != nil {
redirectToError(w, r, err)
return
}
// Poll the snapshot status every 5 seconds until complete or an error
var snapshotResponse *SnapshotResponse
outer:
for {
snapshotResponse, err = getSnapshotStatus(client, sessionID, int(fileSessionID))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(snapshotResponse)
switch snapshotResponse.Status {
case "Complete":
break outer
case "Error":
redirectToError(w, r, errors.New("Shapshot error"))
return
}
time.Sleep(5 * time.Second)
}
// Download Snapshot
resp, err := http.Get(snapshotResponse.DownloadURL)
if err != nil {
redirectToError(w, r, err)
return
}
fileReader := resp.Body
// Delete Session
err = sessionDelete(client, sessionID)
if err != nil {
redirectToError(w, r, err)
return
}
// Start checkin
projectFilesResponse, err := checkinProjectFile(client, projectID, int(fileProjectID))
if err != nil {
redirectToError(w, r, err)
return
}
// Upload new revision to Aws
err = uploadToAWS(projectFilesResponse, fileReader, resp.ContentLength)
if err != nil {
redirectToError(w, r, err)
return
}
// Confirm checkin
err = confirmProjectCheckin(client, projectID, int(fileProjectID), "Checkin from Roundtripper")
if err != nil {
redirectToError(w, r, err)
return
}
// Kick off job to flatten the file
_, err = flattenProjectFile(client, projectID, int(fileProjectID))
if err != nil {
redirectToError(w, r, err)
return
}
// Generate a share link to the file
sharedLinkResponse, err := getSharedLink(client, projectID, int(fileProjectID))
if err != nil {
redirectToError(w, r, err)
return
}
html, err := Asset("assets/finish.html")
if err != nil {
redirectToError(w, r, err)
return
}
t, _ := template.New("finishSession").Parse(string(html))
finishSessionData := struct {
ProjectLink string
}{ProjectLink: sharedLinkResponse.ShareLink}
t.Execute(w, finishSessionData)
}