1313 * limitations under the License.
1414 */
1515
16- import { shadow , stringToPDFString , warn } from "../shared/util.js" ;
16+ import { stringToPDFString , warn } from "../shared/util.js" ;
1717import { BaseStream } from "./base_stream.js" ;
1818import { Dict } from "./primitives.js" ;
1919
2020function pickPlatformItem ( dict ) {
21- if ( ! ( dict instanceof Dict ) ) {
22- return null ;
23- }
24- // Look for the filename in this order:
25- // UF, F, Unix, Mac, DOS
26- if ( dict . has ( "UF" ) ) {
27- return dict . get ( "UF" ) ;
28- } else if ( dict . has ( "F" ) ) {
29- return dict . get ( "F" ) ;
30- } else if ( dict . has ( "Unix" ) ) {
31- return dict . get ( "Unix" ) ;
32- } else if ( dict . has ( "Mac" ) ) {
33- return dict . get ( "Mac" ) ;
34- } else if ( dict . has ( "DOS" ) ) {
35- return dict . get ( "DOS" ) ;
21+ if ( dict instanceof Dict ) {
22+ // Look for the filename in this order: UF, F, Unix, Mac, DOS
23+ for ( const key of [ "UF" , "F" , "Unix" , "Mac" , "DOS" ] ) {
24+ if ( dict . has ( key ) ) {
25+ return dict . get ( key ) ;
26+ }
27+ }
3628 }
3729 return null ;
3830}
@@ -51,11 +43,10 @@ function stripPath(str) {
5143class FileSpec {
5244 #contentAvailable = false ;
5345
54- constructor ( root , xref , skipContent = false ) {
46+ constructor ( root , skipContent = false ) {
5547 if ( ! ( root instanceof Dict ) ) {
5648 return ;
5749 }
58- this . xref = xref ;
5950 this . root = root ;
6051 if ( root . has ( "FS" ) ) {
6152 this . fs = root . get ( "FS" ) ;
@@ -73,56 +64,46 @@ class FileSpec {
7364 }
7465
7566 get filename ( ) {
76- let filename = "" ;
77-
7867 const item = pickPlatformItem ( this . root ) ;
7968 if ( item && typeof item === "string" ) {
80- filename = stringToPDFString ( item , /* keepEscapeSequence = */ true )
69+ // NOTE: The following replacement order is INTENTIONAL, regardless of
70+ // what some static code analysers (e.g. CodeQL) may claim.
71+ return stringToPDFString ( item , /* keepEscapeSequence = */ true )
8172 . replaceAll ( "\\\\" , "\\" )
8273 . replaceAll ( "\\/" , "/" )
8374 . replaceAll ( "\\" , "/" ) ;
8475 }
85- return shadow ( this , "filename" , filename || "unnamed" ) ;
76+ return "" ;
8677 }
8778
8879 get content ( ) {
8980 if ( ! this . #contentAvailable) {
9081 return null ;
9182 }
92- this . _contentRef || = pickPlatformItem ( this . root ?. get ( "EF" ) ) ;
83+ const ef = pickPlatformItem ( this . root ?. get ( "EF" ) ) ;
9384
94- let content = null ;
95- if ( this . _contentRef ) {
96- const fileObj = this . xref . fetchIfRef ( this . _contentRef ) ;
97- if ( fileObj instanceof BaseStream ) {
98- content = fileObj . getBytes ( ) ;
99- } else {
100- warn (
101- "Embedded file specification points to non-existing/invalid content"
102- ) ;
103- }
104- } else {
105- warn ( "Embedded file specification does not have any content" ) ;
85+ if ( ef instanceof BaseStream ) {
86+ return ef . getBytes ( ) ;
10687 }
107- return content ;
88+ warn ( "Embedded file specification points to non-existing/invalid content" ) ;
89+ return null ;
10890 }
10991
11092 get description ( ) {
111- let description = "" ;
112-
11393 const desc = this . root ?. get ( "Desc" ) ;
11494 if ( desc && typeof desc === "string" ) {
115- description = stringToPDFString ( desc ) ;
95+ return stringToPDFString ( desc ) ;
11696 }
117- return shadow ( this , "description" , description ) ;
97+ return "" ;
11898 }
11999
120100 get serializable ( ) {
101+ const { filename, content, description } = this ;
121102 return {
122- rawFilename : this . filename ,
123- filename : stripPath ( this . filename ) ,
124- content : this . content ,
125- description : this . description ,
103+ rawFilename : filename ,
104+ filename : stripPath ( filename ) || "unnamed" ,
105+ content,
106+ description,
126107 } ;
127108 }
128109}
0 commit comments