| 599 | return data |
| 600 | |
| 601 | def write(self, data, remote_path): |
| 602 | |
| 603 | action = self.actions.get('write', {}) |
| 604 | payload_write = action.get('write') |
| 605 | payload_truncate = action.get('truncate') |
| 606 | call_name = action.get('call', 'inject') |
| 607 | |
| 608 | # Skip if something is missing or call function is not set |
| 609 | if not action or not payload_write or not payload_truncate or not call_name or not hasattr(self, call_name): |
| 610 | return |
| 611 | |
| 612 | # Check existance and overwrite with --force-overwrite |
| 613 | if self.get('blind') or self.md5(remote_path): |
| 614 | if not self.channel.args.get('force_overwrite'): |
| 615 | if self.get('blind'): |
| 616 | log.warn('Blind upload can overwrite files, rerun with --force-overwrite to confirm') |
| 617 | else: |
| 618 | log.warn('Remote path already exists, rerun with --force-overwrite for upload') |
| 619 | return |
| 620 | else: |
| 621 | execution_code = payload_truncate % ({ 'path' : remote_path }) |
| 622 | getattr(self, call_name)( |
| 623 | code = execution_code |
| 624 | ) |
| 625 | |
| 626 | # Upload file in chunks of 500 characters |
| 627 | for chunk in chunkit(data, 500): |
| 628 | |
| 629 | log.debug('[b64 encoding] %s' % chunk) |
| 630 | chunk_b64 = base64.urlsafe_b64encode(chunk) |
| 631 | |
| 632 | execution_code = payload_write % ({ 'path' : remote_path, 'chunk_b64' : chunk_b64 }) |
| 633 | getattr(self, call_name)( |
| 634 | code = execution_code |
| 635 | ) |
| 636 | |
| 637 | if self.get('blind'): |
| 638 | log.warn('Blind upload can\'t check the upload correctness, check manually') |
| 639 | elif not md5(data) == self.md5(remote_path): |
| 640 | log.warn('Remote file md5 mismatch, check manually') |
| 641 | else: |
| 642 | log.warn('File uploaded correctly') |
| 643 | |
| 644 | |
| 645 | def evaluate(self, code, **kwargs): |