utils.go 751 B

123456789101112131415161718192021222324252627282930313233343536
  1. package handler
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. )
  8. // generateRequestId generates a unique request ID
  9. func generateRequestId() string {
  10. return fmt.Sprintf("%d-%d", time.Now().Unix(), time.Now().Nanosecond())
  11. }
  12. // getContentType returns the MIME type for a file based on its extension
  13. func getContentType(filename string) string {
  14. ext := strings.ToLower(filepath.Ext(filename))
  15. switch ext {
  16. case ".txt":
  17. return "text/plain"
  18. case ".html", ".htm":
  19. return "text/html"
  20. case ".json":
  21. return "application/json"
  22. case ".xml":
  23. return "application/xml"
  24. case ".jpg", ".jpeg":
  25. return "image/jpeg"
  26. case ".png":
  27. return "image/png"
  28. case ".pdf":
  29. return "application/pdf"
  30. default:
  31. return "application/octet-stream"
  32. }
  33. }