Please do this in a separate private method, just like for js files. You could easily factor the checks in only one method.