Skip to content

Commit b6ce93a

Browse files
committed
Dynamic storagePath
1 parent 00dc971 commit b6ce93a

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

demo/server/image-processing.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _app.createThumbnails = (collection, fileRef, cb) ->
3636
}, _app.NOOP
3737

3838
_.each sizes, (size, name) ->
39-
path = "#{collection.storagePath}/#{name}-#{fileRef._id}.#{fileRef.extension}"
39+
path = "#{collection.storagePath(fileRef)}/#{name}-#{fileRef._id}.#{fileRef.extension}"
4040

4141
copyPaste = ->
4242
fs.copy fileRef.path, path, (error) -> bound ->

docs/constructor.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
Storage path on file system
5151
</td>
5252
<td>
53-
<code>assets/app/uploads</code>
53+
<code>function { return 'assets/app/uploads'; }</code>
54+
<br />
55+
Always converted into the function since <code>v1.7.4</code>
5456
</td>
5557
<td>
5658
Relative to running script<br />

files.coffee

+25-27
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ if Meteor.isServer
2727
###
2828
class writeStream
2929
constructor: (@path, @maxLength, @file) ->
30+
if not @path or not _.isString @path
31+
return
3032
self = @
33+
fs.ensureFileSync @path
3134
@stream = fs.createWriteStream @path, {flags: 'a', mode: self.permissions, highWaterMark: 0}
3235
@drained = true
3336
@aborted = false
@@ -493,7 +496,17 @@ class FilesCollection
493496
@ddp ?= Meteor
494497
@onInitiateUpload ?= false
495498
@interceptDownload ?= false
499+
storagePath ?= -> "assets#{nodePath.sep}app#{nodePath.sep}uploads#{nodePath.sep}#{@collectionName}"
496500

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
497510

498511
if Meteor.isClient
499512
@onbeforeunloadMessage ?= 'Upload in a progress... Do you want to abort?'
@@ -577,36 +590,20 @@ class FilesCollection
577590
headers['Accept-Ranges'] = 'bytes'
578591
return headers
579592

580-
if @public and not storagePath
593+
if @public and (not storagePath or not _.isString(storagePath))
581594
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."
582595

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-
599596
console.info('[FilesCollection.storagePath] Set to:', @storagePath) if @debug
600597

601-
fs.mkdirs @storagePath, {mode: @parentDirPermissions}, (error) ->
598+
fs.mkdirs @storagePath({}), {mode: @parentDirPermissions}, (error) ->
602599
if error
603600
throw new Meteor.Error 401, "[FilesCollection.#{self.collectionName}] Path \"#{self.storagePath}\" is not writable!", error
604601
return
605602

606603
check @strict, Boolean
607604
check @throttle, Match.OneOf false, Number
608605
check @permissions, Number
609-
check @storagePath, String
606+
check @storagePath, Function
610607
check @cacheControl, String
611608
check @onAfterRemove, Match.OneOf false, Function
612609
check @onAfterUpload, Match.OneOf false, Function
@@ -961,7 +958,7 @@ class FilesCollection
961958
if _continueUpload
962959
self._preCollection.remove {_id}
963960
self.remove {_id}
964-
self.unlink {_id, path: _continueUpload.file.path}
961+
self.unlink {_id, path: _continueUpload.file.path} if _continueUpload?.file?.path
965962
return true
966963

967964
Meteor.methods _methods
@@ -979,21 +976,22 @@ class FilesCollection
979976
opts.chunkId ?= -1
980977
opts.FSName ?= opts.fileId
981978
opts.file.meta ?= {}
979+
console.warn ">>>>> opts.file.meta", opts.file.meta
982980

983981
console.info "[FilesCollection] [Upload] [#{transport}] Got ##{opts.chunkId}/#{opts.fileLength} chunks, dst: #{opts.file.name or opts.file.fileName}" if @debug
984982

985983
fileName = @_getFileName opts.file
986984
{extension, extensionWithDot} = @_getExt fileName
987985

988986
result = opts.file
989-
result.path = "#{@storagePath}#{nodePath.sep}#{opts.FSName}#{extensionWithDot}"
990987
result.name = fileName
991988
result.meta = opts.file.meta
992989
result.extension = extension
993990
result.ext = extension
994-
result = @_dataToSchema result
995991
result._id = opts.fileId
996992
result.userId = userId or null
993+
result.path = "#{@storagePath(result)}#{nodePath.sep}#{opts.FSName}#{extensionWithDot}"
994+
result = _.extend result, @_dataToSchema result
997995

998996
if @onBeforeUpload and _.isFunction @onBeforeUpload
999997
ctx = _.extend {
@@ -1167,7 +1165,7 @@ class FilesCollection
11671165
@returns {Object}
11681166
###
11691167
_dataToSchema: (data) ->
1170-
return {
1168+
ds =
11711169
name: data.name
11721170
extension: data.extension
11731171
path: data.path
@@ -1186,10 +1184,10 @@ class FilesCollection
11861184
isText: /^text\//i.test data.type
11871185
isJSON: /application\/json/i.test data.type
11881186
isPDF: /application\/pdf|application\/x-pdf/i.test data.type
1189-
_storagePath: data._storagePath or @storagePath
11901187
_downloadRoute: data._downloadRoute or @downloadRoute
11911188
_collectionName: data._collectionName or @collectionName
1192-
}
1189+
ds._storagePath = data._storagePath or @storagePath(_.extend(data, ds))
1190+
return ds
11931191

11941192
###
11951193
@locus Server
@@ -1229,7 +1227,7 @@ class FilesCollection
12291227

12301228
self = @
12311229
opts ?= {}
1232-
opts.path = "#{@storagePath}#{nodePath.sep}#{FSName}#{extensionWithDot}"
1230+
opts.path = "#{@storagePath(opts)}#{nodePath.sep}#{FSName}#{extensionWithDot}"
12331231
opts.type = @_getMimeType opts
12341232
opts.meta ?= {}
12351233
opts.size ?= buffer.length
@@ -1305,7 +1303,7 @@ class FilesCollection
13051303

13061304
{extension, extensionWithDot} = @_getExt fileName
13071305
opts.meta ?= {}
1308-
opts.path = "#{@storagePath}#{nodePath.sep}#{FSName}#{extensionWithDot}"
1306+
opts.path = "#{@storagePath(opts)}#{nodePath.sep}#{FSName}#{extensionWithDot}"
13091307

13101308
storeResult = (result, callback) ->
13111309
result._id = fileId

0 commit comments

Comments
 (0)