KonradIT / mmt

Media Management Tool - make importing videos/photos from GoPro and other action cameras/drones a little bit more bearable.
http://mmt.camera
Apache License 2.0
109 stars 12 forks source link

SortBy/Location not working as configured #125

Open ztmr opened 1 year ago

ztmr commented 1 year ago

SortBy/Location is not working as configured

Context:

The GetOrder function in pkg/utils/ordering.go is calling the location lookup asynchronously, which means that the location almost always gets added as the last piece of the target path.

The location needs to be resolved upfront (if required) and appended at the right time/order.

Expected Behavior:

Default configuration is to sort by date,location,camera so the expected result is to store target files in <date>/<location>/<camera> directory structure.

Current Behavior:

Since the location is resolved asynchronously, the date and camera gets added to the path string first, and location ends up to be the latest almost in all the cases.

I've made a quick and dirty hack to to prove this issue and it works fine with the following code:

func GetOrder(sortoptions SortOptions, GetLocation locationUtil, osPathname, out, mediaDate, deviceName string) string {
  order := orderFromConfig()
  dayFolder := out

  var wg sync.WaitGroup

  location := fallbackFromConfig()

  for _, item := range order {
    switch item {
    case "location":
      if GetLocation == nil {
        continue
      }
      wg.Add(1)
      go func() {
        defer wg.Done()
        locationFromFile, locerr := GetLocation.GetLocation(osPathname)
        if locerr == nil {
          reverseLocation, reverseerr := ReverseLocation(*locationFromFile)
          if reverseerr == nil {
            location = reverseLocation
            if location == "" || location == " " {
              location = fallbackFromConfig()
            }
          }
        }
      }()
    default:
    }
  }

  wg.Wait()

  for _, item := range order {
    switch item {
    case "date":
      dayFolder = filepath.Join(dayFolder, mediaDate)
    case "camera":
      if sortoptions.ByCamera {
        dayFolder = filepath.Join(dayFolder, deviceName)
      }
    case "location":
      if sortoptions.ByLocation {
        dayFolder = filepath.Join(dayFolder, location)
      }

    default:
      // Not supported
    }
  }

  if _, err := os.Stat(dayFolder); os.IsNotExist(err) {
    _ = os.MkdirAll(dayFolder, 0o755)
  }
  return dayFolder
}

Logs / Screenshots:

N/A

Steps to Reproduce:

Default configuration. No need to change anything, just run import on any camera.