@@ -3,9 +3,9 @@ import path from 'path'
3
3
import { promisify } from 'util'
4
4
import mime from 'mime-types'
5
5
import prettyBytes from 'pretty-bytes'
6
- import { asyncPipe , CTX , CTXPost } from '../lib'
6
+ import { asyncPipe , CTXPost } from '../lib'
7
7
import { HTTPServerConfig , LoggerInstance } from '@sofie-package-manager/api'
8
- import { BadResponse , Storage } from './storage'
8
+ import { BadResponse , PackageInfo , ResponseMeta , Storage } from './storage'
9
9
import { Readable } from 'stream'
10
10
11
11
// Note: Explicit types here, due to that for some strange reason, promisify wont pass through the correct typings.
@@ -39,19 +39,14 @@ export class FileStorage extends Storage {
39
39
}
40
40
41
41
getInfo ( ) : string {
42
- return this . _basePath
42
+ return `basePath: " ${ this . _basePath } ", cleanFileAge: ${ this . config . httpServer . cleanFileAge } `
43
43
}
44
44
45
45
async init ( ) : Promise < void > {
46
46
await fsMkDir ( this . _basePath , { recursive : true } )
47
47
}
48
48
49
- async listPackages ( ctx : CTX ) : Promise < true | BadResponse > {
50
- type PackageInfo = {
51
- path : string
52
- size : string
53
- modified : string
54
- }
49
+ async listPackages ( ) : Promise < { meta : ResponseMeta ; body : { packages : PackageInfo [ ] } } | BadResponse > {
55
50
const packages : PackageInfo [ ] = [ ]
56
51
57
52
const getAllFiles = async ( basePath : string , dirPath : string ) => {
@@ -84,9 +79,11 @@ export class FileStorage extends Storage {
84
79
return 0
85
80
} )
86
81
87
- ctx . body = { packages : packages }
82
+ const meta : ResponseMeta = {
83
+ statusCode : 200 ,
84
+ }
88
85
89
- return true
86
+ return { meta , body : { packages } }
90
87
}
91
88
private async getFileInfo ( paramPath : string ) : Promise <
92
89
| {
@@ -118,40 +115,40 @@ export class FileStorage extends Storage {
118
115
lastModified : stat . mtime ,
119
116
}
120
117
}
121
- async headPackage ( paramPath : string , ctx : CTX ) : Promise < true | BadResponse > {
118
+ async headPackage ( paramPath : string ) : Promise < { meta : ResponseMeta } | BadResponse > {
122
119
const fileInfo = await this . getFileInfo ( paramPath )
123
120
124
121
if ( ! fileInfo . found ) {
125
122
return { code : 404 , reason : 'Package not found' }
126
123
}
127
124
128
- this . setHeaders ( fileInfo , ctx )
129
-
130
- ctx . response . status = 204
131
-
132
- ctx . body = undefined
125
+ const meta : ResponseMeta = {
126
+ statusCode : 204 ,
127
+ }
128
+ this . updateMetaWithFileInfo ( meta , fileInfo )
133
129
134
- return true
130
+ return { meta }
135
131
}
136
- async getPackage ( paramPath : string , ctx : CTX ) : Promise < true | BadResponse > {
132
+ async getPackage ( paramPath : string ) : Promise < { meta : ResponseMeta ; body : any } | BadResponse > {
137
133
const fileInfo = await this . getFileInfo ( paramPath )
138
134
139
135
if ( ! fileInfo . found ) {
140
136
return { code : 404 , reason : 'Package not found' }
141
137
}
142
-
143
- this . setHeaders ( fileInfo , ctx )
138
+ const meta : ResponseMeta = {
139
+ statusCode : 200 ,
140
+ }
141
+ this . updateMetaWithFileInfo ( meta , fileInfo )
144
142
145
143
const readStream = fs . createReadStream ( fileInfo . fullPath )
146
- ctx . body = readStream
147
144
148
- return true
145
+ return { meta , body : readStream }
149
146
}
150
147
async postPackage (
151
148
paramPath : string ,
152
149
ctx : CTXPost ,
153
150
fileStreamOrText : string | Readable | undefined
154
- ) : Promise < true | BadResponse > {
151
+ ) : Promise < { meta : ResponseMeta ; body : any } | BadResponse > {
155
152
const fullPath = path . join ( this . _basePath , paramPath )
156
153
157
154
await fsMkDir ( path . dirname ( fullPath ) , { recursive : true } )
@@ -164,25 +161,27 @@ export class FileStorage extends Storage {
164
161
plainText = fileStreamOrText
165
162
}
166
163
164
+ const meta : ResponseMeta = {
165
+ statusCode : 200 ,
166
+ }
167
+
167
168
if ( plainText ) {
168
169
// store plain text into file
169
170
await fsWriteFile ( fullPath , plainText )
170
171
171
- ctx . body = { code : 201 , message : `${ exists ? 'Updated' : 'Inserted' } "${ paramPath } "` }
172
- ctx . response . status = 201
173
- return true
172
+ meta . statusCode = 201
173
+ return { meta, body : { code : 201 , message : `${ exists ? 'Updated' : 'Inserted' } "${ paramPath } "` } }
174
174
} else if ( fileStreamOrText && typeof fileStreamOrText !== 'string' ) {
175
175
const fileStream = fileStreamOrText
176
176
await asyncPipe ( fileStream , fs . createWriteStream ( fullPath ) )
177
177
178
- ctx . body = { code : 201 , message : `${ exists ? 'Updated' : 'Inserted' } "${ paramPath } "` }
179
- ctx . response . status = 201
180
- return true
178
+ meta . statusCode = 201
179
+ return { meta, body : { code : 201 , message : `${ exists ? 'Updated' : 'Inserted' } "${ paramPath } "` } }
181
180
} else {
182
181
return { code : 400 , reason : 'No files provided' }
183
182
}
184
183
}
185
- async deletePackage ( paramPath : string , ctx : CTXPost ) : Promise < true | BadResponse > {
184
+ async deletePackage ( paramPath : string ) : Promise < { meta : ResponseMeta ; body : any } | BadResponse > {
186
185
const fullPath = path . join ( this . _basePath , paramPath )
187
186
188
187
if ( ! ( await this . exists ( fullPath ) ) ) {
@@ -191,8 +190,11 @@ export class FileStorage extends Storage {
191
190
192
191
await fsUnlink ( fullPath )
193
192
194
- ctx . body = { message : `Deleted "${ paramPath } "` }
195
- return true
193
+ const meta : ResponseMeta = {
194
+ statusCode : 200 ,
195
+ }
196
+
197
+ return { meta, body : { message : `Deleted "${ paramPath } "` } }
196
198
}
197
199
198
200
private async exists ( fullPath : string ) {
@@ -280,21 +282,23 @@ export class FileStorage extends Storage {
280
282
* @param {CTX } ctx
281
283
* @memberof FileStorage
282
284
*/
283
- private setHeaders ( info : FileInfo , ctx : CTX ) {
284
- ctx . type = info . mimeType
285
- ctx . length = info . length
286
- ctx . lastModified = info . lastModified
285
+ private updateMetaWithFileInfo ( meta : ResponseMeta , info : FileInfo ) : void {
286
+ meta . type = info . mimeType
287
+ meta . length = info . length
288
+ meta . lastModified = info . lastModified
289
+
290
+ if ( ! meta . headers ) meta . headers = { }
287
291
288
292
// Check the config. 0 or -1 means it's disabled:
289
293
if ( this . config . httpServer . cleanFileAge >= 0 ) {
290
- ctx . set (
291
- 'Expires' ,
292
- FileStorage . calculateExpiresTimestamp ( info . lastModified , this . config . httpServer . cleanFileAge )
294
+ meta . headers [ 'Expires' ] = FileStorage . calculateExpiresTimestamp (
295
+ info . lastModified ,
296
+ this . config . httpServer . cleanFileAge
293
297
)
294
298
}
295
299
}
296
300
/**
297
- * Calculate the expiration timestamp, given a starting Date point and timespan duration
301
+ * Calculate the expiration timestamp, given a starting Date point and time-span duration
298
302
*
299
303
* @private
300
304
* @static
0 commit comments