@@ -30,24 +30,49 @@ class DocumentReferenceHandler extends stream_1.Transform {
30
30
} ) ;
31
31
this . options = options ;
32
32
}
33
- async downloadAttachment ( url ) {
34
- if ( url . search ( / ^ h t t p s ? : \/ \/ . + / ) === - 1 ) {
35
- url = new url_1 . URL ( url , this . options . baseUrl ) . href ;
33
+ async downloadAttachment ( attachment ) {
34
+ if ( ! attachment . url ) {
35
+ throw new Error ( "DocumentReferenceHandler.downloadAttachment called on attachment that has no 'url'" ) ;
36
36
}
37
+ // If the url is relative then convert it to absolute on the same base
38
+ const url = new url_1 . URL ( attachment . url , this . options . baseUrl ) ;
39
+
37
40
const res = await this . options . request ( {
38
41
url,
39
42
responseType : "buffer" ,
40
- throwHttpErrors : false
43
+ throwHttpErrors : false ,
44
+ headers : {
45
+ accept : attachment . contentType || "application/json+fhir"
46
+ }
41
47
} ) ;
48
+
42
49
if ( res . statusCode >= 400 ) {
43
50
throw new errors_1 . FileDownloadError ( {
44
- fileUrl : url ,
51
+ fileUrl : attachment . url ,
45
52
body : null ,
46
53
code : res . statusCode
47
54
} ) ;
48
55
}
49
- this . options . onDownloadComplete ( url , res . body . byteLength ) ;
50
- return res ;
56
+
57
+ const contentType = res . headers [ "content-type" ] || "" ;
58
+
59
+ // We may have gotten back a Binary FHIR resource
60
+ if ( contentType . match ( / \b a p p l i c a t i o n \/ j s o n ( \+ f h i r ) ? \b / ) ) {
61
+ const json = JSON . parse ( res . body . toString ( "utf8" ) ) ;
62
+ const { resourceType, contentType, data } = json ;
63
+ if ( resourceType === "Binary" ) {
64
+ const buffer = Buffer . from ( data , "base64" ) ;
65
+ this . options . onDownloadComplete ( attachment . url , buffer . byteLength ) ;
66
+ return { contentType, data : buffer } ;
67
+ }
68
+ }
69
+
70
+ this . options . onDownloadComplete ( attachment . url , res . body . byteLength ) ;
71
+
72
+ return {
73
+ contentType : contentType || attachment . contentType || "" ,
74
+ data : res . body
75
+ } ;
51
76
}
52
77
async inlineAttachmentData ( node , data ) {
53
78
if ( node . contentType == "application/pdf" && this . options . pdfToText ) {
@@ -65,28 +90,27 @@ class DocumentReferenceHandler extends stream_1.Transform {
65
90
if ( ! attachment . url ) {
66
91
continue ;
67
92
}
68
- const response = await this . downloadAttachment ( attachment . url ) ;
69
- if ( this . canPutAttachmentInline ( response , attachment . contentType ) ) {
70
- await this . inlineAttachmentData ( attachment , response . body ) ;
93
+ const response = await this . downloadAttachment ( attachment ) ;
94
+ if ( this . canPutAttachmentInline ( response . data , response . contentType ) ) {
95
+ await this . inlineAttachmentData ( attachment , response . data ) ;
71
96
}
72
97
else {
73
98
const fileName = Date . now ( ) + "-" + node_jose_1 . default . util . randomBytes ( 6 ) . toString ( "hex" ) + ( 0 , path_1 . extname ) ( attachment . url ) ;
74
- await this . options . save ( fileName , stream_1 . Readable . from ( response . body ) , "attachments" ) ;
99
+ await this . options . save ( fileName , stream_1 . Readable . from ( response . data ) , "attachments" ) ;
75
100
attachment . url = `./attachments/${ fileName } ` ;
76
101
}
77
102
this . emit ( "attachment" ) ;
78
103
}
79
104
return resource ;
80
105
}
81
- canPutAttachmentInline ( response , contentType ) {
82
- if ( response . body . byteLength > this . options . inlineAttachments ) {
106
+ canPutAttachmentInline ( data , contentType ) {
107
+ if ( data . byteLength > this . options . inlineAttachments ) {
83
108
return false ;
84
109
}
85
- const type = contentType || response . headers [ "content-type" ] || "" ;
86
- if ( ! type ) {
110
+ if ( ! contentType ) {
87
111
return false ;
88
112
}
89
- if ( ! this . options . inlineAttachmentTypes . find ( m => type . startsWith ( m ) ) ) {
113
+ if ( ! this . options . inlineAttachmentTypes . find ( m => contentType . startsWith ( m ) ) ) {
90
114
return false ;
91
115
}
92
116
return true ;
0 commit comments