def control_recording(recording_id): """Change the state of a recording.""" validate.validate_form(request.form, recording.form_schema['control_recording']) try: retrieved_media = recording.media_dict[recording_id] except KeyError: raise HTTPError( 404, 'No recording with id "{}" was found'.format(recording_id)) command = request.form['command'] media_state = retrieved_media.current_state if command == 'start' and media_state in [ Multimedia.NULL, Multimedia.PAUSE ]: retrieved_media.record() elif command == 'pause' and media_state == Multimedia.RECORD: retrieved_media.pause() elif command == 'stop' and media_state in [ Multimedia.RECORD, Multimedia.PAUSE ]: retrieved_media.stop() else: raise HTTPError(400, 'Command "{}" could not be performed'.format(command)) if media_state is not Multimedia.NULL: key = str(recording_id) recording.media_info[key]['null_multimeda'] = False return ''
def create_recording(): """Initializes a recording and returns its id.""" if validate.validate_create_recording_request_form(request.form): new_filename = request.form['filename'] new_media = Multimedia(recording.record_config, recording.record_plugin_manager) success, filename = new_media.load_backend(None, new_filename) if success: filepath = new_media.plugman.get_plugin_by_name( new_media.config.record_to_file_plugin, "Output").plugin_object.location new_recording_id = recording.next_id recording.next_id = recording.next_id + 1 if new_recording_id not in recording.media_dict: recording.media_dict[new_recording_id] = { 'media': new_media, 'filename': filename, 'filepath': filepath } return {'id': new_recording_id} else: raise HTTPError('Provided id already in use', 500) else: raise HTTPError('Could not load multimedia backend', 500) else: raise HTTPError('Form data was invalid', 400)
def control_recording(recording_id): """Change the state of a recording.""" recording_id = int(recording_id) if recording_id in recording.media_dict: retrieved_media_entry = recording.media_dict[recording_id] retrieved_media = retrieved_media_entry['media'] if validate.validate_control_recording_request_form(request.form): command = request.form['command'] media_state = retrieved_media.current_state if command == 'start' and media_state in [ Multimedia.NULL, Multimedia.PAUSE ]: retrieved_media.record() elif command == 'pause' and media_state == Multimedia.RECORD: retrieved_media.pause() elif command == 'stop' and media_state in [ Multimedia.RECORD, Multimedia.PAUSE ]: retrieved_media.stop() else: raise HTTPError('command could not be performed', 400) else: raise HTTPError('Form data was invalid', 400) else: raise HTTPError('recording id could not be found', 404) return ''
def get_specific_recording(recording_id): """Returns specific recording by id.""" recording_id = int(recording_id) if recording_id in recording.media_dict: retrieved_media_entry = recording.media_dict[recording_id] retrieved_media = retrieved_media_entry['media'] retrieved_filename = retrieved_media_entry['filename'] state_indicator = retrieved_media.current_state if state_indicator == Multimedia.NULL: state = 'NULL' elif state_indicator == Multimedia.RECORD: state = 'RECORD' elif state_indicator == Multimedia.PAUSE: state = 'PAUSE' elif state_indicator == Multimedia.STOP: state = 'STOP' else: raise HTTPError('recording state could not be determined', 500) if os.path.isfile(retrieved_media_entry['filepath']): filesize = os.path.getsize(retrieved_media_entry['filepath']) else: filesize = 'NA' return { 'id': recording_id, 'filename': retrieved_filename, 'filesize': filesize, 'status': state } else: raise HTTPError('recording id could not be found', 404)
def create_recording(): """Initializes a recording and returns its id.""" if not validate.validate_create_recording_request_form(request.form): raise HTTPError('Form data was invalid', 400) new_filename = request.form['filename'] new_media = Multimedia(recording.config, recording.plugin_manager) success, filename = new_media.load_backend(None, new_filename) if not success: raise HTTPError('Could not load multimedia backend', 500) filepath = new_media.plugman.get_plugin_by_name( new_media.config.record_to_file_plugin, "Output").plugin_object.location new_recording_id = recording.next_id key = str(new_recording_id) recording.media_dict[new_recording_id] = new_media recording.media_info[key] = { 'filename': filename, 'filepath': filepath, 'null_multimeda': True, } recording.next_id = recording.next_id + 1 recording.media_info.sync() return {'id': new_recording_id}
def control_recording(recording_id): """Change the state of a recording.""" if not validate.validate_control_recording_request_form(request.form): raise HTTPError('Form data was invalid', 400) try: retrieved_media = recording.media_dict[recording_id] except KeyError: raise HTTPError('recording id could not be found', 404) command = request.form['command'] media_state = retrieved_media.current_state if command == 'start' and media_state in [ Multimedia.NULL, Multimedia.PAUSE ]: retrieved_media.record() elif command == 'pause' and media_state == Multimedia.RECORD: retrieved_media.pause() elif command == 'stop' and media_state in [ Multimedia.RECORD, Multimedia.PAUSE ]: retrieved_media.stop() else: raise HTTPError('command could not be performed', 400) if media_state is not Multimedia.NULL: key = str(recording_id) recording.media_info[key]['null_multimeda'] = False return ''
def delete_recording(recording_id): """Deletes a recording given an id.""" try: retrieved_media = recording.media_dict[recording_id] except KeyError: raise HTTPError( 404, 'No recording with id "{}" was found'.format(recording_id)) key = str(recording_id) retrieved_media_entry = recording.media_info[key] if retrieved_media.current_state in [Multimedia.RECORD, Multimedia.PAUSE]: retrieved_media.stop() # Delete the file if it exists try: os.remove(retrieved_media_entry['filepath']) except OSError: pass del recording.media_dict[recording_id] del recording.media_info[key] recording.media_info.sync() return ''
def delete_recording(recording_id): """Deletes a recording given an id.""" recording_id = int(recording_id) if recording_id in recording.media_dict: retrieved_media_entry = recording.media_dict[recording_id] retrieved_media = retrieved_media_entry['media'] if retrieved_media.current_state == Multimedia.RECORD or retrieved_media.current_state == Multimedia.PAUSE: retrieved_media.stop() # Delete the file if it exists if os.path.isfile(retrieved_media_entry['filepath']): os.remove(retrieved_media_entry['filepath']) del recording.media_dict[recording_id] else: raise HTTPError('recording id could not be found', 404) return ''
def get_specific_recording(recording_id): """Returns specific recording by id.""" key = str(recording_id) try: retrieved_media_entry = recording.media_info[key] except KeyError: raise HTTPError( 404, 'No recording with id "{}" was found'.format(recording_id)) current_state = recording.media_dict[recording_id].current_state filename = retrieved_media_entry['filename'] try: filesize = os.path.getsize(retrieved_media_entry['filepath']) except OSError: filesize = 'NA' return { 'id': recording_id, 'filename': filename, 'filesize': filesize, 'status': current_state, }
def get_specific_recording(recording_id): """Returns specific recording by id.""" key = str(recording_id) try: retrieved_media_entry = recording.media_info[key] except KeyError: raise HTTPError('media with given recording id could not be found', 404) current_state = recording.media_dict[recording_id].current_state filename = retrieved_media_entry['filename'] try: filesize = os.path.getsize(retrieved_media_entry['filepath']) except OSError: filesize = 'NA' return { 'id': recording_id, 'filename': filename, 'filesize': filesize, 'status': current_state, }
def validate_form(to_validate, schema): try: validate(to_validate, schema) except ValidationError as e: raise HTTPError(400, 'Form data was invalid: {}'.format(e.message))