NewFileFromPath 从路径创建文件
(path string)
| 291 | |
| 292 | // NewFileFromPath 从路径创建文件 |
| 293 | func NewFileFromPath(path string) (*File, error) { |
| 294 | data, err := os.ReadFile(path) |
| 295 | if err != nil { |
| 296 | return nil, fmt.Errorf("failed to read file: %w", err) |
| 297 | } |
| 298 | |
| 299 | file := &File{ |
| 300 | Name: filepath.Base(path), |
| 301 | Size: int64(len(data)), |
| 302 | Extension: filepath.Ext(path), |
| 303 | Data: base64.StdEncoding.EncodeToString(data), |
| 304 | } |
| 305 | |
| 306 | // 尝试检测 MIME 类型 |
| 307 | file.MimeType = detectMimeType(path) |
| 308 | |
| 309 | return file, nil |
| 310 | } |
| 311 | |
| 312 | // detectMimeType 检测 MIME 类型 |
| 313 | func detectMimeType(path string) string { |
no test coverage detected