aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnirudh Oppiliappan <x@icyphox.sh>2021-06-07 08:44:45 +0530
committerAnirudh Oppiliappan <x@icyphox.sh>2021-06-07 08:44:45 +0530
commite997795d4345c01173e0af149cccf82b543e2eff (patch)
treedfeb45e9935ce27ba36ae32ac71f7620c8c7ceae
parentd050de4e1388e5b85a95702a9a26d8787c715729 (diff)
downloadfsrv-e997795d4345c01173e0af149cccf82b543e2eff.tar.gz
Done
-rw-r--r--index.html5
-rw-r--r--main.go59
2 files changed, 37 insertions, 27 deletions
diff --git a/index.html b/index.html
index 72a661c..44f7770 100644
--- a/index.html
+++ b/index.html
@@ -9,11 +9,14 @@
9 <body> 9 <body>
10 <form 10 <form
11 enctype="multipart/form-data" 11 enctype="multipart/form-data"
12 action="http://localhost:9393/" 12 action="http://localhost:4040"
13 method="post" 13 method="post"
14 > 14 >
15 <p>file:</p>
15 <input type="file" name="file" /> 16 <input type="file" name="file" />
16 <input type="submit" value="upload" /> 17 <input type="submit" value="upload" />
18 <p>key:</p>
19 <input type="password" name="key" />
17 </form> 20 </form>
18 </body> 21 </body>
19</html> 22</html>
diff --git a/main.go b/main.go
index a4117aa..470d54f 100644
--- a/main.go
+++ b/main.go
@@ -30,35 +30,42 @@ func randName(n int) string {
30} 30}
31 31
32func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) { 32func (s *settings) uploadFile(w http.ResponseWriter, r *http.Request) {
33 key := r.FormValue("key") 33 switch r.Method {
34 fmt.Println(key) 34 case "POST":
35 if key != s.key { 35 key := r.FormValue("key")
36 fmt.Fprintf(w, "incorrect key") 36 fmt.Println(key)
37 log.Printf("incorrect key: %+v", key) 37 if key != s.key {
38 return 38 fmt.Fprintf(w, "incorrect key")
39 } 39 log.Printf("incorrect key: %+v", key)
40 r.ParseMultipartForm(10 << 20) 40 return
41 file, handler, err := r.FormFile("file") 41 }
42 if err != nil { 42 r.ParseMultipartForm(10 << 20)
43 log.Println(err) 43 file, handler, err := r.FormFile("file")
44 return 44 if err != nil {
45 } 45 log.Println(err)
46 defer file.Close() 46 return
47 log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size) 47 }
48 defer file.Close()
49 log.Printf("file: %+v\t%+v bytes", handler.Filename, handler.Size)
48 50
49 ext := filepath.Ext(handler.Filename) 51 ext := filepath.Ext(handler.Filename)
50 fileBytes, err := io.ReadAll(file) 52 fileBytes, err := io.ReadAll(file)
51 if err != nil { 53 if err != nil {
52 log.Println(err) 54 log.Println(err)
53 } 55 }
54 56
55 newFile := randName(5) + ext 57 newFile := randName(5) + ext
56 diskFile := filepath.Join(s.storepath, newFile) 58 diskFile := filepath.Join(s.storepath, newFile)
57 os.WriteFile(diskFile, fileBytes, 0644) 59 os.WriteFile(diskFile, fileBytes, 0644)
58 log.Printf("wrote: %+v", diskFile) 60 log.Printf("wrote: %+v", diskFile)
59 61
60 fileUrl := s.url + "/" + newFile 62 fileUrl := s.url + "/" + newFile
61 fmt.Fprintf(w, "%v", fileUrl) 63 fmt.Fprintf(w, "%v", fileUrl)
64 case "GET":
65 http.ServeFile(w, r, "index.html")
66 default:
67 fmt.Fprintf(w, "unsupported method")
68 }
62} 69}
63 70
64func (s *settings) readSettings() { 71func (s *settings) readSettings() {