diff --git a/src/c20_server/data_repository.py b/src/c20_server/data_repository.py index e91914c..6d4d0fa 100644 --- a/src/c20_server/data_repository.py +++ b/src/c20_server/data_repository.py @@ -8,12 +8,18 @@ class DataRepository: def __init__(self, base_path): self.base_path = base_path - def save_data(self, directory_name, filename, contents): + def save_text_data(self, directory_name, filename, contents): self.ensure_directory_exists(directory_name) path = os.path.join(self.base_path, directory_name, filename) with open(path, 'w') as file: file.write(json.dumps(contents)) + def save_binary_data(self, directory_name, filename, contents): + self.ensure_directory_exists(directory_name) + path = os.path.join(self.base_path, directory_name, filename) + with open(path, 'wb') as file: + file.write(contents) + def ensure_directory_exists(self, directory_name): Path(os.path.join(self.base_path, directory_name))\ .mkdir(parents=True, exist_ok=True) diff --git a/src/c20_server/flask_server.py b/src/c20_server/flask_server.py index baf0e57..8e94b9f 100644 --- a/src/c20_server/flask_server.py +++ b/src/c20_server/flask_server.py @@ -42,7 +42,19 @@ def _return_result(): return {}, 400 update_job_manager(job_manager, client_data) - save_data(data_repository, client_data['data']) + save_text_data(data_repository, client_data['data']) + + return {}, 200 + + @app.route('/upload_file', methods=['POST']) + def upload_file(): + file = request.files['file'] + contents = file.stream.read() + filename = file.filename + + job_id = request.form['job_id'] + client_id = request.form['client_id'] + data_repository.save_text_data('foldername', filename, contents) return {}, 200 @@ -67,11 +79,11 @@ def update_job_manager(job_manager, client_data): print(job, '\n') -def save_data(data_repository, list_of_data_dicts): +def save_text_data(data_repository, list_of_data_dicts): data_items = DataExtractor.extract(list_of_data_dicts) for data_item in data_items: - data_repository.save_data(data_item.folder_name, - data_item.file_name, data_item.contents) + data_repository.save_text_data(data_item.folder_name, + data_item.file_name, data_item.contents) def redis_connect(): diff --git a/src/c20_server/spy_data_repository.py b/src/c20_server/spy_data_repository.py index 527d1a4..bbfc837 100644 --- a/src/c20_server/spy_data_repository.py +++ b/src/c20_server/spy_data_repository.py @@ -9,7 +9,7 @@ class SpyDataRepository: def __init__(self): self.saved_items = [] - def save_data(self, directory_name, filename, contents): + def save_text_data(self, directory_name, filename, contents): item = SavedItem(directory_name=directory_name, filename=filename, contents=contents) diff --git a/src/file_flask.py b/src/file_flask.py new file mode 100644 index 0000000..8804c51 --- /dev/null +++ b/src/file_flask.py @@ -0,0 +1,19 @@ + +from flask import Flask, request + +app = Flask(__name__) + + +@app.route('/', methods=['POST']) +def upload(): + + file = request.files['file'] + print(file.stream.read()) + print(file.filename) + print(request.form['job_id']) + print(request.form['client_id']) + return {}, 200 + + +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/src/upload_file.py b/src/upload_file.py new file mode 100644 index 0000000..8aeccff --- /dev/null +++ b/src/upload_file.py @@ -0,0 +1,10 @@ + +import requests + +binary_data = bytearray([1, 2, 3, 4]) + +files = {'file': ('foo.bin', binary_data)} +data = {'job_id': 'job42', + 'client_id': 93} + +requests.post('http://localhost:5000/', files=files, data=data) diff --git a/tests/test_data_repository.py b/tests/test_data_repository.py index dda8474..47d14dd 100644 --- a/tests/test_data_repository.py +++ b/tests/test_data_repository.py @@ -12,14 +12,27 @@ def ensure_directory_exists(self, directory_name): self.ensure_directory_exists_called = True -def test_class_saves_data(mocker): +def test_class_saves_text_data(mocker): file_mock = mocker.patch('c20_server.data_repository.open', mock_open()) data_repo = DummyMakeDirDataRepository('data') directory = 'FAA/DOC-ID/' filename = 'document.json' contents = {'id': 'FAA'} - data_repo.save_data(directory, filename, contents) + data_repo.save_text_data(directory, filename, contents) file_mock.assert_called_once_with('data/FAA/DOC-ID/document.json', 'w') file_mock().write.assert_called_once_with('{"id": "FAA"}') assert data_repo.ensure_directory_exists_called + + +def test_class_saves_binary_data(mocker): + file_mock = mocker.patch('c20_server.data_repository.open', mock_open()) + data_repo = DummyMakeDirDataRepository('data') + directory = 'FAA/DOC-ID/' + filename = 'file.bin' + contents = bytearray([1, 2, 3, 4]) + data_repo.save_binary_data(directory, filename, contents) + + file_mock.assert_called_once_with('data/FAA/DOC-ID/file.bin', 'wb') + file_mock().write.assert_called_once_with(bytearray([1, 2, 3, 4])) + assert data_repo.ensure_directory_exists_called diff --git a/tests/test_flask_server.py b/tests/test_flask_server.py index 933fba3..b8aa6d9 100644 --- a/tests/test_flask_server.py +++ b/tests/test_flask_server.py @@ -1,5 +1,6 @@ import json from unittest.mock import patch +from io import BytesIO import pytest from c20_server.flask_server import create_app, redis_connect from c20_server.mock_job_manager import MockJobManager @@ -197,3 +198,27 @@ def test_run_server_with_no_redis_connection(): with patch('sys.exit') as exit_server: redis_connect() assert exit_server.called + + +def test_upload_file(job_manager): + data_repository_spy = SpyDataRepository() + app = create_app(job_manager, data_repository_spy, MockDatabase(True)) + app.config['TESTING'] = True + client = app.test_client() + + json_data = { + 'client_id': '1', + 'job_id': '1', + 'file': (BytesIO(b'contents'), 'filename.pdf') + } + + result = client.post('/upload_file', content_type='multipart/form-data', + data=json_data) + + assert result.status_code == 200 + + first_save = data_repository_spy.saved_items[0] + + assert first_save.directory_name == 'foldername' + assert first_save.filename == 'filename.pdf' + assert first_save.contents == b'contents'