@@ -42,10 +42,17 @@ function makeAssetCachePath(cacheDir, cacheKey) {
42
42
} ;
43
43
}
44
44
45
- function makeAssetCacheName ( contentType , contentLength ) {
45
+ function encodeAssetCacheName ( contentType , contentLength ) {
46
46
return Buffer . from ( `${ contentType } :${ contentLength } ` ) . toString ( "base64" ) ;
47
47
}
48
48
49
+ function decodeAssetCacheName ( encodedString ) {
50
+ const decodedFileName = Buffer . from ( encodedString , "base64" ) . toString (
51
+ "ascii"
52
+ ) ;
53
+ return decodedFileName . split ( ":" ) ;
54
+ }
55
+
49
56
const middleWare = ( module . exports = function ( options ) {
50
57
return async function ( req , res , next ) {
51
58
options = options || { } ;
@@ -66,38 +73,36 @@ const middleWare = (module.exports = function(options) {
66
73
middleWare . makeDirIfNotExists ( path . join ( options . cacheDir , dir1 , dir2 ) ) ;
67
74
68
75
if ( fs . existsSync ( assetCachePath ) ) {
76
+ const firstFile = fs . readdirSync ( assetCachePath ) [ 0 ] ;
77
+
69
78
if ( options . logger )
70
- options . logger . debug ( `Reading buffer from path ${ assetCachePath } ` ) ;
79
+ options . logger . debug (
80
+ `Reading buffer from path ${ assetCachePath } /${ firstFile } `
81
+ ) ;
71
82
72
- const firstFile = fs . readdirSync ( assetCachePath ) [ 0 ] ;
73
- const decodedFileName = Buffer . from ( "firstFile" , "base64" ) . toString (
74
- "ascii"
83
+ const [ contentType , contentLength ] = middleWare . decodeAssetCacheName (
84
+ firstFile
75
85
) ;
76
86
77
- const [ contentType , contentLength ] = decodedFileName . split ( ":" ) ;
78
-
79
87
res . locals . contentLength = contentLength ;
80
88
res . locals . contentType = contentType ;
81
89
82
90
res . locals . buffer = fs . readFileSync ( `${ assetCachePath } /${ firstFile } ` ) ;
83
91
} else {
84
92
const blob = await ( await fetch ( res . locals . fetchUrl ) ) . blob ( ) ;
85
93
94
+ const fileName = middleWare . encodeAssetCacheName ( blob . type , blob . size ) ;
86
95
if ( options . logger )
87
- options . logger . debug ( `Writing buffer to path ${ assetCachePath } ` ) ;
96
+ options . logger . debug (
97
+ `Writing buffer to path ${ assetCachePath } /${ fileName } `
98
+ ) ;
88
99
89
100
res . locals . buffer = Buffer . from ( await blob . arrayBuffer ( ) , "binary" ) ;
90
101
91
102
res . locals . contentType = blob . type ;
92
103
res . locals . contentLength = blob . size ;
93
104
94
- fs . writeFileSync (
95
- `${ assetCachePath } /${ middleWare . makeAssetCacheName (
96
- blob . type ,
97
- blob . size
98
- ) } `,
99
- res . locals . buffer
100
- ) ;
105
+ fs . writeFileSync ( `${ assetCachePath } /${ fileName } ` , res . locals . buffer ) ;
101
106
}
102
107
103
108
next ( ) ;
@@ -120,4 +125,5 @@ const middleWare = (module.exports = function(options) {
120
125
121
126
middleWare . makeAssetCachePath = makeAssetCachePath ;
122
127
middleWare . makeDirIfNotExists = makeDirIfNotExists ;
123
- middleWare . makeAssetCacheName = makeAssetCacheName ;
128
+ middleWare . encodeAssetCacheName = encodeAssetCacheName ;
129
+ middleWare . decodeAssetCacheName = decodeAssetCacheName ;
0 commit comments