-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (101 loc) · 3.33 KB
/
main.go
File metadata and controls
117 lines (101 loc) · 3.33 KB
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
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
)
// const uploadDir = "static/uploads" // Directory to store uploaded files
var uploadDir string
func main() {
secretKey := flag.String("secret", "your_secret_key", "Secret key for upload")
pathUpload := flag.String("pathUpload", "static/uploads", "Path to upload")
pageUpload := flag.String("pageUpload", "/upload-page", "Path to upload")
port := flag.Int("port", 8088, "Port to listen on")
enableUploadPage := flag.Bool("enableUploadPage", false, "Port to listen on")
allowDownload := flag.Bool("allowDownload", true, "Port to listen on")
flag.Parse()
uploadDir = *pathUpload
// Create upload directory if it doesn't exist
err := os.MkdirAll(uploadDir, 0755)
if err != nil {
fmt.Printf("Error creating upload directory: %v", err)
return
}
if *enableUploadPage {
http.HandleFunc(*pageUpload, uploadPageHandler)
}
if *allowDownload {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(uploadDir)))) // Serve static files
}
http.HandleFunc("/", indexHandler)
http.HandleFunc("/upload", uploadHandler(*secretKey))
fmt.Printf("Server listening on port %d\n", *port)
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/index.html"))
err := tmpl.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func uploadPageHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/upload.html"))
err := tmpl.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func uploadHandler(secret string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Verify secret key
if r.FormValue("secret") != secret {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Invalid secret key")
return
}
// Get uploaded file
file, header, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error retrieving uploaded file: %v", err)
return
}
defer file.Close()
// Read file data
data, err := ioutil.ReadAll(file)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error reading uploaded file: %v", err)
return
}
// Generate unique filename
filename := fmt.Sprintf("%d-%s", time.Now().UnixNano(), header.Filename)
// Save uploaded file
filepath := filepath.Join(uploadDir, filename)
err = ioutil.WriteFile(filepath, data, 0644)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error saving uploaded file: %v", err)
return
}
downloadURL := fmt.Sprintf("/static/%s", filename)
// Generate download link HTML
downloadLink := fmt.Sprintf("<p>Download file: </p><a href='%s'>%s</a>", downloadURL, downloadURL)
// Set content type to HTML
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// Respond with success message and download link
fmt.Fprintf(w, "<!DOCTYPE html><html><body><h1>File uploaded successfully: %s</h1><p>%s</p></body></html>", filename, downloadLink)
}
}