|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package doc |
| 5 | + |
| 6 | +import ( |
| 7 | + "mime" |
| 8 | + "net/http" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + larkcore "github.com/larksuite/oapi-sdk-go/v3/core" |
| 13 | +) |
| 14 | + |
| 15 | +type docMediaExtensionResolution struct { |
| 16 | + Ext string |
| 17 | + Source string |
| 18 | + Detail string |
| 19 | +} |
| 20 | + |
| 21 | +var docMediaMimeToExt = map[string]string{ |
| 22 | + "application/msword": ".doc", |
| 23 | + "application/pdf": ".pdf", |
| 24 | + "application/vnd.ms-excel": ".xls", |
| 25 | + "application/vnd.ms-powerpoint": ".ppt", |
| 26 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation": ".pptx", |
| 27 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx", |
| 28 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx", |
| 29 | + "application/xml": ".xml", |
| 30 | + "application/zip": ".zip", |
| 31 | + "image/bmp": ".bmp", |
| 32 | + "image/gif": ".gif", |
| 33 | + "image/jpeg": ".jpg", |
| 34 | + "image/png": ".png", |
| 35 | + "image/svg+xml": ".svg", |
| 36 | + "image/webp": ".webp", |
| 37 | + "text/csv": ".csv", |
| 38 | + "text/html": ".html", |
| 39 | + "text/plain": ".txt", |
| 40 | + "text/xml": ".xml", |
| 41 | + "video/mp4": ".mp4", |
| 42 | +} |
| 43 | + |
| 44 | +func autoAppendDocMediaExtension(outputPath string, header http.Header, fallbackExt string) (string, *docMediaExtensionResolution) { |
| 45 | + if docMediaHasExplicitExtension(outputPath) { |
| 46 | + return outputPath, nil |
| 47 | + } |
| 48 | + normalizedPath := outputPath |
| 49 | + if filepath.Ext(outputPath) == "." { |
| 50 | + normalizedPath = strings.TrimSuffix(outputPath, ".") |
| 51 | + } |
| 52 | + if resolution := docMediaExtensionByContentType(header.Get("Content-Type")); resolution != nil { |
| 53 | + return normalizedPath + resolution.Ext, resolution |
| 54 | + } |
| 55 | + if resolution := docMediaExtensionByContentDisposition(header); resolution != nil { |
| 56 | + return normalizedPath + resolution.Ext, resolution |
| 57 | + } |
| 58 | + if fallbackExt != "" { |
| 59 | + return normalizedPath + fallbackExt, &docMediaExtensionResolution{ |
| 60 | + Ext: fallbackExt, |
| 61 | + Source: "fallback", |
| 62 | + Detail: "default fallback", |
| 63 | + } |
| 64 | + } |
| 65 | + return outputPath, nil |
| 66 | +} |
| 67 | + |
| 68 | +func docMediaHasExplicitExtension(path string) bool { |
| 69 | + ext := filepath.Ext(path) |
| 70 | + return ext != "" && ext != "." |
| 71 | +} |
| 72 | + |
| 73 | +func docMediaExtensionByContentType(contentType string) *docMediaExtensionResolution { |
| 74 | + if contentType == "" { |
| 75 | + return nil |
| 76 | + } |
| 77 | + mediaType, _, err := mime.ParseMediaType(contentType) |
| 78 | + if err != nil { |
| 79 | + mediaType = strings.TrimSpace(strings.Split(contentType, ";")[0]) |
| 80 | + } |
| 81 | + if ext, ok := docMediaMimeToExt[strings.ToLower(mediaType)]; ok { |
| 82 | + return &docMediaExtensionResolution{ |
| 83 | + Ext: ext, |
| 84 | + Source: "Content-Type", |
| 85 | + Detail: contentType, |
| 86 | + } |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func docMediaExtensionByContentDisposition(header http.Header) *docMediaExtensionResolution { |
| 92 | + filename := strings.TrimSpace(larkcore.FileNameByHeader(header)) |
| 93 | + if filename == "" { |
| 94 | + return nil |
| 95 | + } |
| 96 | + ext := filepath.Ext(filename) |
| 97 | + if ext == "" || ext == "." { |
| 98 | + return nil |
| 99 | + } |
| 100 | + return &docMediaExtensionResolution{ |
| 101 | + Ext: ext, |
| 102 | + Source: "Content-Disposition", |
| 103 | + Detail: filename, |
| 104 | + } |
| 105 | +} |
0 commit comments