filetype_detect.go 895 字节
package tool

import (
	"path/filepath"
	"strings"
)

const (
	Image = "image"
	Video = "video"
)

var TypeMap = map[string]string{
	"jpg":  Image,
	"png":  Image,
	"gif":  Image,
	"webp": Image,
	"cr2":  Image,
	"tif":  Image,
	"bmp":  Image,
	"heif": Image,
	"jxr":  Image,
	"psd":  Image,
	"ico":  Image,
	"dwg":  Image,
	"avif": Image,

	"mp4":  Video,
	"m4v":  Video,
	"mkv":  Video,
	"webm": Video,
	"mov":  Video,
	"avi":  Video,
	"wmv":  Video,
	"mpg":  Video,
	"flv":  Video,
	"3gp":  Video,
}
var DefaultFileTypeDetector = FileTypeDetector{}

type FileTypeDetector struct {
}

func (c FileTypeDetector) Classify(medias []string, mediaType string) []string {
	result := make([]string, 0)
	for _, media := range medias {
		v, ok := TypeMap[strings.Trim(filepath.Ext(media), ".")]
		if !ok {
			continue
		}
		if v == mediaType {
			result = append(result, media)
		}
	}
	return result
}