Skip to content

Commit 9648097

Browse files
authored
Merge pull request #305 from embedpdf/fix/document-extension
Fixed document name extraction to always include `.pdf` extension whe…
2 parents 22b9e03 + fb988c4 commit 9648097

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

.changeset/friendly-pens-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@embedpdf/plugin-document-manager': patch
3+
---
4+
5+
Fixed document name extraction to always include `.pdf` extension when extracting filename from URL.

.changeset/loud-doors-shine.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@embedpdf/plugin-document-manager': patch
3+
---
4+
5+
Added optional `name` property to `LoadDocumentUrlOptions` to allow specifying a custom document name. When not provided, the name is extracted from the URL. If extraction fails, `undefined` is returned to allow downstream handling of default names.

packages/plugin-document-manager/src/lib/document-manager-plugin.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class DocumentManagerPlugin extends BasePlugin<
237237
return task;
238238
}
239239

240-
const documentName = this.extractNameFromUrl(options.url);
240+
const documentName = options.name ?? this.extractNameFromUrl(options.url);
241241

242242
// Store options for potential retry
243243
this.loadOptions.set(documentId, options);
@@ -585,14 +585,21 @@ export class DocumentManagerPlugin extends BasePlugin<
585585
return `doc-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
586586
}
587587

588-
private extractNameFromUrl(url: string): string {
588+
private extractNameFromUrl(url: string): string | undefined {
589589
try {
590590
const urlObj = new URL(url);
591591
const pathname = urlObj.pathname;
592-
const filename = pathname.split('/').pop() || 'document.pdf';
593-
return decodeURIComponent(filename);
592+
const lastSegment = pathname.split('/').pop();
593+
if (!lastSegment) {
594+
return undefined;
595+
}
596+
let filename = decodeURIComponent(lastSegment);
597+
if (!filename.toLowerCase().endsWith('.pdf')) {
598+
filename += '.pdf';
599+
}
600+
return filename;
594601
} catch {
595-
return 'document.pdf';
602+
return undefined;
596603
}
597604
}
598605

packages/plugin-document-manager/src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface DocumentErrorEvent {
3737
export interface LoadDocumentUrlOptions {
3838
url: string;
3939
documentId?: string;
40+
name?: string; // Optional display name for the document. If not provided, extracted from URL
4041
password?: string;
4142
mode?: 'auto' | 'range-request' | 'full-fetch';
4243
requestOptions?: PdfRequestOptions;

0 commit comments

Comments
 (0)