Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to v2 of coffee #172

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### See included LICENSE file for details.
***************************************************************************/

var currentVersion = '1.3.8';
var currentVersion = '2.0.0';

Package.describe({
summary: 'Collections that efficiently store files using MongoDB GridFS, with built-in HTTP support',
Expand All @@ -27,7 +27,7 @@ Npm.depends({
});

Package.onUse(function(api) {
api.use('coffeescript@1.12.3_1', ['server','client']);
api.use('coffeescript@2.0.3_3', ['server','client']);
api.use('[email protected]', 'server');
api.use('[email protected]', ['server', 'client']);
api.use('[email protected]', 'server');
Expand All @@ -45,7 +45,7 @@ Package.onUse(function(api) {

Package.onTest(function (api) {
api.use('vsivsi:file-collection@' + currentVersion, ['server', 'client']);
api.use('coffeescript@1.12.3_1', ['server', 'client']);
api.use('coffeescript@2.0.3_3', ['server', 'client']);
api.use('[email protected]', ['server', 'client']);
api.use('[email protected]', ['server','client']);
api.use('[email protected]', ['server','client']);
Expand Down
20 changes: 11 additions & 9 deletions src/gridFS_client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ if Meteor.isClient

class FileCollection extends Mongo.Collection

constructor: (@root = share.defaultRoot, options = {}) ->
constructor: (root = share.defaultRoot, options = {}) ->
unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

if typeof root is 'object'
options = root
root = share.defaultRoot

super root + '.files', { idGeneration: 'MONGO' }

unless @ instanceof FileCollection
return new FileCollection(root, options)

unless @ instanceof Mongo.Collection
throw new Meteor.Error 'The global definition of Mongo.Collection has changed since the file-collection package was loaded. Please ensure that any packages that redefine Mongo.Collection are loaded before file-collection.'

unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

if typeof @root is 'object'
options = @root
@root = share.defaultRoot

@root = root
@base = @root
@baseURL = options.baseURL ? "/gridfs/#{@root}"
@chunkSize = options.chunkSize ? share.defaultChunkSize
super @root + '.files', { idGeneration: 'MONGO' }

# This call sets up the optional support for resumable.js
# See the resumable.coffee file for more information
Expand Down
35 changes: 21 additions & 14 deletions src/gridFS_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ if Meteor.isServer

class FileCollection extends Mongo.Collection

constructor: (@root = share.defaultRoot, options = {}) ->
constructor: (root = share.defaultRoot, options = {}) ->

# For CoffeeScript v2 this (aka @) cannot be referenced before a call to super
unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

if typeof root is 'object'
options = root
root = share.defaultRoot

# Call super's constructor
super root + '.files', { idGeneration: 'MONGO' }
@root = root

unless @ instanceof FileCollection
return new FileCollection(@root, options)

unless @ instanceof Mongo.Collection
throw new Meteor.Error 'The global definition of Mongo.Collection has changed since the file-collection package was loaded. Please ensure that any packages that redefine Mongo.Collection are loaded before file-collection.'

unless Mongo.Collection is Mongo.Collection.prototype.constructor
throw new Meteor.Error 'The global definition of Mongo.Collection has been patched by another package, and the prototype constructor has been left in an inconsistent state. Please see this link for a workaround: https://github.com/vsivsi/meteor-file-sample-app/issues/2#issuecomment-120780592'

if typeof @root is 'object'
options = @root
@root = share.defaultRoot

@chunkSize = options.chunkSize ? share.defaultChunkSize

Expand Down Expand Up @@ -57,9 +65,6 @@ if Meteor.isServer
@allows = { read: [], insert: [], write: [], remove: [] }
@denys = { read: [], insert: [], write: [], remove: [] }

# Call super's constructor
super @root + '.files', { idGeneration: 'MONGO' }

# Default indexes
if options.resumable
indexOptions = {}
Expand All @@ -83,7 +88,9 @@ if Meteor.isServer
# share.defaultResponseHeaders[h] = v

# Setup specific allow/deny rules for gridFS, and tie-in the application settings
# FileCollection.__super__ needs to be set up for CoffeeScript v2

FileCollection.__super__ = Mongo.Collection.prototype;
FileCollection.__super__.allow.bind(@)
# Because allow rules are not guaranteed to run,
# all checking is done in the deny rules below
Expand Down Expand Up @@ -246,7 +253,7 @@ if Meteor.isServer

writeStream = Meteor.wrapAsync(@gfs.createWriteStream.bind(@gfs))
root: @root
_id: mongodb.ObjectID("#{file._id}")
_id: mongodb.ObjectID("#{file._id._str}")
mode: 'w'
timeOut: @lockOptions.timeOut
lockExpiration: @lockOptions.lockExpiration
Expand All @@ -258,7 +265,7 @@ if Meteor.isServer
writeStream.on 'expires-soon', () =>
writeStream.renewLock (e, d) ->
if e or not d
console.warn "Automatic Write Lock Renewal Failed: #{file._id}", e
console.warn "Automatic Write Lock Renewal Failed: #{file._id._str}", e

if callback?
writeStream.on 'close', (retFile) ->
Expand Down Expand Up @@ -293,7 +300,7 @@ if Meteor.isServer

readStream = Meteor.wrapAsync(@gfs.createReadStream.bind(@gfs))
root: @root
_id: mongodb.ObjectID("#{file._id}")
_id: mongodb.ObjectID("#{file._id._str}")
timeOut: @lockOptions.timeOut
lockExpiration: @lockOptions.lockExpiration
pollingInterval: @lockOptions.pollingInterval
Expand All @@ -306,7 +313,7 @@ if Meteor.isServer
readStream.on 'expires-soon', () =>
readStream.renewLock (e, d) ->
if e or not d
console.warn "Automatic Read Lock Renewal Failed: #{file._id}", e
console.warn "Automatic Read Lock Renewal Failed: #{file._id._str}", e

if callback?
readStream.on 'close', () ->
Expand All @@ -323,7 +330,7 @@ if Meteor.isServer
ret = 0
@find(selector).forEach (file) =>
res = Meteor.wrapAsync(@gfs.remove.bind(@gfs))
_id: mongodb.ObjectID("#{file._id}")
_id: mongodb.ObjectID("#{file._id._str}")
root: @root
timeOut: @lockOptions.timeOut
lockExpiration: @lockOptions.lockExpiration
Expand Down
8 changes: 4 additions & 4 deletions test/file_collection_tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ createContent = (_id, data, name, chunkNum, chunkSize = 16) ->
Content-Disposition: form-data; name="resumableIdentifier"\r
Content-Type: text/plain\r
\r
#{_id}\r
#{_id._str}\r
--AaB03x\r
Content-Disposition: form-data; name="resumableFilename"\r
Content-Type: text/plain\r
Expand Down Expand Up @@ -529,7 +529,7 @@ createCheckQuery = (_id, data, name, chunkNum, chunkSize = 16) ->
throw new Error "Bad chunkNum" if chunkNum > totalChunks
begin = (chunkNum - 1) * chunkSize
end = if chunkNum is totalChunks then data.length else chunkNum * chunkSize
"?resumableChunkNumber=#{chunkNum}&resumableChunkSize=#{chunkSize}&resumableCurrentChunkSize=#{end-begin}&resumableTotalSize=#{data.length}&resumableType=text/plain&resumableIdentifier=#{_id}&resumableFilename=#{name}&resumableRelativePath=#{name}&resumableTotalChunks=#{totalChunks}"
"?resumableChunkNumber=#{chunkNum}&resumableChunkSize=#{chunkSize}&resumableCurrentChunkSize=#{end-begin}&resumableTotalSize=#{data.length}&resumableType=text/plain&resumableIdentifier=#{_id._str}&resumableFilename=#{name}&resumableRelativePath=#{name}&resumableTotalChunks=#{totalChunks}"

Tinytest.addAsync 'Basic resumable.js REST interface POST/GET/DELETE', (test, onComplete) ->
testColl.insert { filename: 'writeresumablefile', contentType: 'text/plain' }, (err, _id) ->
Expand Down Expand Up @@ -858,7 +858,7 @@ if Meteor.isClient
testColl.resumable.on 'fileAdded', (file) ->
testColl.insert { _id: file.uniqueIdentifier, filename: file.fileName, contentType: file.file.type }, (err, _id) ->
test.fail(err) if err
thisId = "#{_id}"
thisId = "#{_id._str}"
testColl.resumable.upload()

testColl.resumable.on 'fileSuccess', (file) ->
Expand All @@ -885,7 +885,7 @@ if Meteor.isClient
testColl.resumable.on 'fileAdded', (file) ->
testColl.insert { _id: file.uniqueIdentifier, filename: file.fileName, contentType: file.file.type }, (err, _id) ->
test.fail(err) if err
thisId = "#{_id}"
thisId = "#{_id._str}"
testColl.resumable.upload()

testColl.resumable.on 'fileSuccess', (file) ->
Expand Down