@@ -7,20 +7,27 @@ const fse = require('fs-extra');
7
7
const Q = require ( 'q' ) ;
8
8
9
9
module . exports = config => {
10
- return {
11
- createImage ( stream , filePath ) {
10
+ const mediaObj = {
11
+ createImage ( stream , filename , filePath , options = { } ) {
12
+ const defaultOpts = { addTimestamp : true , ext : null } ;
13
+ const mergedOpts = Object . assign ( defaultOpts , options ) ;
12
14
const deferred = Q . defer ( ) ;
13
- const timestamp = new Date ( ) . getTime ( ) . toString ( ) ;
14
- const fileExtension = path . extname ( stream . hapi . filename ) ;
15
- const fileName = slug (
16
- stream . hapi . filename . substr ( 0 , stream . hapi . filename . length - fileExtension . length )
17
- ) ;
15
+ const fileExtension = path . extname ( filename ) ;
16
+ const file = slug ( filename . substr ( 0 , filename . length - fileExtension . length ) ) ;
18
17
const directoryBase = `${ config . publicImgFolder } /${ filePath } ` ;
19
- let imgPath = `${ directoryBase } /${ timestamp + fileName } ` ;
18
+ let imgPath = `${ directoryBase } /` ;
20
19
21
- if ( path . extname ( fileName ) === '' && stream . hapi . headers [ 'content-type' ] ) {
22
- const [ , ext ] = stream . hapi . headers [ 'content-type' ] . split ( '/' ) ;
23
- imgPath = `${ imgPath } .${ ext } ` ;
20
+ if ( mergedOpts . addTimestamp === true ) {
21
+ const timestamp = new Date ( ) . getTime ( ) . toString ( ) ;
22
+ imgPath = `${ imgPath } ${ timestamp } -${ file } ` ;
23
+ } else {
24
+ imgPath = `${ imgPath } ${ file } ` ;
25
+ }
26
+
27
+ if ( fileExtension === '' && mergedOpts . ext !== null ) {
28
+ imgPath = `${ imgPath } .${ mergedOpts . ext } ` ;
29
+ } else {
30
+ imgPath = `${ imgPath } ${ fileExtension } ` ;
24
31
}
25
32
26
33
// make sure the path exists so we can write to it
@@ -44,25 +51,24 @@ module.exports = config => {
44
51
return deferred . promise ;
45
52
} ,
46
53
47
- createFile ( stream , directoryPath , addTimestamp = true ) {
54
+ createFile ( stream , filename , directoryPath , options = { } ) {
55
+ const defaultOpts = { addTimestamp : true , ext : null } ;
56
+ const mergedOpts = Object . assign ( defaultOpts , options ) ;
48
57
const deferred = Q . defer ( ) ;
49
- let fileExtension = path . extname ( stream . hapi . filename ) ;
50
- const fileName = slug (
51
- stream . hapi . filename . substr ( 0 , stream . hapi . filename . length - fileExtension . length )
52
- ) ;
58
+ const fileExtension = path . extname ( filename ) ;
59
+ const file = slug ( filename . substr ( 0 , filename . length - fileExtension . length ) ) ;
53
60
const directoryBase = `${ config . publicFileFolder } /${ directoryPath } ` ;
54
61
let filePath = `${ directoryBase } /` ;
55
62
56
- if ( addTimestamp === true ) {
63
+ if ( mergedOpts . addTimestamp === true ) {
57
64
const timestamp = new Date ( ) . getTime ( ) . toString ( ) ;
58
- filePath = `${ filePath } ${ timestamp } -${ fileName } ` ;
65
+ filePath = `${ filePath } ${ timestamp } -${ file } ` ;
59
66
} else {
60
- filePath = `${ filePath } ${ fileName } ` ;
67
+ filePath = `${ filePath } ${ file } ` ;
61
68
}
62
69
63
- if ( fileExtension === '' && stream . hapi . headers [ 'content-type' ] ) {
64
- const [ , ext ] = stream . hapi . headers [ 'content-type' ] . split ( '/' ) ;
65
- filePath = `${ filePath } .${ ext } ` ;
70
+ if ( fileExtension === '' && mergedOpts . ext !== null ) {
71
+ filePath = `${ filePath } .${ mergedOpts . ext } ` ;
66
72
} else {
67
73
filePath = `${ filePath } ${ fileExtension } ` ;
68
74
}
@@ -104,10 +110,11 @@ module.exports = config => {
104
110
/**
105
111
* Create an archive from a list of path
106
112
* @param {string } archiveName Archive name
113
+ * @param {string } directoryPath Path to the directory where to create the archive
107
114
* @param {array of string } paths File paths array
108
- * @param {object } request Hapi's request object for log matter
115
+ * @param {function } log logger function
109
116
*/
110
- createArchive ( archiveName , directoryPath , paths , request ) {
117
+ createArchive ( archiveName , directoryPath , paths , log ) {
111
118
const archiveDest = path . resolve ( config . publicFileFolder , directoryPath ) ;
112
119
const zipPath = path . resolve ( archiveDest , archiveName ) ;
113
120
const archive = archiver ( 'zip' , { zlib : { level : 9 } } ) ;
@@ -126,14 +133,14 @@ module.exports = config => {
126
133
} ) ;
127
134
128
135
archive . on ( 'end' , ( ) => {
129
- request . log ( [ 'log' ] , `Archive "${ archiveName } " of ${ archive . pointer ( ) } bytes written` ) ;
136
+ log ( [ 'log' ] , `Archive "${ archiveName } " of ${ archive . pointer ( ) } bytes written` ) ;
130
137
resolve ( zipPath ) ;
131
138
} ) ;
132
139
133
140
// good practice to catch warnings (ie stat failures and other non-blocking errors)
134
141
archive . on ( 'warning' , err => {
135
142
if ( err . code === 'ENOENT' ) {
136
- return request . log ( [ 'warning' , 'archive' ] , err ) ;
143
+ return log ( [ 'warning' , 'archive' ] , err ) ;
137
144
}
138
145
139
146
output . close ( ) ;
@@ -159,4 +166,6 @@ module.exports = config => {
159
166
} ) ;
160
167
} ,
161
168
} ;
169
+
170
+ return mediaObj ;
162
171
} ;
0 commit comments