file_decoder.go 3.9 KB

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