file_decoder.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package service
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io"
  6. "one-api/common"
  7. "one-api/constant"
  8. "one-api/dto"
  9. "strings"
  10. )
  11. func GetFileBase64FromUrl(url string) (*dto.LocalFileData, error) {
  12. var maxFileSize = constant.MaxFileDownloadMB * 1024 * 1024
  13. resp, err := DoDownloadRequest(url)
  14. if err != nil {
  15. return nil, err
  16. }
  17. defer resp.Body.Close()
  18. // Always use LimitReader to prevent oversized downloads
  19. fileBytes, err := io.ReadAll(io.LimitReader(resp.Body, int64(maxFileSize+1)))
  20. if err != nil {
  21. return nil, err
  22. }
  23. // Check actual size after reading
  24. if len(fileBytes) > maxFileSize {
  25. return nil, fmt.Errorf("file size exceeds maximum allowed size: %dMB", constant.MaxFileDownloadMB)
  26. }
  27. // Convert to base64
  28. base64Data := base64.StdEncoding.EncodeToString(fileBytes)
  29. mimeType := resp.Header.Get("Content-Type")
  30. if len(strings.Split(mimeType, ";")) > 1 {
  31. // If Content-Type has parameters, take the first part
  32. mimeType = strings.Split(mimeType, ";")[0]
  33. }
  34. if mimeType == "application/octet-stream" {
  35. if common.DebugEnabled {
  36. println("MIME type is application/octet-stream, trying to guess from URL or filename")
  37. }
  38. // try to guess the MIME type from the url last segment
  39. urlParts := strings.Split(url, "/")
  40. if len(urlParts) > 0 {
  41. lastSegment := urlParts[len(urlParts)-1]
  42. if strings.Contains(lastSegment, ".") {
  43. // Extract the file extension
  44. filename := strings.Split(lastSegment, ".")
  45. if len(filename) > 1 {
  46. ext := strings.ToLower(filename[len(filename)-1])
  47. // Guess MIME type based on file extension
  48. mimeType = GetMimeTypeByExtension(ext)
  49. }
  50. }
  51. } else {
  52. // try to guess the MIME type from the file extension
  53. fileName := resp.Header.Get("Content-Disposition")
  54. if fileName != "" {
  55. // Extract the filename from the Content-Disposition header
  56. parts := strings.Split(fileName, ";")
  57. for _, part := range parts {
  58. if strings.HasPrefix(strings.TrimSpace(part), "filename=") {
  59. fileName = strings.TrimSpace(strings.TrimPrefix(part, "filename="))
  60. // Remove quotes if present
  61. if len(fileName) > 2 && fileName[0] == '"' && fileName[len(fileName)-1] == '"' {
  62. fileName = fileName[1 : len(fileName)-1]
  63. }
  64. // Guess MIME type based on file extension
  65. if ext := strings.ToLower(strings.TrimPrefix(fileName, ".")); ext != "" {
  66. mimeType = GetMimeTypeByExtension(ext)
  67. }
  68. break
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return &dto.LocalFileData{
  75. Base64Data: base64Data,
  76. MimeType: mimeType,
  77. Size: int64(len(fileBytes)),
  78. }, nil
  79. }
  80. func GetMimeTypeByExtension(ext string) string {
  81. // Convert to lowercase for case-insensitive comparison
  82. ext = strings.ToLower(ext)
  83. switch ext {
  84. // Text files
  85. case "txt", "md", "markdown", "csv", "json", "xml", "html", "htm":
  86. return "text/plain"
  87. // Image files
  88. case "jpg", "jpeg":
  89. return "image/jpeg"
  90. case "png":
  91. return "image/png"
  92. case "gif":
  93. return "image/gif"
  94. // Audio files
  95. case "mp3":
  96. return "audio/mp3"
  97. case "wav":
  98. return "audio/wav"
  99. case "mpeg":
  100. return "audio/mpeg"
  101. // Video files
  102. case "mp4":
  103. return "video/mp4"
  104. case "wmv":
  105. return "video/wmv"
  106. case "flv":
  107. return "video/flv"
  108. case "mov":
  109. return "video/mov"
  110. case "mpg":
  111. return "video/mpg"
  112. case "avi":
  113. return "video/avi"
  114. case "mpegps":
  115. return "video/mpegps"
  116. // Document files
  117. case "pdf":
  118. return "application/pdf"
  119. default:
  120. return "application/octet-stream" // Default for unknown types
  121. }
  122. }