def __init__(self, request): self.request = request self.form = UploadFileForm(request.POST, request.FILES) self.file_type = None self.file = None
class Upload(object): """Handle File Upload based on App type.""" def __init__(self, request): self.request = request self.form = UploadFileForm(request.POST, request.FILES) self.file_type = None self.file = None @staticmethod def as_view(request): upload = Upload(request) return upload.upload_html() def resp_json(self, data): resp = HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8') resp['Access-Control-Allow-Origin'] = '*' return resp def upload_html(self): request = self.request response_data = { 'description': '', 'status': 'error', } if request.method != 'POST': msg = 'Method not Supported!' logger.error(msg) response_data['description'] = msg return self.resp_json(response_data) if not self.form.is_valid(): msg = 'Invalid Form Data!' logger.error(msg) response_data['description'] = msg return self.resp_json(response_data) self.file = request.FILES['file'] self.file_type = FileType(self.file) if not self.file_type.is_allow_file(): msg = 'File format not Supported!' logger.error(msg) response_data['description'] = msg return self.resp_json(response_data) if self.file_type.is_ipa(): if platform.system() not in LINUX_PLATFORM: msg = 'Static Analysis of iOS IPA requires Mac or Linux' logger.error(msg) response_data['description'] = msg return self.resp_json(response_data) response_data = self.upload() return self.resp_json(response_data) def upload_api(self): """API File Upload.""" api_response = {} request = self.request if not self.form.is_valid(): api_response['error'] = FormUtil.errors_message(self.form) return api_response, HTTP_BAD_REQUEST self.file = request.FILES['file'] self.file_type = FileType(self.file) if not self.file_type.is_allow_file(): api_response['error'] = 'File format not Supported!' return api_response, HTTP_BAD_REQUEST api_response = self.upload() return api_response, 200 def upload(self): request = self.request scanning = Scanning(request) content_type = self.file.content_type file_name = self.file.name logger.info('MIME Type: %s FILE: %s', content_type, file_name) if self.file_type.is_apk(): return scanning.scan_apk() elif self.file_type.is_xapk(): return scanning.scan_xapk() elif self.file_type.is_zip(): return scanning.scan_zip() elif self.file_type.is_ipa(): return scanning.scan_ipa() elif self.file_type.is_appx(): return scanning.scan_appx()