public
Created

Part of my Meteor asset upload client code. This handle the drag and drop events and calls the createAsset method.

  • Download Gist
AssetsClient.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
Template.assetUpload.events(
'drop #asset-upload-dropzone': (e) ->
e.stop()
$(e.target).removeClass 'dropzone-hover'
new AssetUpload(e)
 
'dragenter #asset-upload-dropzone': (e) ->
e.stop()
$(e.target).addClass 'dropzone-hover'
 
'dragleave #asset-upload-dropzone': (e) ->
e.stop()
$(e.target).removeClass 'dropzone-hover'
 
'dragover #asset-upload-dropzone': (e) ->
e.stop();
)
 
class AssetUpload
constructor: (e) ->
@fileList = e.dataTransfer.files
i = @fileList.length
while i--
@uploadFile @fileList[i]
 
uploadFile: (file) ->
reader = new FileReader()
reader.onload = (e) =>
@sendToServer file, reader
reader.readAsBinaryString file
 
sendToServer: (file, reader) ->
Meteor.call 'createAsset', reader.result, file.name, file.size, file.type
AssetsServer.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
Meteor.methods(
createAsset: (blob, title, size, type) ->
console.log 'creating asset'
console.log 'starting the creation'
if !(blob || title || size || type)
throw (new Meteor.Error 400, 'Required parameter missing')
if typeof title != 'string'
throw (new Meteor.Error 413, 'Title is not a string')
if typeof size != 'number'
throw (new Meteor.Error 413, 'Size is not a number')
if typeof type != 'string'
throw (new Meteor.Error 413, 'Type is not a string')
if !this.userId
throw (new Meteor.Error 403, 'You have to be logged in to create anything. Sorry Charlie.')
 
fs = __meteor_bootstrap__.require 'fs'
root = 'public/'
folder = 'assets~/'
path = root + folder + title
encoding = 'binary'
fs.writeFile path, blob, encoding, (error) ->
if error
console.log 'error', error
 
console.log 'writing to mongo'
Assets.insert(
date: new Date()
title: _.escape title
size: size
type: _.escape type
uri: folder + title
authorId: this.userId
)
 
removeAsset: (id) ->
folder = 'public/'
asset = Assets.findOne(id)
if !asset
console.log 'no asset available'
fs = __meteor_bootstrap__.require 'fs'
fs.unlink folder + asset.uri, (error) ->
if error
console.warn 'error: ', error
Assets.remove asset._id
)

so is this mean that i can grab these two file one into client and the other into server and make a template(name='assetUpload') then i can use upload file.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.