Fix 6946 master

Straight port to master from https://github.com/owncloud/core/pull/7745

Please review @icewind1991 @DeepDiver1975