From 4bcc16645c11ef67b5c86e181daf9655b2d4d32f Mon Sep 17 00:00:00 2001 From: "dr.dimitru" Date: Thu, 16 Apr 2015 18:22:38 +0300 Subject: [PATCH 1/2] typo fix --- README.md | 9 +++++---- files.coffee | 8 ++++---- package.js | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7193a854..cf6246d0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Meteor-Files ======== -This package allow to: +This package allows to: - Upload file(s) via DDP * Small files * Huge files, tested on 100GB (Note Browser will eat 7%-10% RAM of the file size) @@ -59,7 +59,7 @@ myFiles = new Meteor.Files '/assets/app/uploads/myFiles', 'myFiles', '/downloads myFiles.insert(file) # Upload file myFiles.find({'meta.userId': Meteor.userId()}).cursor # Current collection cursor -myFiles.find({'meta.userId': Meteor.userId()}).get() # Array or fetched rows +myFiles.find({'meta.userId': Meteor.userId()}).get() # Array of fetched rows myFiles.find({'meta.userId': Meteor.userId()}).remove() # Remove all files on the cursor myFiles.remove({'meta.userId': Meteor.userId()}) # Remove all files returned by passed search @@ -118,14 +118,14 @@ size: Methods ========== -##### `insert(file, [meta], [onUploaded], [onProggress], [onBeforeUpload])` [*Client*] +##### `insert(file, [meta], [onUploaded], [onProgress], [onBeforeUpload])` [*Client*] Returns `FileReader` instance, so you can call `abort()` or any other method to control, `pause` or `resume` upload process, read more: [FileReader](https://developer.mozilla.org/en-US/docs/Web/API/FileReader). - `file` __File__ or __Object__ - HTML5 `files` item, like in change event: `e.currentTarget.files[0]` - `meta` __Object__ - Additional data as object, use later for search - `onUploaded` __Function__ - Callback triggered when upload is finished, with two arguments: * `error` * `fileRef` - see __Current schema__ section above - - `onProggress` __Function__ - Callback triggered when chunk is sent, with only argument: + - `onProgress` __Function__ - Callback triggered when chunk is sent, with only argument: * `progress` __Number__ - Current progress from `0` to `100` - `onBeforeUpload` __Function__ - Callback triggered right before upload is started, with only argument: * Context of the function is `File` - so you are able to check for extension, mime-type, size and etc. @@ -160,6 +160,7 @@ if Meteor is client prgrs.set false $(e.target).val('') + UIBlock.unblock() , (progress) -> prgrs.set progress diff --git a/files.coffee b/files.coffee index 6bf73969..d9332e8b 100644 --- a/files.coffee +++ b/files.coffee @@ -248,7 +248,7 @@ class Meteor.Files @property {File|Object} file - HTML5 `files` item, like in change event: `e.currentTarget.files[0]` @property {Object} meta - Additional data as object, use later for search @property {Function} onUploaded - Callback triggered when upload is finished, with two arguments `error` and `fileRef` - @property {Function} onProggress - Callback triggered when chunk is sent, with only argument `progress` + @property {Function} onProgress - Callback triggered when chunk is sent, with only argument `progress` @property {Function} onBeforeUpload - Callback triggered right before upload is started, with only `FileReader` argument: context is `File` - so you are able to check for extension, mime-type, size and etc. return true to continue @@ -257,12 +257,12 @@ class Meteor.Files @url https://developer.mozilla.org/en-US/docs/Web/API/FileReader @returns {FileReader} ### - insert: (file, meta, onUploaded, onProggress, onBeforeUpload) -> + insert: (file, meta, onUploaded, onProgress, onBeforeUpload) -> console.info "Meteor.Files Debugger: [insert()]" if @debug check file, Match.OneOf File, Object check meta, Match.Optional Object check onUploaded, Match.Optional Function - check onProggress, Match.Optional Function + check onProgress, Match.Optional Function check onBeforeUpload, Match.Optional Function window.onbeforeunload = (e) -> @@ -303,7 +303,7 @@ class Meteor.Files return false fileReader.onload = (chunk) -> - onProggress and onProggress((currentChunk / chunksQty) * 100) + onProgress and onProgress((currentChunk / chunksQty) * 100) if chunksQty is 1 Meteor.call self.methodNames.MeteorFileWrite, chunk.srcElement.result, fileData, meta, first, chunksQty, currentChunk, randFileName, (error, data) -> diff --git a/package.js b/package.js index db863a8b..4316c016 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: 'ostrio:files', - version: '1.0.2', + version: '1.0.3', 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' From eb1cf856f38ac04dfd666401c5330784f0453eb3 Mon Sep 17 00:00:00 2001 From: "dr.dimitru" Date: Fri, 17 Apr 2015 00:06:06 +0300 Subject: [PATCH 2/2] v1.0.3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove auto-publish/subscribe - Add file’s properties: * isVideo * isAudio * isImage - Add `fileURL` Template helper --- .versions | 2 +- README.md | 28 ++++++++++++++++++++++++++++ files.coffee | 44 +++++++++++++++++++++++++++++++++----------- package.js | 1 + 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/.versions b/.versions index dc9a6d49..4d2be6cc 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.0.2 +ostrio:files@1.0.3 ostrio:jsextensions@0.0.4 random@1.0.3 reactive-dict@1.1.0 diff --git a/README.md b/README.md index cf6246d0..da87a52c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ This package allows to: * You may use `Meteor-Files` as temporary storage * After file is uploaded and stored on FS you able to `mv` or `cp` it's content - Support of non-latin (non-Roman) file names + - Subscribe on files you need Why `Meteor-Files`? @@ -56,6 +57,13 @@ myFiles.cacheControl = 'public, max-age=31536000' # Set 'Cache-Control' header f myFiles = new Meteor.Files '/assets/app/uploads/myFiles', 'myFiles', '/downloads/myFiles' +if Meteor.isClient + myFiles.collection.subscribe "MeteorFileSubs", postId.get() + +if Meteor.isServer + Meteor.publish "MeteorFileSubs", (postId) -> + myFiles.collection.find {'meta.postId': postId} + myFiles.insert(file) # Upload file myFiles.find({'meta.userId': Meteor.userId()}).cursor # Current collection cursor @@ -112,10 +120,23 @@ meta: userId: type: String optional: true +isVideo: + type: Boolean +isAudio: + type: Boolean +isImage: + type: Boolean size: type: Number ``` +Template Helper +========== +To get download URL for file, you only need `fileRef` object, so there is no need for subscription +```jade +a(href="{{fileURL fileRef}}?download=true" download) {{fileRef.name}} +``` + Methods ========== ##### `insert(file, [meta], [onUploaded], [onProgress], [onBeforeUpload])` [*Client*] @@ -214,6 +235,13 @@ Mongo Collection Instance - Use to fetch data. __Do not `remove` or `update`__ t ```coffeescript uploads = new Meteor.Files() +if Meteor.isClient + Meteor.subscribe "MeteorFileSubs", postId.get() + +if Meteor.isServer + Meteor.publish "MeteorFileSubs", (postId) -> + uploads.collection.find {'meta.postId': postId} + uploads.collection.find({'meta.post': post._id}) uploads.collection.findOne('hdjJDSHW6784kJS') ``` \ No newline at end of file diff --git a/files.coffee b/files.coffee index d9332e8b..e26c934d 100644 --- a/files.coffee +++ b/files.coffee @@ -99,8 +99,22 @@ class Meteor.Files userId: type: String optional: true + isVideo: + type: Boolean + isAudio: + type: Boolean + isImage: + type: Boolean size: type: Number + _prefix: + type: String + _collectionName: + type: String + _storagePath: + type: String + _downloadRoute: + type: String @collection.attachSchema @schema @@ -125,19 +139,9 @@ class Meteor.Files MeteorFileFindOne: "MeteorFileFindOne#{@_prefix}" MeteorFileUnlink: "MeteorFileUnlink#{@_prefix}" - if Meteor.isClient - Meteor.subscribe "MeteorFileSubs#{@_prefix}" - if Meteor.isServer - Meteor.publish "MeteorFileSubs#{@_prefix}", () -> - self.collection.find {} - _methods = {} - _methods[self.methodNames.MeteorFileRead] = (inst) -> - console.info "Meteor.Files Debugger: [MeteorFileRead]" if @debug - self.read.call cp(_insts[inst._prefix], inst) - _methods[self.methodNames.MeteorFileUnlink] = (inst) -> console.info "Meteor.Files Debugger: [MeteorFileUnlink]" if @debug self.remove.call cp(_insts[inst._prefix], inst), inst.search @@ -168,6 +172,13 @@ class Meteor.Files type: fileData.type size: fileData.size chunk: currentChunk + isVideo: fileData.type.toLowerCase().indexOf("video") > -1 + isAudio: fileData.type.toLowerCase().indexOf("audio") > -1 + isImage: fileData.type.toLowerCase().indexOf("image") > -1 + _prefix: self._prefix + _collectionName: self.collectionName + _storagePath: self.storagePath + _downloadRoute: self.downloadRoute if first fs.outputFileSync path, file, 'binary' @@ -408,4 +419,15 @@ class Meteor.Files link: () -> console.info "Meteor.Files Debugger: [link()]" if @debug if @currentFile - return "#{@downloadRoute}/#{@currentFile._id}/#{@collectionName}" \ No newline at end of file + return "#{@downloadRoute}/#{@currentFile._id}/#{@collectionName}" + +if Meteor.isClient + ### + @description Get download URL for file by fileRef, even without subscription + @example {{fileURL fileRef}} + ### + Template.registerHelper 'fileURL', (fileRef) -> + if fileRef._id + return "#{fileRef._downloadRoute}/#{fileRef._id}/#{fileRef._collectionName}" + else + null \ No newline at end of file diff --git a/package.js b/package.js index 4316c016..d1747986 100644 --- a/package.js +++ b/package.js @@ -9,6 +9,7 @@ Package.describe({ Package.onUse(function(api) { api.versionsFrom('1.1'); api.addFiles('files.coffee'); + api.use('templating', 'client'); api.use(['underscore', 'sha', 'ostrio:jsextensions@0.0.4', 'coffeescript', 'iron:router@1.0.5', 'aldeed:collection2@2.3.3'], ['client', 'server']); });