Refactored the upload attachment functions to remove redundant code