From 6a8a384c030f0be1674c5e1e8c2d03e5bb6bb786 Mon Sep 17 00:00:00 2001 From: "Dmitriy A. Golev" Date: Mon, 13 Jul 2015 05:17:51 +0300 Subject: [PATCH] v1.3.0 - Add `load()` method --- .npm/package/npm-shrinkwrap.json | 194 +++++++++++++++++++++++++++++++ .versions | 2 +- README.md | 24 ++++ files.coffee | 76 +++++++++++- package.js | 5 +- 5 files changed, 294 insertions(+), 7 deletions(-) diff --git a/.npm/package/npm-shrinkwrap.json b/.npm/package/npm-shrinkwrap.json index f6aa6fd1..6efb6229 100644 --- a/.npm/package/npm-shrinkwrap.json +++ b/.npm/package/npm-shrinkwrap.json @@ -55,6 +55,200 @@ } } } + }, + "request": { + "version": "2.58.0", + "dependencies": { + "bl": { + "version": "0.9.4", + "dependencies": { + "readable-stream": { + "version": "1.0.33", + "dependencies": { + "core-util-is": { + "version": "1.0.1" + }, + "isarray": { + "version": "0.0.1" + }, + "string_decoder": { + "version": "0.10.31" + }, + "inherits": { + "version": "2.0.1" + } + } + } + } + }, + "caseless": { + "version": "0.10.0" + }, + "extend": { + "version": "2.0.1" + }, + "forever-agent": { + "version": "0.6.1" + }, + "form-data": { + "version": "1.0.0-rc1", + "dependencies": { + "async": { + "version": "1.3.0" + }, + "mime-types": { + "version": "2.1.2", + "dependencies": { + "mime-db": { + "version": "1.14.0" + } + } + } + } + }, + "json-stringify-safe": { + "version": "5.0.1" + }, + "mime-types": { + "version": "2.0.14", + "dependencies": { + "mime-db": { + "version": "1.12.0" + } + } + }, + "node-uuid": { + "version": "1.4.3" + }, + "qs": { + "version": "3.1.0" + }, + "tunnel-agent": { + "version": "0.4.1" + }, + "tough-cookie": { + "version": "2.0.0" + }, + "http-signature": { + "version": "0.11.0", + "dependencies": { + "assert-plus": { + "version": "0.1.5" + }, + "asn1": { + "version": "0.1.11" + }, + "ctype": { + "version": "0.5.3" + } + } + }, + "oauth-sign": { + "version": "0.8.0" + }, + "hawk": { + "version": "2.3.1", + "dependencies": { + "hoek": { + "version": "2.14.0" + }, + "boom": { + "version": "2.8.0" + }, + "cryptiles": { + "version": "2.0.4" + }, + "sntp": { + "version": "1.0.9" + } + } + }, + "aws-sign2": { + "version": "0.5.0" + }, + "stringstream": { + "version": "0.0.4" + }, + "combined-stream": { + "version": "1.0.5", + "dependencies": { + "delayed-stream": { + "version": "1.0.0" + } + } + }, + "isstream": { + "version": "0.1.2" + }, + "har-validator": { + "version": "1.8.0", + "dependencies": { + "bluebird": { + "version": "2.9.33" + }, + "chalk": { + "version": "1.1.0", + "dependencies": { + "ansi-styles": { + "version": "2.1.0" + }, + "escape-string-regexp": { + "version": "1.0.3" + }, + "has-ansi": { + "version": "2.0.0", + "dependencies": { + "ansi-regex": { + "version": "2.0.0" + } + } + }, + "strip-ansi": { + "version": "3.0.0", + "dependencies": { + "ansi-regex": { + "version": "2.0.0" + } + } + }, + "supports-color": { + "version": "2.0.0" + } + } + }, + "commander": { + "version": "2.8.1", + "dependencies": { + "graceful-readlink": { + "version": "1.0.1" + } + } + }, + "is-my-json-valid": { + "version": "2.12.0", + "dependencies": { + "generate-function": { + "version": "2.0.0" + }, + "generate-object-property": { + "version": "1.2.0", + "dependencies": { + "is-property": { + "version": "1.0.2" + } + } + }, + "jsonpointer": { + "version": "1.1.0" + }, + "xtend": { + "version": "4.0.0" + } + } + } + } + } + } } } } diff --git a/.versions b/.versions index 87d329ee..4e516d23 100644 --- a/.versions +++ b/.versions @@ -32,7 +32,7 @@ minimongo@1.0.8 mongo@1.1.0 observe-sequence@1.0.6 ordered-dict@1.0.3 -ostrio:files@1.2.5 +ostrio:files@1.3.0 ostrio:jsextensions@0.0.4 random@1.0.3 reactive-dict@1.1.0 diff --git a/README.md b/README.md index 55c8ca0d..df7ffdbb 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ * [findOne()](#findonesearch--isomorphic) [*Isomorphic*] * [find()](#findsearch--isomorphic) [*Isomorphic*] * [write()](#writebuffer-options-callback--server) [*Server*] + * [load()](#loadurl-options-callback--server) [*Server*] Meteor-Files ======== @@ -457,4 +458,27 @@ uploads.write buffer (err, fileObj) -> # Download File window.open uploads.link(fileObj, 'original'), '_parent' +``` + +###### `load(url, options, callback)` [*Server*] + - `url` {*String*} - Binary data + - `options` {*Object*} - Object with next properties: + * `meta` - Additional data as object, use later for search + * `name` or `fileName` - File name + - `callback(error, fileObj)` + +Returns: + - `fileObj` {*Object*} + +```coffeescript +uploads = new Meteor.Files() +uploads.load 'http://domain.com/small.png' +, + name: 'small.png' + meta: + post: post._id +, + (err, fileObj) -> + # Download File + window.open uploads.link(fileObj, 'original'), '_parent' ``` \ No newline at end of file diff --git a/files.coffee b/files.coffee index 2f915fcd..940ddd85 100644 --- a/files.coffee +++ b/files.coffee @@ -3,6 +3,13 @@ if Meteor.isServer @description Require "fs-extra" npm package ### fs = Npm.require "fs-extra" + request = Npm.require "request" + + ### + @var {object} bound - Meteor.bindEnvironment aka Fiber wrapper + ### + bound = Meteor.bindEnvironment (callback) -> + return callback() ### @object @@ -265,8 +272,9 @@ class Meteor.Files @function @class Meteor.Files @name write - @param {String} path - Path to file - @param {String} path - Path to file + @param {Buffer} buffer - Binary File's Buffer + @param {Object} opts - {fileName: '', type: '', size: 0, meta: {...}} + @param {Function} callback - function(error, fileObj){...} @description Write buffer to FS and add to Meteor.Files Collection @returns {Files} - Return this ### @@ -290,8 +298,8 @@ class Meteor.Files versions: original: path: path - size: if opts.size then opts.size else buffer.length type: if opts.type then opts.type else 'application/*' + size: if opts.size then opts.size else buffer.length extension: extension isVideo: if opts.type then opts.type.toLowerCase().indexOf("video") > -1 else false isAudio: if opts.type then opts.type.toLowerCase().indexOf("audio") > -1 else false @@ -303,7 +311,6 @@ class Meteor.Files console.info "Meteor.Files Debugger: The file #{fileName} (binary) was added to #{@collectionName}" if @debug - callback and callback null, result if callback fs.outputFile path, buffer, 'binary', callback @@ -311,10 +318,71 @@ class Meteor.Files fs.outputFileSync path, buffer, 'binary' result._id = @collection.insert _.clone result + + callback and callback null, result return result else undefined + ### + @server + @function + @class Meteor.Files + @name load + @param {String} url - URL to file + @param {Object} opts - {fileName: '', meta: {...}} + @param {Function} callback - function(error, fileObj){...} + @description Download file, write stream to FS and add to Meteor.Files Collection + @returns {Files} - Return this + ### + load: if Meteor.isServer then (url, opts, callback) -> + console.info "Meteor.Files Debugger: [load(#{url}, #{opts}, callback)]" if @debug + check url, String + check opts, Match.Optional Object + check callback, Match.Optional Function + + self = @ + + randFileName = @namingFunction.call null, true + fileName = if opts.name or opts.fileName then opts.name or opts.fileName else randFileName + extension = fileName.split('.').pop() + path = "#{@storagePath}/#{randFileName}.#{extension}" + + request.get(url).on('error', (error)-> + throw new Meteor.Error 500, "Error on [load(#{url}, #{opts})]; Error:" + JSON.stringify error + ).on('response', (response) -> + bound -> + result = + name: fileName + extension: extension + path: path + meta: opts.meta + type: response.headers['content-type'] + size: response.headers['content-length'] + versions: + original: + path: path + type: response.headers['content-type'] + size: response.headers['content-length'] + extension: extension + isVideo: response.headers['content-type'].toLowerCase().indexOf("video") > -1 + isAudio: response.headers['content-type'].toLowerCase().indexOf("audio") > -1 + isImage: response.headers['content-type'].toLowerCase().indexOf("image") > -1 + _prefix: self._prefix + _collectionName: self.collectionName + _storagePath: self.storagePath + _downloadRoute: self.downloadRoute + + console.info "Meteor.Files Debugger: The file #{fileName} (binary) was loaded to #{@collectionName}" if @debug + + result._id = self.collection.insert _.clone result + callback and callback null, result + + ).pipe fs.createOutputStream path + + else + undefined + ### @server @function diff --git a/package.js b/package.js index c2a17411..28df7d2b 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: 'ostrio:files', - version: '1.2.5', + version: '1.3.0', summary: 'Upload, Store and Download small and huge files to/from file system (FS) via DDP and HTTP', git: 'https://github.com/VeliovGroup/Meteor-Files', documentation: 'README.md' @@ -14,5 +14,6 @@ Package.onUse(function(api) { }); Npm.depends({ - 'fs-extra': '0.18.1' + 'fs-extra': '0.18.1', + 'request': '2.58.0' }); \ No newline at end of file