Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregorynicholas/5ad8e546753af45583c5 to your computer and use it in GitHub Desktop.
Save gregorynicholas/5ad8e546753af45583c5 to your computer and use it in GitHub Desktop.
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
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
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment