171 lines
4.0 KiB
Go
171 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"slices"
|
|
|
|
"github.com/gabriel-vasile/mimetype"
|
|
"github.com/pelletier/go-toml/v2"
|
|
)
|
|
|
|
type Config struct {
|
|
Paths []string
|
|
}
|
|
|
|
// Define a map of MIME types to their correct extensions.
|
|
var conversionMap = map[string][]string{
|
|
"image/png": {".png"},
|
|
"image/vnd.mozilla.apng": {".png"},
|
|
"image/jpeg": {".jpg", ".jpeg"},
|
|
"image/webp": {".webp"},
|
|
"video/mp4": {".mp4"},
|
|
"video/quicktime": {".mp4"},
|
|
"video/3gpp": {".3gp", ".3gpp", ".mp4"},
|
|
"video/x-m4v": {".m4v", ".mp4"},
|
|
"video/webm": {".webm"},
|
|
"video/x-matroska": {".mkv"},
|
|
"image/gif": {".gif"},
|
|
}
|
|
|
|
var suffixesToCheck = map[string]struct{}{}
|
|
|
|
func loadConfiguration() (*Config, error) {
|
|
dir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
configFilePath := path.Join(dir, "justanyone", "fix-mime-types", "config.toml")
|
|
_, err = os.Stat(configFilePath)
|
|
if err != nil {
|
|
// If it's an error that is not for file not existing, send it upwards
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
|
|
// Otherwise, create the parent directories
|
|
if err := os.MkdirAll(filepath.Dir(configFilePath), 0770); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Marshal a default object
|
|
bytes, _ := toml.Marshal(&Config{Paths: []string{}})
|
|
|
|
file, err := os.Create(configFilePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
file.Write(bytes)
|
|
}
|
|
|
|
file, err := os.Open(configFilePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bytes, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var config Config
|
|
err = toml.Unmarshal(bytes, &config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
func loadSuffixes() {
|
|
for _, extensions := range conversionMap {
|
|
// Only add unique extensions to the suffixesToCheck list.
|
|
for _, ext := range extensions {
|
|
suffixesToCheck[ext] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
loadSuffixes()
|
|
config, err := loadConfiguration()
|
|
if err != nil {
|
|
log.Fatal("Failed to load configuration:", err)
|
|
}
|
|
|
|
if len(config.Paths) == 0 {
|
|
fmt.Println("Warning: you have no configured directory paths")
|
|
}
|
|
|
|
for _, directory := range config.Paths {
|
|
fixExtensionsInDirectory(directory)
|
|
}
|
|
}
|
|
|
|
// This function returns true if the entry is a regular file and has an extension that is in the suffixesToCheck list.
|
|
func shouldEntryBeChecked(entry fs.DirEntry) bool {
|
|
if !entry.Type().IsRegular() {
|
|
return false
|
|
}
|
|
ext := filepath.Ext(entry.Name())
|
|
_, exists := suffixesToCheck[ext]
|
|
if exists {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func getFileMimeType(pathToFile string) *mimetype.MIME {
|
|
mimeType, err := mimetype.DetectFile(pathToFile)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return mimeType
|
|
}
|
|
|
|
func changeFileExtension(originalPath string, newExtension string) {
|
|
oldExt := filepath.Ext(originalPath)
|
|
newPath := originalPath[0:len(originalPath)-len(oldExt)] + newExtension
|
|
os.Rename(originalPath, newPath)
|
|
}
|
|
|
|
func isFileExtensionExpected(currentExt string, expectedExts []string) bool {
|
|
return slices.Contains(expectedExts, currentExt)
|
|
}
|
|
|
|
func fixExtensionsInDirectory(directory string) {
|
|
entries, err := os.ReadDir(directory)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
for _, v := range entries {
|
|
if shouldEntryBeChecked(v) {
|
|
path := filepath.Join(directory, v.Name())
|
|
fileExt := filepath.Ext(path)
|
|
mimeType := getFileMimeType(path).String()
|
|
|
|
expectedExtensions, ok := conversionMap[mimeType]
|
|
if !ok {
|
|
fmt.Println("File at", path, "has unrecognized MIME type", mimeType)
|
|
continue
|
|
}
|
|
// Check if the file's extension is in the list of expected extensions for its MIME type.
|
|
if !isFileExtensionExpected(fileExt, expectedExtensions) {
|
|
fmt.Println("File at", path, "has MIME", mimeType, "and expects", expectedExtensions, "but got", fileExt)
|
|
changeFileExtension(path, expectedExtensions[0])
|
|
}
|
|
} else if v.IsDir() {
|
|
fixExtensionsInDirectory(path.Join(directory, v.Name()))
|
|
}
|
|
}
|
|
}
|