|
| 1 | +############################################################################ |
| 2 | +# Copyright (C) 2014 by Vaughn Iverson |
| 3 | +# fileCollection is free software released under the MIT/X11 license. |
| 4 | +# See included LICENSE file for details. |
| 5 | +############################################################################ |
| 6 | + |
| 7 | +# Both client and server |
| 8 | + |
| 9 | +# Default collection name is 'fs' |
| 10 | +myData = FileCollection({ |
| 11 | + resumable: true, # Enable the resumable.js compatible chunked file upload interface |
| 12 | + http: [ { method: 'get', path: '/:md5', lookup: (params, query) -> return { md5: params.md5 }}]} |
| 13 | + # Define a GET API that uses the md5 sum id files |
| 14 | +) |
| 15 | + |
| 16 | +############################################################ |
| 17 | +# Client-only code |
| 18 | +############################################################ |
| 19 | + |
| 20 | +if Meteor.isClient |
| 21 | + |
| 22 | + Meteor.subscribe 'allData' |
| 23 | + |
| 24 | + Meteor.startup () -> |
| 25 | + |
| 26 | + # Set up an autorun to keep the X-Auth-Token cookie up-to-date |
| 27 | + Deps.autorun () -> |
| 28 | + Meteor.userId() |
| 29 | + token = Accounts._storedLoginToken() |
| 30 | + $.cookie 'X-Auth-Token', token |
| 31 | + |
| 32 | + ################################ |
| 33 | + # Setup resumable.js in the UI |
| 34 | + |
| 35 | + # This assigns a file drop zone to the "file table" |
| 36 | + myData.resumable.assignDrop $(".fileDrop") |
| 37 | + |
| 38 | + # When a file is added |
| 39 | + myData.resumable.on 'fileAdded', (file) -> |
| 40 | + # Keep track of its progress reactivaly in a session variable |
| 41 | + Session.set file.uniqueIdentifier, 0 |
| 42 | + # Create a new file in the file collection to upload to |
| 43 | + myData.insert({ |
| 44 | + _id: file.uniqueIdentifier # This is the ID resumable will use |
| 45 | + filename: file.fileName |
| 46 | + contentType: file.file.type |
| 47 | + }, |
| 48 | + (err, _id) -> |
| 49 | + if err |
| 50 | + console.warn "File creation failed!", err |
| 51 | + return |
| 52 | + # Once the file exists on the server, start uploading |
| 53 | + myData.resumable.upload() |
| 54 | + ) |
| 55 | + |
| 56 | + # Update the upload progress session variable |
| 57 | + myData.resumable.on 'fileProgress', (file) -> |
| 58 | + Session.set file.uniqueIdentifier, Math.floor(100*file.progress()) |
| 59 | + |
| 60 | + # Finish the upload progress in the session variable |
| 61 | + myData.resumable.on 'fileSuccess', (file) -> |
| 62 | + Session.set file.uniqueIdentifier, undefined |
| 63 | + |
| 64 | + # More robust error handling needed! |
| 65 | + myData.resumable.on 'fileError', (file) -> |
| 66 | + console.warn "Error uploading", file.uniqueIdentifier |
| 67 | + Session.set file.uniqueIdentifier, undefined |
| 68 | + |
| 69 | + ##################### |
| 70 | + # UI template helpers |
| 71 | + |
| 72 | + Template.collTest.events |
| 73 | + # Wire up the event to remove a file by clicking the `X` |
| 74 | + 'click .del-file': (e, t) -> |
| 75 | + # Just the remove method does it all |
| 76 | + myData.remove {_id: this._id} |
| 77 | + |
| 78 | + Template.collTest.dataEntries = () -> |
| 79 | + # Reactively populate the table |
| 80 | + myData.find({}) |
| 81 | + |
| 82 | + Template.collTest.owner = () -> |
| 83 | + this.metadata?._auth?.owner |
| 84 | + |
| 85 | + Template.collTest.id = () -> |
| 86 | + "#{this._id}" |
| 87 | + |
| 88 | + Template.collTest.link = () -> |
| 89 | + myData.baseURL + "/" + this.md5 |
| 90 | + |
| 91 | + Template.collTest.uploadStatus = () -> |
| 92 | + percent = Session.get "#{this._id}" |
| 93 | + unless percent? |
| 94 | + "Processing..." |
| 95 | + else |
| 96 | + "Uploading..." |
| 97 | + |
| 98 | + Template.collTest.formattedLength = () -> |
| 99 | + numeral(this.length).format('0.0b') |
| 100 | + |
| 101 | + Template.collTest.uploadProgress = () -> |
| 102 | + percent = Session.get "#{this._id}" |
| 103 | + |
| 104 | + Template.collTest.isImage = () -> |
| 105 | + types = |
| 106 | + 'image/jpeg': true |
| 107 | + 'image/png': true |
| 108 | + 'image/gif': true |
| 109 | + 'image/tiff': true |
| 110 | + types[this.contentType]? |
| 111 | + |
| 112 | + Template.collTest.loginToken = () -> |
| 113 | + Meteor.userId() |
| 114 | + Accounts._storedLoginToken() |
| 115 | + |
| 116 | + Template.collTest.userId = () -> |
| 117 | + Meteor.userId() |
| 118 | + |
| 119 | +############################################################ |
| 120 | +# Server-only code |
| 121 | +############################################################ |
| 122 | + |
| 123 | +if Meteor.isServer |
| 124 | + |
| 125 | + Meteor.startup () -> |
| 126 | + |
| 127 | + # Only publish files owned by this userId, and ignore temp file chunks used by resumable |
| 128 | + Meteor.publish 'allData', () -> |
| 129 | + myData.find({ 'metadata._Resumable': { $exists: false }, 'metadata._auth.owner': this.userId }) |
| 130 | + |
| 131 | + # Don't allow users to modify the user docs |
| 132 | + Meteor.users.deny({update: () -> true }) |
| 133 | + |
| 134 | + # Allow rules for security. Without these, no writes would be allowed by default |
| 135 | + myData.allow |
| 136 | + insert: (userId, file) -> |
| 137 | + # Assign the proper owner when a file is created |
| 138 | + file.metadata = file.metadata ? {} |
| 139 | + file.metadata._auth = |
| 140 | + owner: userId |
| 141 | + true |
| 142 | + remove: (userId, file) -> |
| 143 | + # Only owners can delete |
| 144 | + if file.metadata?._auth?.owner and userId isnt file.metadata._auth.owner |
| 145 | + return false |
| 146 | + true |
| 147 | + read: (userId, file) -> |
| 148 | + # Only owners can GET file data |
| 149 | + if file.metadata?._auth?.owner and userId isnt file.metadata._auth.owner |
| 150 | + return false |
| 151 | + true |
| 152 | + write: (userId, file, fields) -> # This is for the HTTP REST interfaces PUT/POST |
| 153 | + # All client file metadata updates are denied, implement Methods for that... |
| 154 | + # Only owners can upload a file |
| 155 | + if file.metadata?._auth?.owner and userId isnt file.metadata._auth.owner |
| 156 | + return false |
| 157 | + true |
0 commit comments