-
Notifications
You must be signed in to change notification settings - Fork 15
/
exportcsv.go
122 lines (94 loc) · 3.21 KB
/
exportcsv.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
// Copyright 2016 Takbok, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package demo
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
newappengine "google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
"appengine"
"appengine/datastore"
)
func init() {
http.HandleFunc("/export", handleContacts)
http.HandleFunc("/contacts", handleContacts)
http.HandleFunc("/contacts/export", handleContactsExport)
}
func isUrlOnGoogleApp(writer http.ResponseWriter, request *http.Request, url string) bool {
ctx := newappengine.NewContext(request)
client := urlfetch.Client(ctx)
uri := fmt.Sprintf("https://www.google.com/a/%s/ServiceLogin", url)
resp, err := client.Get(uri)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return false
}
body, err := ioutil.ReadAll(resp.Body)
responseBody := string(body[:])
// Check if the Google Apps login URL is valid
return strings.Contains(responseBody, "https://www.google.com/accounts/AccountChooser")
}
func handleContacts(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Redirect(w, r, "/?error=noDirectAccess", http.StatusTemporaryRedirect)
return
}
url, err := getProperDomainNameFromUrl(r.FormValue("url"))
if err != nil {
http.Redirect(w, r, "/?error=badUrl", http.StatusTemporaryRedirect)
return
}
if !isUrlOnGoogleApp(w, r, url) {
http.Redirect(w, r, "/?error=notOnGoogleApps", http.StatusTemporaryRedirect)
return
}
ctx := appengine.NewContext(r)
config.RedirectURL = fmt.Sprintf(`http://%s/contacts/export`, r.Host)
x := AppState{url}
url = config.AuthCodeURL(x.encodeState())
ctx.Infof("Auth: %v", url)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handleContactsExport(w http.ResponseWriter, r *http.Request) {
y := r.FormValue("state")
state := new(AppState)
state.decodeState(y)
w.Header().Set(`Content-Type`, `application/csv`)
w.Header().Set(`Content-Disposition`, `attachment; filename="`+state.Domain+`-contacts-export.csv"`)
ctx := appengine.NewContext(r)
buf := loadFullFeed(state.Domain, ctx, r)
ctx.Infof("%v", buf.String())
writeCSV(ctx, w, buf.Bytes())
}
func handleExport(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
d := &ImportData{}
k := datastore.NewKey(ctx, "ImportData", "ImportedFeed", 0, nil)
w.Header().Set("Content-Type", "application/csv")
if err := datastore.Get(ctx, k, d); err != nil {
ctx.Errorf("get: %v", err)
} else {
writeCSV(ctx, w, d.Data)
}
}
func getProperDomainNameFromUrl(u string) (string, error) {
uri, err := url.Parse(u)
if err != nil {
return ``, fmt.Errorf("The URL seems to be invalid")
}
p := strings.TrimPrefix(uri.Host, "www.")
return p, nil
}