-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeplink.go
109 lines (89 loc) · 1.93 KB
/
deeplink.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
package deeplink
import (
"fmt"
"github.com/michaelfemi81/go-inliner"
"os"
"strings"
"net/http"
"html/template"
"errors"
"path"
"runtime"
)
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
type DeepLink struct {
Android_package_name string
Title string
Ios_store_link string
Fallback string
Url string
}
func (d *DeepLink) Init(f string, t... string) (error) {
d.Fallback = f
if (strings.Trim(f, " ") == "") {
return errors.New("Invalid Fallback");
}
if (len(t) < 1) {
return errors.New("Set At Least Android Package");
} else {
if (len(t) == 1) {
d.Android_package_name = t[0]
d.Ios_store_link = ""
d.Title = ""
} else if (len(t) == 2) {
d.Android_package_name = t[0]
d.Ios_store_link = t[1]
d.Title = ""
} else {
d.Android_package_name = t[0]
d.Ios_store_link = t[1]
d.Title = t[2]
}
}
return nil
}
func(d DeepLink)DoDeepLink(w http.ResponseWriter, r *http.Request)(err error){
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
//fmt.Printf("Filename : %q, Dir : %q\n", filename, path.Dir(filename))
url:=r.URL.Query()["url"];
//fmt.Println(d.Title)
if(url==nil){
err=errors.New("no query uri");
fmt.Fprintln(w,"Invalid Query String");
return
}else{
d.Url=url[0]
t := template.New("index.html")
t,err:=t.ParseFiles(path.Dir(filename)+"/public/index.html");
fil, err := os.OpenFile(path.Dir(filename)+"/public/aa.html", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
// os.Exit(1)
}else{
err=t.Execute(fil,d);
if err != nil {
fmt.Println(err)
// os.Exit(1)
}else{
inliner.RenderToHttp(path.Dir(filename)+"/public/aa.html",w,r);
}
}
defer remove(path.Dir(filename)+"/public/aa.html")
defer fil.Close()
}
return
}
func remove(aa string){
err3 := os.Remove(aa)
if err3 != nil {
fmt.Println(err3)
}
}