@@ -27,7 +27,10 @@ if Meteor.isServer
27
27
###
28
28
class writeStream
29
29
constructor : (@path , @maxLength , @file ) ->
30
+ if not @path or not _ .isString @path
31
+ return
30
32
self = @
33
+ fs .ensureFileSync @path
31
34
@stream = fs .createWriteStream @path , {flags : ' a' , mode : self .permissions , highWaterMark : 0 }
32
35
@drained = true
33
36
@aborted = false
@@ -493,7 +496,17 @@ class FilesCollection
493
496
@ddp ?= Meteor
494
497
@onInitiateUpload ?= false
495
498
@interceptDownload ?= false
499
+ storagePath ?= -> " assets#{ nodePath .sep } app#{ nodePath .sep } uploads#{ nodePath .sep }#{ @collectionName } "
496
500
501
+ if _ .isString storagePath
502
+ @ storagePath = -> storagePath
503
+ else
504
+ @ storagePath = ->
505
+ sp = storagePath .apply @ , arguments
506
+ unless _ .isString sp
507
+ throw new Meteor.Error 400 , " [FilesCollection.#{ self .collectionName } ] \" storagePath\" function must return a String!"
508
+ sp = sp .replace / \/ $ / , ' '
509
+ return if Meteor .isServer then nodePath .normalize (sp) else sp
497
510
498
511
if Meteor .isClient
499
512
@onbeforeunloadMessage ?= ' Upload in a progress... Do you want to abort?'
@@ -577,36 +590,20 @@ class FilesCollection
577
590
headers[' Accept-Ranges' ] = ' bytes'
578
591
return headers
579
592
580
- if @public and not storagePath
593
+ if @public and ( not storagePath or not _ . isString (storagePath))
581
594
throw new Meteor.Error 500 , " [FilesCollection.#{ @collectionName } ] \" storagePath\" must be set on \" public\" collections! Note: \" storagePath\" must be equal on be inside of your web/proxy-server (absolute) root."
582
595
583
- storagePath ?= " assets#{ nodePath .sep } app#{ nodePath .sep } uploads#{ nodePath .sep }#{ @collectionName } "
584
- Object .defineProperty self, ' storagePath' , {
585
- get : ->
586
- sp = ' '
587
- if _ .isString storagePath
588
- sp = storagePath
589
- else if _ .isFunction storagePath
590
- sp = storagePath .call self, " assets#{ nodePath .sep } app#{ nodePath .sep } uploads#{ nodePath .sep }#{ self .collectionName } "
591
-
592
- unless _ .isString sp
593
- throw new Meteor.Error 400 , " [FilesCollection.#{ self .collectionName } ] \" storagePath\" function must return a String!"
594
-
595
- sp = sp .replace / \/ $ / , ' '
596
- return nodePath .normalize sp
597
- }
598
-
599
596
console .info (' [FilesCollection.storagePath] Set to:' , @storagePath ) if @debug
600
597
601
- fs .mkdirs @storagePath , {mode : @parentDirPermissions }, (error ) ->
598
+ fs .mkdirs @ storagePath ({}) , {mode : @parentDirPermissions }, (error ) ->
602
599
if error
603
600
throw new Meteor.Error 401 , " [FilesCollection.#{ self .collectionName } ] Path \" #{ self .storagePath } \" is not writable!" , error
604
601
return
605
602
606
603
check @strict , Boolean
607
604
check @throttle , Match .OneOf false , Number
608
605
check @permissions , Number
609
- check @storagePath , String
606
+ check @storagePath , Function
610
607
check @cacheControl , String
611
608
check @onAfterRemove , Match .OneOf false , Function
612
609
check @onAfterUpload , Match .OneOf false , Function
@@ -961,7 +958,7 @@ class FilesCollection
961
958
if _continueUpload
962
959
self ._preCollection .remove {_id}
963
960
self .remove {_id}
964
- self .unlink {_id, path : _continueUpload .file .path }
961
+ self .unlink {_id, path : _continueUpload .file .path } if _continueUpload ? . file ? . path
965
962
return true
966
963
967
964
Meteor .methods _methods
@@ -979,21 +976,22 @@ class FilesCollection
979
976
opts .chunkId ?= - 1
980
977
opts .FSName ?= opts .fileId
981
978
opts .file .meta ?= {}
979
+ console .warn " >>>>> opts.file.meta" , opts .file .meta
982
980
983
981
console .info " [FilesCollection] [Upload] [#{ transport} ] Got ##{ opts .chunkId } /#{ opts .fileLength } chunks, dst: #{ opts .file .name or opts .file .fileName } " if @debug
984
982
985
983
fileName = @ _getFileName opts .file
986
984
{extension , extensionWithDot } = @ _getExt fileName
987
985
988
986
result = opts .file
989
- result .path = " #{ @storagePath }#{ nodePath .sep }#{ opts .FSName }#{ extensionWithDot} "
990
987
result .name = fileName
991
988
result .meta = opts .file .meta
992
989
result .extension = extension
993
990
result .ext = extension
994
- result = @ _dataToSchema result
995
991
result ._id = opts .fileId
996
992
result .userId = userId or null
993
+ result .path = " #{ @ storagePath (result)}#{ nodePath .sep }#{ opts .FSName }#{ extensionWithDot} "
994
+ result = _ .extend result, @ _dataToSchema result
997
995
998
996
if @onBeforeUpload and _ .isFunction @onBeforeUpload
999
997
ctx = _ .extend {
@@ -1167,7 +1165,7 @@ class FilesCollection
1167
1165
@returns {Object}
1168
1166
###
1169
1167
_dataToSchema : (data ) ->
1170
- return {
1168
+ ds =
1171
1169
name : data .name
1172
1170
extension : data .extension
1173
1171
path : data .path
@@ -1186,10 +1184,10 @@ class FilesCollection
1186
1184
isText : / ^ text\/ / i .test data .type
1187
1185
isJSON : / application\/ json/ i .test data .type
1188
1186
isPDF : / application\/ pdf| application\/ x-pdf/ i .test data .type
1189
- _storagePath : data ._storagePath or @storagePath
1190
1187
_downloadRoute : data ._downloadRoute or @downloadRoute
1191
1188
_collectionName : data ._collectionName or @collectionName
1192
- }
1189
+ ds ._storagePath = data ._storagePath or @ storagePath (_ .extend (data, ds))
1190
+ return ds
1193
1191
1194
1192
###
1195
1193
@locus Server
@@ -1229,7 +1227,7 @@ class FilesCollection
1229
1227
1230
1228
self = @
1231
1229
opts ?= {}
1232
- opts .path = " #{ @storagePath }#{ nodePath .sep }#{ FSName}#{ extensionWithDot} "
1230
+ opts .path = " #{ @ storagePath (opts) }#{ nodePath .sep }#{ FSName}#{ extensionWithDot} "
1233
1231
opts .type = @ _getMimeType opts
1234
1232
opts .meta ?= {}
1235
1233
opts .size ?= buffer .length
@@ -1305,7 +1303,7 @@ class FilesCollection
1305
1303
1306
1304
{extension , extensionWithDot } = @ _getExt fileName
1307
1305
opts .meta ?= {}
1308
- opts .path = " #{ @storagePath }#{ nodePath .sep }#{ FSName}#{ extensionWithDot} "
1306
+ opts .path = " #{ @ storagePath (opts) }#{ nodePath .sep }#{ FSName}#{ extensionWithDot} "
1309
1307
1310
1308
storeResult = (result , callback ) ->
1311
1309
result ._id = fileId
0 commit comments