From c89567991bae104d5a88a1fd297adb6fe92e64bb Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 11 Sep 2022 14:32:38 +0100 Subject: [PATCH] fix: Unsure filename when downloading an untitled document fix: Unsure unique filename when downloading revision --- server/models/Document.ts | 4 ++++ server/routes/api/documents.ts | 6 +++--- server/routes/api/revisions.ts | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/server/models/Document.ts b/server/models/Document.ts index 26b40fa07..8d5208722 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -759,6 +759,10 @@ class Document extends ParanoidModel { return undefined; }; + get titleWithDefault(): string { + return this.title || "Untitled"; + } + /** * Get a list of users that have collaborated on this document * diff --git a/server/routes/api/documents.ts b/server/routes/api/documents.ts index e0e3637cc..5c7bc1385 100644 --- a/server/routes/api/documents.ts +++ b/server/routes/api/documents.ts @@ -472,9 +472,9 @@ router.post( ctx.set("Content-Type", contentType); ctx.set( "Content-Disposition", - `attachment; filename="${slugify(document.title)}.${mime.extension( - contentType - )}"` + `attachment; filename="${slugify( + document.titleWithDefault + )}.${mime.extension(contentType)}"` ); ctx.body = content; return; diff --git a/server/routes/api/revisions.ts b/server/routes/api/revisions.ts index 621f32274..c7c681941 100644 --- a/server/routes/api/revisions.ts +++ b/server/routes/api/revisions.ts @@ -79,7 +79,9 @@ router.post("revisions.diff", auth(), async (ctx) => { ctx.set("Content-Type", "text/html"); ctx.set( "Content-Disposition", - `attachment; filename="${slugify(document.title)}.html"` + `attachment; filename="${slugify(document.titleWithDefault)}-${ + revision.id + }.html"` ); ctx.body = content; return;