-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
739 lines (648 loc) · 23.7 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/robfig/cron/v3"
"golang.org/x/crypto/bcrypt"
"golang.org/x/time/rate"
"gopkg.in/yaml.v3"
)
const (
bufferSize = 10 * 1024 * 1024 // 10MB buffer size
)
var (
AppConfig Cfg
rateLimiters = make(map[string]*rate.Limiter)
)
type UserCredentials struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type Cfg struct {
ServerPort string `yaml:"ServerPort"`
EnableTLS bool `yaml:"EnableTLS"`
CertPathCrt string `yaml:"CertPathCrt"`
CertPathKey string `yaml:"CertPathKey"`
MaxUploadSize int64 `yaml:"MaxUploadSize"`
MaxExpireHours int `yaml:"MaxExpireHours"`
EnablePassword bool `yaml:"EnablePassword"`
ShowUploadBox bool `yaml:"ShowUploadBox"`
ShowMenuDownloadPage *bool `yaml:"ShowMenuDownloadPage"`
UploadDir string `yaml:"UploadDir"`
RateLimitPeriod int `yaml:"RateLimitPeriod"`
RateLimitAttempts int `yaml:"RateLimitAttempts"`
}
// FileInfo stores metadata about uploaded files
type FileInfo struct {
FileID string `json:"file_id"`
Timestamp time.Time `json:"timestamp"`
OneTimeDownload bool `json:"one_time_download"`
ExpiryDate time.Time `json:"expiry_date,omitempty"`
MaxDownloads int `json:"max_downloads,omitempty"`
Downloads int `json:"downloads"`
}
func formatSize(size int64) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
)
var result string
switch {
case size >= TB:
result = fmt.Sprintf("%.2f TB", float64(size)/TB)
case size >= GB:
result = fmt.Sprintf("%.2f GB", float64(size)/GB)
case size >= MB:
result = fmt.Sprintf("%.2f MB", float64(size)/MB)
case size >= KB:
result = fmt.Sprintf("%.2f KB", float64(size)/KB)
default:
result = fmt.Sprintf("%d bytes", size)
}
return result
}
func readUserCredentials(filePath string) ([]UserCredentials, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
var credentials []UserCredentials
err = yaml.Unmarshal(data, &credentials)
if err != nil {
return nil, err
}
return credentials, nil
}
// Middleware to add cache headers
func cacheMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set Cache-Control and Expires headers for static files
w.Header().Set("Cache-Control", "public, max-age=86400") // 1 day
w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(http.TimeFormat))
next.ServeHTTP(w, r)
})
}
// Get Client ip from X-Forwarded-For header if exist
func getClientIP(r *http.Request) string {
// Read the IP from the X-Forwarded-For header, if it exists
forwarded := r.Header.Get("X-Forwarded-For")
if forwarded != "" {
// X-Forwarded-For can contain a comma-separated list of IP addresses
// Take the first IP address in the list
parts := strings.Split(forwarded, ",")
return strings.TrimSpace(parts[0])
}
// Otherwise, return the remote IP address
return r.RemoteAddr
}
// basicAuth is a middleware function that implements basic authentication
func basicAuth(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// If password authentication is not enabled, skip to the next handler
if !AppConfig.EnablePassword {
next.ServeHTTP(w, r)
return
}
// Get the client's IP address
ip := getClientIP(r)
// Check to see if there is a rate limiter for this IP address
limiter, ok := rateLimiters[ip]
if !ok {
// Create a new rate limiter for this IP address (for example, 5 attempts per minute)
limiter = rate.NewLimiter(rate.Every(time.Duration(AppConfig.RateLimitPeriod)*time.Second), AppConfig.RateLimitAttempts)
rateLimiters[ip] = limiter
}
// Consume a token from the rate limiter
if !limiter.Allow() {
// If there are no tokens available, return a 429 Too Many Requests error
http.Error(w, "Too many requests", http.StatusTooManyRequests)
return
}
// Get the Authorization header from the request
auth := r.Header.Get("Authorization")
if auth == "" {
// If the header is empty, return a 401 Unauthorized response with a WWW-Authenticate header
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Split the Authorization header into two parts: the scheme and the credentials
parts := strings.SplitN(auth, " ", 2)
if len(parts) != 2 || parts[0] != "Basic" {
// If the header is malformed, return a 401 Unauthorized response
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Decode the credentials from base64
payload, _ := base64.StdEncoding.DecodeString(parts[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || !validateCredentials(pair[0], pair[1]) {
// If the credentials are invalid, return a 401 Unauthorized response
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// If the credentials are valid, call the next handler in the chain
next.ServeHTTP(w, r)
}
}
// validateCredentials checks if the provided username and password are valid
func validateCredentials(username, password string) bool {
credentialsPath := "config/credentials.yaml"
if _, err := os.Stat(credentialsPath); os.IsNotExist(err) {
credentialsPath = "credentials.yaml"
}
credentials, err := readUserCredentials(credentialsPath)
if err != nil {
fmt.Println("Error reading credentials file:", err)
return false
}
for _, cred := range credentials {
if cred.Username == username {
err := bcrypt.CompareHashAndPassword([]byte(cred.Password), []byte(password))
return err == nil
}
}
return false
}
// formatDuration formats a duration in hours into a human-readable string
func formatDuration(hours int) string {
years := hours / (24 * 365)
remainingHours := hours % (24 * 365)
months := remainingHours / (24 * 30) // Approximation: 30 days per month
remainingHours = remainingHours % (24 * 30)
weeks := remainingHours / (24 * 7)
remainingHours = remainingHours % (24 * 7)
days := remainingHours / 24
remainingHours = remainingHours % 24
var durationParts []string
if years > 0 {
durationParts = append(durationParts, fmt.Sprintf("%d years", years))
}
if months > 0 {
durationParts = append(durationParts, fmt.Sprintf("%d months", months))
}
if weeks > 0 {
durationParts = append(durationParts, fmt.Sprintf("%d weeks", weeks))
}
if days > 0 {
durationParts = append(durationParts, fmt.Sprintf("%d days", days))
}
if remainingHours > 0 {
durationParts = append(durationParts, fmt.Sprintf("%d hours", remainingHours))
}
return strings.Join(durationParts, " ")
}
// serveUploadPage serves the upload page template
func serveUploadPage(w http.ResponseWriter, r *http.Request) {
// Construct the path to the upload.html template
tmplPath := filepath.Join("templates", "upload.html")
// Create a new template with a custom function map
tmpl := template.Must(template.New("").Funcs(template.FuncMap{
// Register a custom function to format file sizes
"formatSize": formatSize,
}).ParseFiles(tmplPath))
// Calculate the maximum expiry duration as a string
maxExpireDuration := formatDuration(AppConfig.MaxExpireHours)
// Create a struct to hold data for the template
data := struct {
MaxUploadSize int64
MaxExpireDuration string
}{
// Set the maximum upload size from the AppConfig
MaxUploadSize: AppConfig.MaxUploadSize,
// Set the maximum expiry duration as a string
MaxExpireDuration: maxExpireDuration,
}
// Execute the template with the data
if err := tmpl.ExecuteTemplate(w, "upload.html", data); err != nil {
// If there's an error executing the template, return an error response
http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError)
}
}
// validateInput checks if the input data is valid
func validateExpiryDate(expiryDate time.Time) error {
if expiryDate.Before(time.Now()) {
return fmt.Errorf("expiry date must be in the future")
}
return nil
}
func validateMaxDownloads(maxDownloads int) error {
if maxDownloads < 0 {
return fmt.Errorf("max downloads must be non-negative")
}
return nil
}
func validateInput(oneTimeDownload bool, expiryDate time.Time, maxDownloads int) error {
if err := validateExpiryDate(expiryDate); err != nil {
return err
}
if err := validateMaxDownloads(maxDownloads); err != nil {
return err
}
return nil
}
// uploadFile handles the file upload process
func uploadFile(w http.ResponseWriter, r *http.Request) {
fmt.Println("Starting file upload")
// Limit file size to AppConfig.MaxUploadSize
r.Body = http.MaxBytesReader(w, r.Body, AppConfig.MaxUploadSize)
// Create a multipart reader to read the request body
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, "Error reading multipart data", http.StatusInternalServerError)
return
}
// Initialize variables to store file information
var tempFile *os.File
var tempFilePath string
var foundFile bool
var oneTimeDownload bool
var expiryDate time.Time
var maxDownloads int
// Default expiry date to x hours from now
expiryDate = time.Now().Add(time.Duration(AppConfig.MaxExpireHours) * time.Hour)
// Iterate over each part of the multipart request
for {
part, err := reader.NextPart()
if err == io.EOF {
break // No more parts to read
}
if err != nil {
http.Error(w, "Error reading multipart data", http.StatusInternalServerError)
return
}
// Check if the part is a file
if part.FormName() == "file" {
// Create a temporary file to store the uploaded file
tempFile, err = os.CreateTemp(AppConfig.UploadDir, "upload-*.enc")
if err != nil {
http.Error(w, "Error creating temporary file", http.StatusInternalServerError)
return
}
tempFilePath = tempFile.Name() // Keep track of the file path
defer tempFile.Close()
// Copy the file data from the part to the temporary file
_, err = io.Copy(tempFile, part)
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
http.Error(w, "Error writing to temporary file", http.StatusInternalServerError)
return
}
foundFile = true
} else if part.FormName() == "oneTimeDownload" {
// Read the one-time download flag from the part
buf := new(bytes.Buffer)
buf.ReadFrom(part)
oneTimeDownload = buf.String() == "true"
} else if part.FormName() == "expiryDate" {
// Read the expiry date from the part
buf := new(bytes.Buffer)
buf.ReadFrom(part)
expiryDate, err = time.Parse("2006-01-02", buf.String())
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
http.Error(w, "Invalid date format. Use YYYY-MM-DD.", http.StatusBadRequest)
return
}
} else if part.FormName() == "maxDownloads" {
// Read the maximum downloads value from the part
buf := new(bytes.Buffer)
buf.ReadFrom(part)
maxDownloads, err = strconv.Atoi(buf.String())
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
http.Error(w, "Invalid max downloads value", http.StatusBadRequest)
return
}
}
}
// Check if a file was uploaded
if !foundFile {
http.Error(w, "No file uploaded", http.StatusBadRequest)
return
}
// Validate input data
if err := validateInput(oneTimeDownload, expiryDate, maxDownloads); err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Create a FileInfo struct to store file information
fileInfo := FileInfo{
FileID: filepath.Base(tempFile.Name()),
Timestamp: time.Now(),
OneTimeDownload: oneTimeDownload,
ExpiryDate: expiryDate,
MaxDownloads: maxDownloads,
Downloads: 0,
}
// Save file info to JSON file
infoFile, err := os.Create(filepath.Join(AppConfig.UploadDir, fileInfo.FileID+".json"))
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
http.Error(w, "Error creating info file", http.StatusInternalServerError)
return
}
defer infoFile.Close()
err = json.NewEncoder(infoFile).Encode(fileInfo)
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
os.Remove(filepath.Join(AppConfig.UploadDir, fileInfo.FileID+".json")) // Clean up the JSON file on error
http.Error(w, "Error encoding JSON file", http.StatusInternalServerError)
return
}
// Send response with file info
jsonResponse, err := json.Marshal(fileInfo)
if err != nil {
tempFile.Close()
os.Remove(tempFilePath) // Clean up the temp file on error
os.Remove(filepath.Join(AppConfig.UploadDir, fileInfo.FileID+".json")) // Clean up the JSON file on error
http.Error(w, "Error creating JSON response", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
fmt.Println("File uploaded successfully:", fileInfo.FileID)
}
// downloadFile handles the file download process
func downloadFile(w http.ResponseWriter, r *http.Request) {
// Extract the file ID from the URL path
vars := mux.Vars(r)
fileID := vars["fileID"]
// Open and decode file info
infoFilePath := filepath.Join(AppConfig.UploadDir, fileID+".json")
infoFile, err := os.Open(infoFilePath)
if err != nil {
// If the file is not found, return a 404 error
http.Error(w, "File not found", http.StatusNotFound)
return
}
var fileInfo FileInfo
err = json.NewDecoder(infoFile).Decode(&fileInfo)
infoFile.Close() // Ensure the file is closed before attempting to delete
if err != nil {
// If there's an error decoding the JSON, return a 500 error
http.Error(w, "Error reading file info", http.StatusInternalServerError)
return
}
// Check if the file has expired
if fileInfo.ExpiryDate.Before(time.Now()) {
// If the file has expired, delete it and return a 410 error
deleteFileAndMetadata(filepath.Join(AppConfig.UploadDir, fileID), infoFilePath)
http.Error(w, "File has expired", http.StatusGone)
return
}
// Check if the file has reached the maximum number of downloads
if fileInfo.MaxDownloads > 0 && fileInfo.Downloads >= fileInfo.MaxDownloads {
// If the file has reached the maximum number of downloads, return a 410 error
http.Error(w, "File has reached the maximum number of downloads", http.StatusGone)
return
}
// Increment the download count
fileInfo.Downloads++
// Update file info with new download count
infoFile, err = os.Create(infoFilePath)
if err != nil {
// If there's an error updating the info file, return a 500 error
http.Error(w, "Error updating info file", http.StatusInternalServerError)
return
}
json.NewEncoder(infoFile).Encode(&fileInfo)
infoFile.Close()
// Open the actual file for download
filePath := filepath.Join(AppConfig.UploadDir, fileID)
file, err := os.Open(filePath)
if err != nil {
// If the file is not found, return a 404 error
http.Error(w, "File not found", http.StatusNotFound)
return
}
defer file.Close()
// Get file stats to set response headers
fileStat, err := file.Stat()
if err != nil {
// If there's an error getting file info, return a 500 error
http.Error(w, "Error getting file info", http.StatusInternalServerError)
return
}
// Set headers and write file to response
w.Header().Set("Content-Disposition", "attachment; filename="+fileID)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", fmt.Sprintf("%d", fileStat.Size()))
buffer := make([]byte, bufferSize)
for {
n, err := file.Read(buffer)
if err != nil {
if err == os.ErrClosed || err == io.EOF {
break
}
// If there's an error reading the file, return a 500 error
http.Error(w, "Error reading file", http.StatusInternalServerError)
return
}
if n > 0 {
w.Write(buffer[:n])
w.(http.Flusher).Flush()
}
}
// Delete file if it's a one-time download or reached max downloads
if fileInfo.OneTimeDownload || (fileInfo.MaxDownloads > 0 && fileInfo.Downloads >= fileInfo.MaxDownloads) {
deleteFileAndMetadata(filePath, infoFilePath)
fmt.Println("Deleted file:", fileID)
}
}
// deleteOldFiles scans the uploads directory and deletes expired files
func deleteOldFiles() {
// Read the contents of the uploads directory
files, err := os.ReadDir(AppConfig.UploadDir)
if err != nil {
// If there's an error reading the directory, print a message and exit
fmt.Println("Error reading upload directory:", err)
return
}
// Iterate over each file in the directory
for _, file := range files {
// Check if the file is a JSON file (metadata file)
if filepath.Ext(file.Name()) == ".json" {
// Construct the full path to the metadata file
infoFilePath := filepath.Join(AppConfig.UploadDir, file.Name())
// Open the metadata file
infoFile, err := os.Open(infoFilePath)
if err != nil {
// If there's an error opening the file, print a message and skip to the next file
fmt.Println("Error opening info file:", err)
continue
}
// Decode the JSON data into a FileInfo struct
var fileInfo FileInfo
err = json.NewDecoder(infoFile).Decode(&fileInfo)
// Ensure the file is closed before attempting to delete
infoFile.Close()
if err != nil {
// If there's an error decoding the JSON, print a message and skip to the next file
fmt.Println("Error decoding info file:", err)
continue
}
// Check if the file has expired
if time.Now().After(fileInfo.ExpiryDate) {
// Construct the full path to the file
filePath := filepath.Join(AppConfig.UploadDir, fileInfo.FileID)
// Delete the file and its metadata
deleteFileAndMetadata(filePath, infoFilePath)
fmt.Println("Deleted expired file:", fileInfo.FileID)
}
}
}
}
// deleteFileAndMetadata deletes a file and its associated metadata file
func deleteFileAndMetadata(filePath, infoFilePath string) {
// Attempt to delete the file
if err := os.Remove(filePath); err != nil {
// If there's an error deleting the file, print a message with the file path and error
fmt.Println("Error deleting file:", filePath, err)
}
// Attempt to delete the metadata file
if err := os.Remove(infoFilePath); err != nil {
// If there's an error deleting the metadata file, print a message with the file path and error
fmt.Println("Error deleting metadata file:", infoFilePath, err)
}
}
// ReadConfig reads the configuration file and populates the AppConfig struct
func ReadConfig() {
configPath := "./config/config.yaml"
if _, err := os.Stat(configPath); os.IsNotExist(err) {
configPath = "./config.yaml"
}
// Open the configuration file
f, err := os.Open(configPath)
if err != nil {
// If there's an error, print it and exit
fmt.Println(err)
return
}
// Defer closing the file until we're done with it
defer f.Close()
// Create a new YAML decoder to read the configuration file
decoder := yaml.NewDecoder(f)
// Decode the YAML data into the AppConfig struct
err = decoder.Decode(&AppConfig)
if err != nil {
// If there's an error decoding the YAML, print it and exit
fmt.Println(err)
return
}
// Set default value for UploadDir if it's not specified in the config
if AppConfig.UploadDir == "" {
AppConfig.UploadDir = "./uploads"
}
// Set default values for rate limiting if they are not specified in the config
if AppConfig.RateLimitPeriod <= 0 {
AppConfig.RateLimitPeriod = 60 // Default to 60 seconds
}
if AppConfig.RateLimitAttempts <= 0 {
AppConfig.RateLimitAttempts = 5 // Default to 5 attempts
}
if AppConfig.ShowMenuDownloadPage == nil {
defaultValue := true
AppConfig.ShowMenuDownloadPage = &defaultValue
}
}
func serveDownloadPage(w http.ResponseWriter, r *http.Request) {
// Construct the path to the download.html template
tmplPath := filepath.Join("templates", "download.html")
// Create a new template with a custom function map
tmpl := template.Must(template.New("").ParseFiles(tmplPath))
// Create a struct to hold data for the template
data := struct {
ShowUploadBox bool
ShowMenuDownloadPage bool
}{
// Set the ShowUploadBox from the AppConfig
ShowUploadBox: AppConfig.ShowUploadBox,
// Set the ShowMenuDownloadPage from the AppConfig
ShowMenuDownloadPage: *AppConfig.ShowMenuDownloadPage,
}
// Execute the template with the data
if err := tmpl.ExecuteTemplate(w, "download.html", data); err != nil {
// If there's an error executing the template, return an error response
http.Error(w, "Error executing template: "+err.Error(), http.StatusInternalServerError)
}
}
func main() {
// Read configuration from file
ReadConfig()
// Ensure the upload directory exists
if _, err := os.Stat(AppConfig.UploadDir); os.IsNotExist(err) {
err := os.MkdirAll(AppConfig.UploadDir, os.ModePerm)
if err != nil {
fmt.Printf("Error creating upload directory: %v\n", err)
return
}
}
// Create a new router to handle HTTP requests
r := mux.NewRouter()
// Define routes for file upload and download
// -----------------------------
// Upload routes
r.HandleFunc("/upload", basicAuth(uploadFile)).Methods("POST")
r.HandleFunc("/upload.html", basicAuth(serveUploadPage)).Methods("GET")
r.HandleFunc("/download.html", serveDownloadPage).Methods("GET")
// Share download route (same as above, but with /share prefix)
r.HandleFunc("/share/download.html", serveDownloadPage).Methods("GET")
// Share upload routes (same as above, but with /share prefix)
r.HandleFunc("/share/upload", basicAuth(uploadFile)).Methods("POST")
r.HandleFunc("/share/upload.html", basicAuth(serveUploadPage)).Methods("GET")
// Download route
r.HandleFunc("/download/{fileID}", downloadFile).Methods("GET")
// Share download route (same as above, but with /share prefix)
r.HandleFunc("/share/download/{fileID}", downloadFile).Methods("GET")
// Serve static files directly from the root URL path
// This allows us to serve static assets (e.g. CSS, JS, images) from the /static directory
// Serve static files with caching middleware
r.PathPrefix("/share/").Handler(http.StripPrefix("/share", cacheMiddleware(http.FileServer(http.Dir("./static")))))
r.PathPrefix("/").Handler(cacheMiddleware(http.FileServer(http.Dir("./static"))))
// Schedule deletion of old files every hour using cron
// This ensures that old files are automatically removed from the system
c := cron.New()
c.AddFunc("@hourly", deleteOldFiles)
c.Start()
// Start the server on port AppConfig.ServerPort
srv := &http.Server{
Handler: r,
Addr: ":" + AppConfig.ServerPort,
}
// Print startup message and server status
fmt.Println("Starting server on port " + AppConfig.ServerPort)
if AppConfig.EnableTLS {
fmt.Println("HTTPS enabled")
// Start the server with TLS enabled
if err := srv.ListenAndServeTLS(AppConfig.CertPathCrt, AppConfig.CertPathKey); err != nil {
fmt.Println("Server error:", err)
}
} else {
fmt.Println("HTTPS disabled, using HTTP")
// Start the server with TLS disabled
if err := srv.ListenAndServe(); err != nil {
fmt.Println("Server error:", err)
}
}
}