127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gabriel-vasile/mimetype"
|
|
"github.com/pelletier/go-toml/v2"
|
|
)
|
|
|
|
type Config struct {
|
|
Paths []string
|
|
}
|
|
|
|
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 main() {
|
|
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)
|
|
}
|
|
}
|
|
|
|
func shouldEntryBeChecked(entry fs.DirEntry) bool {
|
|
//fmt.Println(filepath.Ext(entry.Name()))
|
|
// If it's a file
|
|
return (entry.Type().IsRegular() &&
|
|
// If it ends with .jpeg or .jpg
|
|
(strings.HasSuffix(entry.Name(), ".jpeg") ||
|
|
strings.HasSuffix(entry.Name(), ".jpg")))
|
|
}
|
|
|
|
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 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())
|
|
if getFileMimeType(path).String() == "image/png" {
|
|
fmt.Println("File at", path, "was detected as png with wrong extension")
|
|
changeFileExtension(path, ".png")
|
|
}
|
|
} else if v.IsDir() {
|
|
fixExtensionsInDirectory(path.Join(directory, v.Name()))
|
|
}
|
|
}
|
|
}
|