upload: Name the link based on the uploaded filename.

We previously parsed the filename as stored on disk, which is rather
sanitized.  Use as close to the original filename as possible.
This commit is contained in:
Alex Vandiver
2024-08-30 03:03:24 +00:00
committed by Tim Abbott
parent 8e5cdcc174
commit 8bd94b82bf
2 changed files with 8 additions and 4 deletions

View File

@@ -372,10 +372,13 @@ export function setup_upload(config: Config): Uppy {
uppy.on("upload-success", (file, response) => { uppy.on("upload-success", (file, response) => {
assert(file !== undefined); assert(file !== undefined);
const {url} = z.object({url: z.string()}).parse(response.body); const {url, filename} = z
const split_url = url.split("/"); .object({url: z.string(), filename: z.string()})
const filename = split_url.at(-1); .parse(response.body);
const syntax_to_insert = "[" + filename + "](" + url + ")"; // Our markdown does not have escape characters, so we cannot link any text with brackets;
// strip them out, if present.
const filtered_filename = filename.replaceAll("[", "").replaceAll("]", "");
const syntax_to_insert = "[" + filtered_filename + "](" + url + ")";
const $text_area = config.textarea(); const $text_area = config.textarea();
const replacement_successful = compose_ui.replace_syntax( const replacement_successful = compose_ui.replace_syntax(
get_translated_status(file), get_translated_status(file),

View File

@@ -501,6 +501,7 @@ test("uppy_events", ({override_rewire, mock_template}) => {
let response = { let response = {
body: { body: {
url: "/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png", url: "/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png",
filename: "copenhagen.png",
}, },
}; };