-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (114 loc) · 3.42 KB
/
main.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
package main
import (
"fmt"
"os"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "send name and pass and url to /load use post")
})
router.POST("/load", Load)
router.GET("/helloGET", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello "+c.Query("name")+" from GET!"})
})
router.POST("/helloPOST", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello "+c.PostForm("name")+" from POST!"})
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
router.Run(":"+port)
}
func Load(c *gin.Context) {
filename, err := get_pdf(c.PostForm("url"), c.PostForm("filename"))
if err != nil {
c.String(http.StatusInternalServerError, "server error")
}
err = send_pdf(c.PostForm("token"), filename)
if err != nil {
c.String(http.StatusInternalServerError, "server error")
}
err = del_file(filename)
if err != nil {
c.String(http.StatusInternalServerError, "server error")
}
c.String(http.StatusOK, filename+"is done")
}
func get_pdf(URL,name string) (string, error){
client := &http.Client{}
// API Explorer | API情報サイト
// https://dropbox.github.io/dropbox-api-v2-explorer/#files_download
url := URL
filename := name+".pdf"
// Create Request | リクエスト作成
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
// Send Request | 送信
res, err := client.Do(req)
if err != nil {
os.Exit(1)
return "", err
}
// logging Status | ステータスをロギング
fmt.Println("status:", res.Status)
// Read Body(=File's Binary) | レスポンス(ファイルのバイナリ)を取得
body, err := ioutil.ReadAll(res.Body)
if err != nil {
os.Exit(1)
return "", err
}
// Create Blank File | 空ファイルを作成
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return "", err
}
// Finally Close File | deferでメソッドの最後にファイルを閉じる予約を行う
defer func() {
file.Close()
}()
// Fill File with Body(=File's Binary) | 空ファイルにバイナリを書き込む
file.Write(body)
return filename, nil
}
func send_pdf(token, filename string) (err error){
//150MB以下のみ
type Send_json struct {
Path string `json:"path"`
Mode string `json:"mode"`
}
client := &http.Client{}
url := "https://content.dropboxapi.com/2/files/upload"
//param := `{ "path": "/"+filename, "mode": "add", "autorename": true, "mute": false, "strict_conflict": false}`
param, err := json.Marshal(Send_json{Path:"/"+filename, Mode:"add"})
if err != nil {
return
}
file, err := ioutil.ReadFile(filename)
if err != nil {
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(file))
if err != nil {
return
}
// Setup Header | ヘッダを登録
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Dropbox-API-Arg", string(param))
req.Header.Set("Content-Type", "application/octet-stream")
res, err := client.Do(req)
fmt.Println("status:", res.Status)
return err
}
func del_file(filename string) (err error){
err = os.Remove(filename)
return
}