This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
fileio.go
194 lines (168 loc) · 4.97 KB
/
fileio.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/gabriel-vasile/mimetype"
gonanoid "github.com/matoous/go-nanoid"
"github.com/codenoid/file.io/storage"
)
var box *rice.Box
var fs http.Handler
var indexHTML []byte
var stg storage.StorageHandler
func main() {
box = rice.MustFindBox("web")
fs = http.FileServer(box.HTTPBox())
indexHTML, _ = box.Bytes("views/index.html")
database := os.Getenv("DATABASE")
if database == "" {
panic("empty DATABASE env, example value: redis://127.0.0.1:27017, badger:/path/to/folder;")
}
conn, err := storage.Connect(database)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
stg = conn
// runtime test
if err := stg.Set("test", []byte("test"), 1*time.Second); err != nil {
fmt.Println("runtime test failed")
panic(err)
}
fmt.Println("starting server on *:8080")
// start listen
fmt.Println(http.ListenAndServe(":8080", http.HandlerFunc(Index)))
}
// Index for testing
func Index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// Serve static file
if len(r.URL.Path) > 7 {
if r.URL.Path[:7] == "/static" {
fs.ServeHTTP(w, r)
return
}
}
// show index.html
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html")
w.Write(indexHTML)
return
}
// has file id
fileID := strings.ReplaceAll(r.URL.Path, "/", "")
if len(fileID) == 6 {
// get file from database
bytes, err := stg.Get(fileID)
if err == nil {
drop := false
// download rate limiting, check if the quota are still sufficient
quotaByte, err := stg.Get("mg-" + fileID)
if err == nil {
quota, err := strconv.Atoi(string(quotaByte))
if err == nil {
// fix this
stg.Set("mg-"+fileID, []byte(strconv.Itoa(quota-1)), 0)
if quota <= 1 {
drop = true
}
}
}
// set Content-Disposition header if fn-<file-id> are exist
filename, err := stg.Get("fn-" + fileID)
if err == nil {
w.Header().Set("Content-Disposition", "attachment; filename="+string(filename))
if strings.Contains(string(filename), ".apk") {
w.Header().Set("Content-Type", "application/vnd.android.package-archive")
} else {
w.Header().Set("Content-Type", mimetype.Detect(bytes).String())
}
}
// write file []byte as response
w.Write(bytes)
if drop {
// Delete file from storage
stg.Del(fileID)
stg.Del("fn-" + fileID)
stg.Del("mg-" + fileID)
}
return
}
stg.Del("mg-" + fileID)
}
w.WriteHeader(404)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"success": false, "error": 404, "message": "file not found"} `))
return
}
if r.Method == "POST" {
fileExp := 30 * time.Minute // in minute
fileExpStr := r.URL.Query().Get("exp")
if fileExpStr != "" {
duration, err := time.ParseDuration(fileExpStr)
if err != nil {
w.Write([]byte(`{"success": false, "error": 402, "message": "A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as '300ms', '-1.5h' or '2h45m'. Valid time units are 'ns', 'us' (or 'µs'), 'ms', 's', 'm', 'h'. "} `))
return
}
fileExp = duration
}
maxDownload := 1
maxDownloadStr := r.URL.Query().Get("max")
if maxDownloadStr != "" {
maxInt, err := strconv.Atoi(maxDownloadStr)
if err != nil {
w.Write([]byte(`{"success": false, "error": 402, "message": "max download must be digit only"} `))
return
}
maxDownload = maxInt
}
r.ParseMultipartForm(1000 << 20)
// FormFile returns the first file for the given key `file`
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, fileHeader, err := r.FormFile("file")
if err != nil {
w.Write([]byte(fmt.Sprintf(`{"success": false, "error": 402, "message": "%v"}`, err.Error())))
return
}
defer file.Close()
// read all of the contents of our uploaded file into a
// byte array, fix this
fileBytes, err := ioutil.ReadAll(file)
if err == nil {
// generate random but short unique based on nanoid
// the payload are AiUeO69 with length 6
id, err := gonanoid.Generate("AiUeO69", 6)
if err == nil {
// set file content with id as key
err = stg.Set(id, fileBytes, fileExp)
if err == nil {
// set file max get / read
stg.Set("mg-"+id, []byte(strconv.Itoa(maxDownload)), (fileExp + 10))
// set file name expiration with fn-<file-id> as key
stg.Set("fn-"+id, []byte(fileHeader.Filename), (fileExp + 10))
// setup json response
data := map[string]interface{}{
"success": true,
"key": id,
"link": "http://" + r.Host + "/" + id,
"expiry": fileExp.String(),
"sec_exp": fileExp.Seconds(),
}
resp, _ := json.Marshal(data)
w.Write(resp)
return
}
}
}
w.Write([]byte(fmt.Sprintf(`{"success": false, "error": 402, "message": "%v"}`, err.Error())))
return
}
}