image upload code refactor