You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.7 KiB

1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "log"
  8. "os"
  9. "path"
  10. "time"
  11. "github.com/rwcarlsen/goexif/exif"
  12. )
  13. const version = "v0.0.2_2024-01-26"
  14. const layout = "2006-01-02T15:04:05.000Z"
  15. func main() {
  16. fmt.Println("version:", version)
  17. var versionFlag bool
  18. var inputDirFlag, outputDirFlag string
  19. flag.BoolVar(&versionFlag, "version", false, "print current version")
  20. flag.BoolVar(&versionFlag, "v", false, "print current version")
  21. flag.StringVar(&inputDirFlag, "input", "./", "input directory")
  22. flag.StringVar(&inputDirFlag, "i", "./", "input directory")
  23. flag.StringVar(&outputDirFlag, "output", "output", "output directory")
  24. flag.StringVar(&outputDirFlag, "o", "output", "output directory")
  25. flag.Parse()
  26. if versionFlag {
  27. os.Exit(0)
  28. }
  29. if err := os.Mkdir(outputDirFlag, os.ModePerm); err != nil {
  30. log.Fatal("output directory already exists")
  31. }
  32. entries, err := os.ReadDir(inputDirFlag)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. for _, e := range entries {
  37. // skip subdirectories
  38. if e.IsDir() {
  39. continue
  40. }
  41. fileName := inputDirFlag + "/" + e.Name()
  42. fileExtension := path.Ext(fileName)
  43. // get file
  44. fmt.Printf("---FileName: %s,", fileName)
  45. f, err := os.Open(fileName)
  46. if err != nil {
  47. fmt.Println("\ne", err)
  48. }
  49. defer f.Close()
  50. // get camera date
  51. date, err := getExifDate(f)
  52. if err != nil && (err == io.EOF || err.Error() == "exif: failed to find exif intro marker") {
  53. fmt.Println(" not an img file, skipping file")
  54. continue
  55. }
  56. if err != nil {
  57. fmt.Println("err", err)
  58. continue
  59. }
  60. fmt.Println("\n DATE", date)
  61. newFileName := getValidFileName(outputDirFlag+"/"+date.String(), fileExtension)
  62. newFileName = newFileName + fileExtension
  63. fmt.Println(" storing the new img as", newFileName)
  64. // duplicate the original file into newFileName
  65. f, err = os.Open(fileName)
  66. if err != nil {
  67. fmt.Println("\ne", err)
  68. }
  69. defer f.Close()
  70. fo, err := os.Create(newFileName)
  71. if err != nil {
  72. panic(err)
  73. }
  74. defer fo.Close()
  75. buf := make([]byte, 1024)
  76. for {
  77. n, err := f.Read(buf)
  78. if err != nil && err != io.EOF {
  79. panic(err)
  80. }
  81. if n == 0 {
  82. break
  83. }
  84. if _, err := fo.Write(buf[:n]); err != nil {
  85. panic(err)
  86. }
  87. }
  88. // set the img date into the file date (file's access and modification times)
  89. os.Chtimes(newFileName, *date, *date)
  90. }
  91. }
  92. func getExifDate(f *os.File) (*time.Time, error) {
  93. x, err := exif.Decode(f)
  94. if err != nil {
  95. return nil, err
  96. }
  97. d, err := x.DateTime()
  98. if err != nil {
  99. return nil, err
  100. }
  101. return &d, nil
  102. }
  103. func getValidFileName(fileName, fileExtension string) string {
  104. _, err := os.Open(fileName + fileExtension)
  105. if !errors.Is(err, os.ErrNotExist) {
  106. return getValidFileName(fileName+"_1", fileExtension)
  107. }
  108. return fileName
  109. }