1
1
package cmd
2
2
3
3
import (
4
+ _ "embed"
4
5
"fmt"
5
6
"io"
6
- "io/ioutil"
7
7
"net/http"
8
8
"os"
9
9
"path/filepath"
@@ -13,6 +13,9 @@ import (
13
13
"github.com/spf13/cobra"
14
14
)
15
15
16
+ //go:embed static/upload.html
17
+ var uploadHTML string
18
+
16
19
var folder string
17
20
var filePath string
18
21
var port int
@@ -76,23 +79,7 @@ var startCmd = &cobra.Command{
76
79
}
77
80
78
81
func uploadPage (w http.ResponseWriter , r * http.Request ) {
79
- htmlContent := `<!DOCTYPE html>
80
- <html lang="en">
81
- <head>
82
- <meta charset="UTF-8" />
83
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
84
- <meta http-equiv="X-UA-Compatible" content="ie=edge" />
85
- <title>Upload Files</title>
86
- </head>
87
- <body>
88
- <form enctype="multipart/form-data" action="/api/upload" method="post">
89
- <input type="file" multiple="multiple" name="files" />
90
- <input type="submit" value="upload" />
91
- </form>
92
- </body>
93
- </html>
94
- `
95
- w .Write ([]byte (htmlContent ))
82
+ w .Write ([]byte (uploadHTML ))
96
83
}
97
84
98
85
func uploadFile (w http.ResponseWriter , r * http.Request ) {
@@ -105,7 +92,7 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
105
92
file , err := fh .Open ()
106
93
if err != nil {
107
94
fmt .Println (err )
108
- w .WriteHeader (400 )
95
+ w .WriteHeader (http . StatusBadRequest )
109
96
w .Write ([]byte ("Error retrieving the file" ))
110
97
return
111
98
}
@@ -119,10 +106,10 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
119
106
fmt .Printf ("MIME type: %+v\n " , fh .Header ["Content-Type" ])
120
107
121
108
// 3. write temporary file on our server
122
- tempFile , err := ioutil . TempFile ("" , "http-upload-*" )
109
+ tempFile , err := os . CreateTemp ("" , "http-upload-*" )
123
110
if err != nil {
124
111
fmt .Println (err )
125
- w .WriteHeader (500 )
112
+ w .WriteHeader (http . StatusInternalServerError )
126
113
w .Write ([]byte ("Error creating temp file" ))
127
114
return
128
115
}
@@ -131,7 +118,7 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
131
118
132
119
if _ , err := io .CopyN (tempFile , file , fileSize ); err != nil {
133
120
fmt .Println (err )
134
- w .WriteHeader (500 )
121
+ w .WriteHeader (http . StatusInternalServerError )
135
122
w .Write ([]byte ("Error saving the file" ))
136
123
return
137
124
}
@@ -155,5 +142,5 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
155
142
156
143
// done
157
144
fmt .Printf ("Successfully Uploaded File\n \n \n " )
158
- http .Redirect (w , r , "/" , 302 )
145
+ http .Redirect (w , r , "/" , http . StatusFound )
159
146
}
0 commit comments