Maybe set `mime_type = nil` where the file is set, the override it inside the same `defined?(ActionDispatch) && file.is_a?(ActionDispatch::Http::UploadedFile)` block, while finally adding these two as fallbacks:

```
file = ...
mime_type = nil

case file
when ...
when ..
else
  if defined?(ActionDispatch) && file.is_a?(ActionDispatch::Http::UploadedFile)
   path = ...
   mime_type = file.content_type
  else
    ...
end

if path
  mime_type ||= MIME::Types || ...
end
```

Not sure if it's cleaner, but it keeps the ActionDispatch logic in one place.