(url string)
| 11 | ) |
| 12 | |
| 13 | func GetFileBase64FromUrl(url string) (*dto.LocalFileData, error) { |
| 14 | var maxFileSize = constant.MaxFileDownloadMB * 1024 * 1024 |
| 15 | |
| 16 | resp, err := DoDownloadRequest(url) |
| 17 | if err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | defer resp.Body.Close() |
| 21 | |
| 22 | // Always use LimitReader to prevent oversized downloads |
| 23 | fileBytes, err := io.ReadAll(io.LimitReader(resp.Body, int64(maxFileSize+1))) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | // Check actual size after reading |
| 28 | if len(fileBytes) > maxFileSize { |
| 29 | return nil, fmt.Errorf("file size exceeds maximum allowed size: %dMB", constant.MaxFileDownloadMB) |
| 30 | } |
| 31 | |
| 32 | // Convert to base64 |
| 33 | base64Data := base64.StdEncoding.EncodeToString(fileBytes) |
| 34 | |
| 35 | mimeType := resp.Header.Get("Content-Type") |
| 36 | if len(strings.Split(mimeType, ";")) > 1 { |
| 37 | // If Content-Type has parameters, take the first part |
| 38 | mimeType = strings.Split(mimeType, ";")[0] |
| 39 | } |
| 40 | if mimeType == "application/octet-stream" { |
| 41 | if common.DebugEnabled { |
| 42 | println("MIME type is application/octet-stream, trying to guess from URL or filename") |
| 43 | } |
| 44 | // try to guess the MIME type from the url last segment |
| 45 | urlParts := strings.Split(url, "/") |
| 46 | if len(urlParts) > 0 { |
| 47 | lastSegment := urlParts[len(urlParts)-1] |
| 48 | if strings.Contains(lastSegment, ".") { |
| 49 | // Extract the file extension |
| 50 | filename := strings.Split(lastSegment, ".") |
| 51 | if len(filename) > 1 { |
| 52 | ext := strings.ToLower(filename[len(filename)-1]) |
| 53 | // Guess MIME type based on file extension |
| 54 | mimeType = GetMimeTypeByExtension(ext) |
| 55 | } |
| 56 | } |
| 57 | } else { |
| 58 | // try to guess the MIME type from the file extension |
| 59 | fileName := resp.Header.Get("Content-Disposition") |
| 60 | if fileName != "" { |
| 61 | // Extract the filename from the Content-Disposition header |
| 62 | parts := strings.Split(fileName, ";") |
| 63 | for _, part := range parts { |
| 64 | if strings.HasPrefix(strings.TrimSpace(part), "filename=") { |
| 65 | fileName = strings.TrimSpace(strings.TrimPrefix(part, "filename=")) |
| 66 | // Remove quotes if present |
| 67 | if len(fileName) > 2 && fileName[0] == '"' && fileName[len(fileName)-1] == '"' { |
| 68 | fileName = fileName[1 : len(fileName)-1] |
| 69 | } |
| 70 | // Guess MIME type based on file extension |
no test coverage detected