diff --git a/meteor-file-uploader.js b/meteor-file-uploader.js deleted file mode 100644 index 00e7355..0000000 --- a/meteor-file-uploader.js +++ /dev/null @@ -1,75 +0,0 @@ -/************************ FileUploader ***************************************/ -FileUploader = function (options) { - this.options = options || {}; - this.method = options.method; - this.template = options.template; - this.files = new Meteor.Collection(null); -}; - -FileUploader.prototype = { - constructor: FileUploader, - - render: function (data) { - if (!this.template) return; - - var self = this; - var html; - - html = Spark.isolate(function () { - return self.template(_.extend({ - files: function () { - return self.files.find(); - } - }, data || {})); - }); - - html = Spark.attachEvents({ - 'change input[type=file]': function (e, tmpl) { - var fileInput = e.currentTarget; - var file; - var mFile; - - for (var i = 0; i < fileInput.files.length; i++) { - file = fileInput.files[i]; - - mFile = new MeteorFile(file, { - collection: self.files - }); - - self.files.insert(mFile.toJSONValue()); - - mFile.upload( - file, - self.method, - self.options, - function (err) { - if (err) throw err; - } - ); - } - } - }, html); - - return html; - } -}; -/*****************************************************************************/ - -/************************ Handlebars *****************************************/ -Handlebars.registerHelper('FileUploader', function (options) { - var uploadOptions = options.hash; - - var uploader = new FileUploader(_.extend({ - template: options.fn - }, uploadOptions, { - size: eval(uploadOptions.size) - })); - - return uploader.render(options.data || {}); -}); - -Handlebars.registerHelper('humanize', function (number, options) { - return MeteorFile.humanize(number); -}); -/*****************************************************************************/ - diff --git a/meteor-file.js b/meteor-file.js index fd79fb3..cf51db8 100644 --- a/meteor-file.js +++ b/meteor-file.js @@ -101,7 +101,35 @@ EJSON.addType("MeteorFile", MeteorFile.fromJSONValue); /************************ Client *********************************************/ if (Meteor.isClient) { + + /** + * Create a binary string out of an array of numbers (bytes), each varying + * from 0-255. + * + * @param {Array} bytes The array of numbers to transform into a binary str. + * @return {string} The byte array as a string. + */ + function arrayToBinaryString(bytes) { + if (typeof bytes != typeof []) { + return null; + } + var i = bytes.length; + var bstr = new Array(i); + while (i--) { + bstr[i] = String.fromCharCode(bytes[i]); + } + return bstr.join(''); + } + + function toDataURL(contentType, uint8Array) { + return 'data:' + contentType + ';base64,' + + self.btoa(arrayToBinaryString(uint8Array)); + } + _.extend(MeteorFile.prototype, { + getDataUrl: function() { + return toDataURL(this.type, this.data); + }, read: function (file, options, callback) { if (arguments.length == 2) callback = options; diff --git a/package.js b/package.js index 79d2a86..e02633c 100644 --- a/package.js +++ b/package.js @@ -4,9 +4,7 @@ Package.describe({ Package.on_use(function (api) { api.use(["underscore", "ejson"], ["client", "server"]); - api.use(["handlebars", "spark"], "client"); api.add_files(["meteor-file.js"], ["client", "server"]); - api.add_files("meteor-file-uploader.js", "client"); if (typeof api.export !== 'undefined') { api.export("MeteorFile", ["client", "server"]);