def test_block_size_when_chunks_exceeded_limit(self, getsize): # mock getsize.return_value = 4 * 1024 * 1024 * 1024 * 1024 default_block_size = 4 * 1024 * 1024 block_size = FileService.get_block_size("test") self.assertNotEqual(default_block_size, block_size)
def test_block_size_should_be_default(self, getsize): # mock getsize.return_value = 128 default_block_size = 4 * 1024 * 1024 block_size = FileService.get_block_size("test") self.assertEqual(default_block_size, block_size)
def process_chunk(self, base_url, auth, issue_key, buf, lock, progress, count): index = next(count) etag = FileService.generate_etag(buf) response = self.check_if_chunk_exists(base_url, auth, issue_key, [etag]) if not response["data"]["results"][etag]["exists"]: self.upload_chunk(base_url, auth, issue_key, etag, buf) with lock: progress.update(1) return etag, index
def run(self): init() auth = (self.user, self.auth_token) block_size = FileService.get_block_size(self.file) file_size = os.path.getsize(self.file) expected_chunks = (file_size // block_size) + 1 lock = Lock() click.echo('Preparing file to upload: %s' % click.format_filename(self.file)) t = tqdm(total=expected_chunks, unit='B', unit_scale=False) t.set_description("Uploading progress") count = itertools.count() try: with ThreadPoolExecutor(max_workers=4) as executor: uploads = [] with open(self.file, 'rb') as infile: buf = infile.read(block_size) while len(buf) > 0: self.semaphore.acquire() try: future = executor.submit(self.process_chunk, self.base_url, auth, self.issue_key, buf, lock, t, count) uploads.append(future) buf = infile.read(block_size) except: self.semaphore.release() raise else: future.add_done_callback( lambda x: self.semaphore.release()) chunk_list = [ future.result() for future in futures.as_completed(uploads) ] self.create_file_chunked(self.base_url, auth, chunk_list, self.file, self.issue_key) t.update(1) t.close() click.echo( 'The file has been successfully uploaded and attached to the ticket %s' % self.issue_key) except AuthException as e: t.close() print(Fore.RED + "[ERROR] Authentication error. Check your API credentials.") raise e except Exception as e: print(Fore.RED + "[ERROR] " + str(e)) raise e
def test_raise_form_conversion(self): fs = FileService() file_list = { 'files': [{ 'id': '0', 'name': 'Test', 'mimeType': 'mimeType:application/vnd.google-apps.form', 'starred': False, 'trashed': False, 'owners': [{ 'kind': 'drive#user', 'displayName': 'Malliarjunarao Kosuri', 'me': True, 'permissionId': '123', 'emailAddress': '*****@*****.**' }] }] } assert fs.download(file_list['files']) == False
def get_files(): if request.headers.get("Authorization") != Config.auth_token: return jsonify({"message": "Invalid auth_token"}) data = request.json file_type = data.get("file_type", None) if not file_type: return jsonify({"message": "file_type not supplied"}) num_files = data.get("num_files", 1) output_file = FileService.get_files(file_type, num_files) if not output_file: return jsonify({"message": "no files present"}) else: return send_file( output_file, attachment_filename=output_file, as_attachment=True )
def test_generate_etag(self): expected_etag = 'a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd-4' actual_etag = FileService.generate_etag(b'data') self.assertEqual(expected_etag, actual_etag)
type=str, help='logger file') args = parser.parse_args() # Logging logging_cfg(args.logfile) # get values from environment access_token = os.environ.get('ACCESS_TOKEN') refresh_token = os.environ.get('REFRESH_TOKEN') token_uri = os.environ.get('TOKEN_URI') client_id = os.environ.get('CLIENT_ID') client_secret = os.environ.get('CLIENT_SECRET') if __name__ == "__main__": # login to Google drive ds = DriveClient() ds.login(access_token, refresh_token, token_uri, client_id, client_secret) cred = ds.get_credentials() # create file services https://developers.google.com/apps-script/reference/drive fs = FileService() fs.create_service(drive='drive', api_version='v3', credentials=cred) files = fs.list_files(args.name) for file in files: print("file_id:{0}, file_name:{1}, mimeType:{2}".format( file['id'], file['name'], file['mimeType'])) # download files fs.download(files)
def get_file(**kwargs): file_path = FileService.get_files(kwargs["type"], kwargs.get("num_files", 1)) with open(file_path, "rb") as content_file: content = content_file.read() return content # .encode()