1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Merge pull request #18034 from Snuffleupagus/FileSpec-filename-stripPath

[api-minor] Improve the `FileSpec` implementation
This commit is contained in:
Jonas Jenwald 2024-05-03 09:03:17 +02:00 committed by GitHub
commit 1b811ac113
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 49 additions and 58 deletions

View file

@ -1619,8 +1619,8 @@ class Catalog {
/* xref = */ null,
/* skipContent = */ true
);
const { filename } = fs.serializable;
url = filename;
const { rawFilename } = fs.serializable;
url = rawFilename;
} else if (typeof urlDict === "string") {
url = urlDict;
}

View file

@ -18,6 +18,9 @@ import { BaseStream } from "./base_stream.js";
import { Dict } from "./primitives.js";
function pickPlatformItem(dict) {
if (!(dict instanceof Dict)) {
return null;
}
// Look for the filename in this order:
// UF, F, Unix, Mac, DOS
if (dict.has("UF")) {
@ -34,6 +37,10 @@ function pickPlatformItem(dict) {
return null;
}
function stripPath(str) {
return str.substring(str.lastIndexOf("/") + 1);
}
/**
* "A PDF file can refer to the contents of another file by using a File
* Specification (PDF 1.1)", see the spec (7.11) for more details.
@ -66,26 +73,27 @@ class FileSpec {
}
get filename() {
if (!this._filename && this.root) {
const filename = pickPlatformItem(this.root) || "unnamed";
this._filename = stringToPDFString(filename)
let filename = "";
const item = pickPlatformItem(this.root);
if (item && typeof item === "string") {
filename = stringToPDFString(item)
.replaceAll("\\\\", "\\")
.replaceAll("\\/", "/")
.replaceAll("\\", "/");
}
return this._filename;
return shadow(this, "filename", filename || "unnamed");
}
get content() {
if (!this.#contentAvailable) {
return null;
}
if (!this.contentRef && this.root) {
this.contentRef = pickPlatformItem(this.root.get("EF"));
}
this._contentRef ||= pickPlatformItem(this.root?.get("EF"));
let content = null;
if (this.contentRef) {
const fileObj = this.xref.fetchIfRef(this.contentRef);
if (this._contentRef) {
const fileObj = this.xref.fetchIfRef(this._contentRef);
if (fileObj instanceof BaseStream) {
content = fileObj.getBytes();
} else {
@ -94,7 +102,7 @@ class FileSpec {
);
}
} else {
warn("Embedded file specification does not have a content");
warn("Embedded file specification does not have any content");
}
return content;
}
@ -111,7 +119,8 @@ class FileSpec {
get serializable() {
return {
filename: this.filename,
rawFilename: this.filename,
filename: stripPath(this.filename),
content: this.content,
description: this.description,
};

View file

@ -37,7 +37,6 @@ import {
} from "../shared/util.js";
import {
DOMSVGFactory,
getFilenameFromUrl,
PDFDateString,
setLayerDimensions,
} from "./display_utils.js";
@ -2859,15 +2858,13 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
constructor(parameters) {
super(parameters, { isRenderable: true });
const { filename, content, description } = this.data.file;
this.filename = getFilenameFromUrl(filename, /* onlyStripPath = */ true);
this.content = content;
const { file } = this.data;
this.filename = file.filename;
this.content = file.content;
this.linkService.eventBus?.dispatch("fileattachmentannotation", {
source: this,
filename,
content,
description,
...file,
});
}

View file

@ -803,13 +803,10 @@ function isPdfFile(filename) {
/**
* Gets the filename from a given URL.
* @param {string} url
* @param {boolean} [onlyStripPath]
* @returns {string}
*/
function getFilenameFromUrl(url, onlyStripPath = false) {
if (!onlyStripPath) {
[url] = url.split(/[#?]/, 1);
}
function getFilenameFromUrl(url) {
[url] = url.split(/[#?]/, 1);
return url.substring(url.lastIndexOf("/") + 1);
}