Skip to content

@deltaepsilon /AssetsClient.coffee
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Part of my Meteor asset upload client code. This handle the drag and drop events and calls the createAsset method.
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
)
@crapthings

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.