FIX: Fetch csrf token if needed before uploading (#23825)

This commit is contained in:
Jarek Radosz 2023-10-09 15:47:36 +02:00 committed by GitHub
parent f13f2fecfe
commit 53c9c9c1e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import { cacheShortUploadUrl } from "pretty-text/upload-short-url";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
import { run } from "@ember/runloop"; import { run } from "@ember/runloop";
import escapeRegExp from "discourse-common/utils/escape-regexp"; import escapeRegExp from "discourse-common/utils/escape-regexp";
import { updateCsrfToken } from "discourse/lib/ajax";
// Note: This mixin is used _in addition_ to the ComposerUpload mixin // Note: This mixin is used _in addition_ to the ComposerUpload mixin
// on the composer-editor component. It overrides some, but not all, // on the composer-editor component. It overrides some, but not all,
@ -39,6 +40,8 @@ import escapeRegExp from "discourse-common/utils/escape-regexp";
// //
export default Mixin.create(ExtendableUploader, UppyS3Multipart, { export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
dialog: service(), dialog: service(),
session: service(),
uploadRootPath: "/uploads", uploadRootPath: "/uploads",
uploadTargetBound: false, uploadTargetBound: false,
useUploadPlaceholders: true, useUploadPlaceholders: true,
@ -596,8 +599,13 @@ export default Mixin.create(ExtendableUploader, UppyS3Multipart, {
}, },
@bind @bind
_addFiles(files, opts = {}) { async _addFiles(files, opts = {}) {
if (!this.session.csrfToken) {
await updateCsrfToken();
}
files = Array.isArray(files) ? files : [files]; files = Array.isArray(files) ? files : [files];
try { try {
this._uppyInstance.addFiles( this._uppyInstance.addFiles(
files.map((file) => { files.map((file) => {

View File

@ -3,7 +3,7 @@ import { run } from "@ember/runloop";
import ExtendableUploader from "discourse/mixins/extendable-uploader"; import ExtendableUploader from "discourse/mixins/extendable-uploader";
import { or } from "@ember/object/computed"; import { or } from "@ember/object/computed";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { ajax } from "discourse/lib/ajax"; import { ajax, updateCsrfToken } from "discourse/lib/ajax";
import { import {
bindFileInputChangeListener, bindFileInputChangeListener,
displayErrorForUpload, displayErrorForUpload,
@ -429,8 +429,13 @@ export default Mixin.create(UppyS3Multipart, ExtendableUploader, {
}, },
@bind @bind
_addFiles(files, opts = {}) { async _addFiles(files, opts = {}) {
if (!this.session.csrfToken) {
await updateCsrfToken();
}
files = Array.isArray(files) ? files : [files]; files = Array.isArray(files) ? files : [files];
try { try {
this._uppyInstance.addFiles( this._uppyInstance.addFiles(
files.map((file) => { files.map((file) => {