Indeed this method is doing a lot more than uploading a file. Really looks like the full module would benefit if this method is split in several method for smaller tasks.