mmt icon indicating copy to clipboard operation
mmt copied to clipboard

SortBy/Location not working as configured

Open ztmr opened this issue 2 years ago • 0 comments

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.

  • Camera: all
  • Firmware Version: all
  • Operating System: all
  • Commit ID: 51a42683ee0d62efbe06b1fb1d30b828d06e467d (current development branch HEAD)

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.

ztmr avatar Oct 08 '23 12:10 ztmr