-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathi.go
196 lines (152 loc) · 3.81 KB
/
i.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
195
196
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"regexp"
"strings"
"time"
)
var (
// the address to listen on
address = "127.0.0.1:9005"
// the directory to save the images in
root = "/var/www/i.fourtf.com/"
// the root of the link that will be generated
webRoot = "https://i.fourtf.com/"
// maximum age for the files
// the program will delete the files older than maxAge every 2 hours
maxAge = time.Hour * 24 * 365
// files to be ignored when deleting old files
deleteIgnoreRegexp = regexp.MustCompile("index\\.html|favicon\\.ico")
// length of the random filename
randomAdjectivesCount = 2
adjectives = make([]string, 0)
filetypes = make(map[string]string)
)
func main() {
rand.Seed(time.Now().UnixNano())
b, err := ioutil.ReadFile("./filetypes.json")
if err == nil {
data := make(map[string][]string)
if err = json.Unmarshal(b, &data); err != nil {
fmt.Println(err)
} else {
for val, keys := range data {
for _, key := range keys {
filetypes["."+strings.TrimLeft(key, ".")] = val
}
}
}
}
fmt.Println(filetypes)
file, err := os.Open("./adjectives1.txt")
if err != nil {
panic(err)
}
r := bufio.NewReader(file)
for {
line, _, err := r.ReadLine()
if err != nil {
break
}
adjectives = append(adjectives, string(line))
}
// uncomment to collect old files
// go func() {
// for {
// <-time.After(time.Hour * 2)
// collectGarbage()
// }
// }()
// create server with read and write timeouts and the desired address
server := &http.Server{
ReadTimeout: time.Minute,
WriteTimeout: time.Minute,
Addr: address,
}
// open http server
http.HandleFunc("/", handleUpload)
server.ListenAndServe()
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
infile, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "Error parsing uploaded file: "+err.Error(), http.StatusBadRequest)
return
}
defer infile.Close()
filename := header.Filename
var ext string
// get extension from file name
index := strings.LastIndex(filename, ".")
if index == -1 {
ext = ""
} else {
ext = filename[index:]
filename = filename[:index]
}
lastWord := "File"
fmt.Println(ext)
if val, ok := filetypes[ext]; ok {
lastWord = strings.Title(val)
}
var savePath string
var link string
// find a random filename that doesn't exist already
for i := 0; i < 100; i++ {
random := ""
for j := 0; j < randomAdjectivesCount; j++ {
random += strings.TrimSpace(strings.Title(adjectives[rand.Intn(len(adjectives))]))
}
random += lastWord
// fuck with link
savePath = root + random + ext
link = webRoot + random + ext
if _, err := os.Stat(savePath); os.IsNotExist(err) {
break
}
}
// save the file
outfile, err := os.Create(savePath)
if err != nil {
http.Error(w, "error while saving file: "+err.Error(), http.StatusBadRequest)
return
}
_, err = io.Copy(outfile, infile)
if err != nil {
http.Error(w, "error while saving file: "+err.Error(), http.StatusBadRequest)
return
}
outfile.Close()
// return the link as the http body
w.Write([]byte(link))
// do this or it doesn't work
io.Copy(ioutil.Discard, r.Body)
}
func collectGarbage() {
files, err := ioutil.ReadDir(root)
if err != nil {
return
}
for _, file := range files {
fname := file.Name()
if file.IsDir() || deleteIgnoreRegexp.MatchString(fname) {
continue
}
if time.Since(file.ModTime()) > maxAge {
err := os.Remove(root + fname)
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("Removed %s \n", fname)
}
}
}