Remove duplicate code for image uploading