Skip to content

Commit

Permalink
Merge pull request #4 from sony-csl-maker/martin/refactorRecorderUpload
Browse files Browse the repository at this point in the history
Martin/refactor recorder upload
  • Loading branch information
martinvanaud authored May 31, 2023
2 parents 672541a + fb2ad7a commit efc9681
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
11 changes: 6 additions & 5 deletions client/src/MainContainer/MainContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ const MainContainer: FC = () => {
}, [currentUploadedFileState.isSuccessUploading]);

const onUpload = useCallback((file: File[]) => {
uploadFileToServer(file[0]);
uploadFileToServer(file[0], false);
}, []);

const uploadFileToServer = (file: File) => {
const uploadFileToServer = (file: File, fromRecorder: boolean) => {
const fromRecordParameter = fromRecorder ? '?fromRecorder=true' : '';
setCurrentUploadedFileState((state) => {
return {
...state,
Expand All @@ -80,7 +81,7 @@ const MainContainer: FC = () => {
const data = new FormData();
data.append("file", file);
axios
.post(`${API_ENDPOINT}/audiofile/upload`, data, {
.post(`${API_ENDPOINT}/audiofile/upload${fromRecordParameter}`, data, {
headers: {
"User-Id": localStorage.getItem("user_id"),
},
Expand All @@ -99,8 +100,8 @@ const MainContainer: FC = () => {

const onGetAudioRecorderElement = (blob: Blob) => {
console.log(blob);
const file = new File([blob], "audio-record.wav", { type: blob.type });
uploadFileToServer(file);
const file = new File([blob], "audio-record.wav", { type: 'audio/wav' });
uploadFileToServer(file, true);
};

return (
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def before_each_api_request():

def after_each_api_request():
user_id = g.get('user_id', None)
user_folder_path = g.get('user_folder_path', None)

user_activity[user_id] = datetime.now().isoformat()


Expand Down Expand Up @@ -95,6 +97,8 @@ def post_audiofile_upload():
user_id = g.get('user_id', None)
user_folder_path = g.get('user_folder_path', None)

from_recorder = extract_request_argument(request, 'fromRecorder')

# Check if file was passed as request parameter, if not send response with http code 400
if 'file' not in request.files:
return Flask.response_class(
Expand All @@ -103,6 +107,10 @@ def post_audiofile_upload():
mimetype='application/json'
)

for files in os.listdir(user_folder_path):
if files.endswith((".wav", ".csv")):
os.remove(os.path.join(user_folder_path, files))

# Assign the file passed as parameter to local variable
file = request.files['file']

Expand All @@ -119,15 +127,11 @@ def post_audiofile_upload():
mimetype='application/json'
)

for files in os.listdir(user_folder_path):
if files.endswith((".wav", ".csv")):
os.remove(os.path.join(user_folder_path, files))

# Assign the file filename to local variable using secure_filename (from Werkzeug) which returns a secure version
# of it so that it can be safely stored
filename = secure_filename(file.filename)

if file.filename.endswith((".wav")):
if from_recorder == "true":
file_name, extension = os.path.splitext(filename)
file_path = os.path.join(user_folder_path, file_name + extension)
file.save(file_path)
Expand All @@ -143,11 +147,8 @@ def post_audiofile_upload():

os.remove(file_path)
else:
return Flask.response_class(
response='File format not supported',
status=HTTPStatus.BAD_REQUEST,
mimetype='application/json'
)
file_path = os.path.join(user_folder_path, "audiofile.wav")
file.save(file_path)

return response, HTTPStatus.OK

Expand Down

0 comments on commit efc9681

Please sign in to comment.