Flask-wtf with trix-editor – How do I upload images

Solution
const editor = document.querySelector('trix-editor');
(function() {
    HOST = '/_upload/'

  addEventListener("trix-attachment-add", function(event) {
    if (event.attachment.file) {
      uploadFileAttachment(event.attachment)
    }
      // get rid of the progress bar as Fetch does not support progress yet
      // this code originally used XMLHttpRequest instead of Fetch
      event.attachment.setUploadProgress(100)
  })

  function uploadFileAttachment(attachment) {
    uploadFile(attachment.file, setAttributes)

    function setAttributes(attributes) {
      attachment.setAttributes(attributes)
      alert(attributes)
    }
  }

  function uploadFile(file, successCallback) {
    var key = createStorageKey(file)
    var formData = createFormData(key, file)

    fetch(HOST, {method: 'POST', body: formData}).then(function(response){
        response.json().then(function(data){
            alert(data.file, data.status)
            if (data.status == 204) {
                var attributes = {
                  url: HOST + key,
                  href: HOST + key + "?content-disposition=attachment"
                }
                console.log(attributes)
                successCallback(attributes)
            }
        })
  })
}


  function createStorageKey(file) {
    var date = new Date()
    var day = date.toISOString().slice(0,10)
    var name = date.getTime() + "-" + file.name
    return [day, name ].join("/")
  }

  function createFormData(key, file) {
    var data = new FormData()
    data.append("key", key)
    data.append("Content-Type", file.type)
    data.append("file", file)
    return data
  }
})();
def upload():
    path = current_app.config['UPLOAD_DIRECTORY']
    new_path = request.form["key"].split('/')[0]
    file_upload_name = os.path.join(path, request.form["key"])
    print(file_upload_name)
    upload_path = os.path.join(path, new_path)

    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)

        file = request.files['file']

        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)

        if file and allowed_file(file.filename):
            if not os.path.exists(upload_path):
                os.mkdir(upload_path)

            filename = secure_filename(file.filename)
            session["id"] = filename
            attachment = os.path.join(upload_path, filename)
            file.save(attachment)
            file.close()
            os.rename(attachment, file_upload_name)
            print(os.listdir(upload_path))
            return jsonify({'file': attachment, 'status': 204})
    return f'Nothing to see here'