Skip to content

Commit

Permalink
Merge pull request #1 from VeliovGroup/dev
Browse files Browse the repository at this point in the history
- Remove auto-publish/subscribe
- Add file’s properties:
   * isVideo
   * isAudio
   * isImage
- Add `fileURL` Template helper
  • Loading branch information
dr-dimitru committed Apr 16, 2015
2 parents e4049de + eb1cf85 commit 60614d7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ [email protected]
[email protected]
[email protected]
[email protected]
ostrio:[email protected].2
ostrio:[email protected].3
ostrio:[email protected]
[email protected]
[email protected]
Expand Down
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -16,6 +16,7 @@ This package allow 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`?
Expand Down Expand Up @@ -56,10 +57,17 @@ 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
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
Expand Down Expand Up @@ -112,20 +120,33 @@ 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], [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.
Expand Down Expand Up @@ -160,6 +181,7 @@ if Meteor is client

prgrs.set false
$(e.target).val('')
UIBlock.unblock()
,
(progress) ->
prgrs.set progress
Expand Down Expand Up @@ -213,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')
```
52 changes: 37 additions & 15 deletions files.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -248,7 +259,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
Expand All @@ -257,12 +268,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) ->
Expand Down Expand Up @@ -303,7 +314,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) ->
Expand Down Expand Up @@ -408,4 +419,15 @@ class Meteor.Files
link: () ->
console.info "Meteor.Files Debugger: [link()]" if @debug
if @currentFile
return "#{@downloadRoute}/#{@currentFile._id}/#{@collectionName}"
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
3 changes: 2 additions & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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:[email protected]', 'coffeescript', 'iron:[email protected]', 'aldeed:[email protected]'], ['client', 'server']);
});

Expand Down

0 comments on commit 60614d7

Please sign in to comment.