Set up portrait uploading for thangs.

This commit is contained in:
Scott Erickson 2014-01-06 12:37:35 -08:00
parent a99073fc5e
commit f656a4f4cf
10 changed files with 62 additions and 9 deletions

View file

@ -76,6 +76,7 @@ module.exports = class ThangType extends CocoModel
addPortrait: ->
# The portrait is built very differently than the other animations, so it gets a separate function.
return unless @actions
portrait = @actions.portrait
return unless portrait
scale = portrait.scale or 1
@ -145,10 +146,15 @@ module.exports = class ThangType extends CocoModel
spriteSheetKey: (options) ->
"#{@get('name')} - #{options.resolutionFactor}"
getPortrait: (spriteOptionsOrKey, size=100) ->
getPortraitImage: (spriteOptionsOrKey, size=100) ->
src = @getPortraitSource(spriteOptionsOrKey, size)
$('<img />').attr('src', url)
getPortraitSource: (spriteOptionsOrKey, size=100) ->
key = spriteOptionsOrKey
key = if _.isObject(key) then @spriteSheetKey(key) else key
spriteSheet = @spriteSheets[key]
spriteSheet ?= @buildSpriteSheet({portraitOnly:true})
return unless spriteSheet
canvas = $("<canvas width='#{size}' height='#{size}'></canvas>")
stage = new createjs.Stage(canvas[0])
@ -159,5 +165,20 @@ module.exports = class ThangType extends CocoModel
sprite.gotoAndStop 'portrait'
stage.addChild(sprite)
stage.update()
url = stage.toDataURL()
img = $('<img />').attr('src', url)
stage.toDataURL()
uploadGenericPortrait: ->
src = @getPortraitSource()
return unless src
src = src.replace('data:image/png;base64,', '').replace(/\ /g, '+')
body =
filename: 'portrait.png'
mimetype: 'image/png'
path: "db/thang.type/#{@get('original')}"
b64png: src
force: 'true'
$.ajax('/file', { type: 'POST', data: body, success: @onFileUploaded })
onFileUploaded: =>
console.log 'Image uploaded'

View file

@ -0,0 +1,9 @@
#thang-type-home-view
.portrait
width: 30px
#portrait-col
width: 30px
td
vertical-align: middle

View file

@ -8,6 +8,7 @@ table.table
th Version
for article in documents
- article = article.attributes;
tr
td
a(href="/editor/article/#{article.slug || article._id}")

View file

@ -8,6 +8,7 @@ table.table
th Version
for level in documents
- level = level.attributes;
tr
td
a(href="/editor/level/#{level.slug || level._id}")

View file

@ -1,13 +1,18 @@
table.table
tr
th(colspan=3) Results: #{documents.length}
th(colspan=3) #{documents.length} results
tr
th#portrait-col
th Name
th Version
for thang in documents
- thang = thang.attributes;
- path = '/file/db/thang.type/'+thang.original+'/portrait.png'
tr
td
img(src=path).portrait
td
a(href="/editor/thang/#{thang.slug || thang._id}")
| #{thang.name}

View file

@ -218,7 +218,7 @@ module.exports = class ThangTypeEditView extends View
updatePortrait: ->
options = @getSpriteOptions()
portrait = @thangType.getPortrait(options)
portrait = @thangType.getPortraitImage(options)
return unless portrait
portrait?.attr('id', 'portrait').addClass('img-polaroid')
$('#portrait').replaceWith(portrait)

View file

@ -1,6 +1,7 @@
SearchView = require 'views/kinds/SearchView'
module.exports = class ThangTypeHomeView extends SearchView
id: 'thang-type-home-view'
modelLabel: 'Thang Type'
model: require 'models/ThangType'
modelURL: '/db/thang.type'

View file

@ -65,7 +65,7 @@ module.exports = class ThangTypeHomeView extends View
onSearchChange: =>
@hideLoading()
documents = (m.attributes for m in @collection.models)
documents = @collection.models
table = $(@tableTemplate(documents:documents))
@$el.find('table').replaceWith(table)

View file

@ -57,7 +57,8 @@
"node-force-domain": "~0.1.0",
"mailchimp-api": "",
"express-useragent": "~0.0.9",
"gridfs-stream": ""
"gridfs-stream": "",
"stream-buffers": ""
},
"devDependencies": {
"jade": "0.33.x",

View file

@ -54,6 +54,7 @@ postFileSchema =
# source
url: { type: 'string', description: 'The url to download the file from.' }
postName: { type: 'string', description: 'The input field this file was sent on.' }
b64png: { type: 'string', description: 'Raw png data to upload.' }
# options
force: { type: 'string', 'default': '', description: 'Whether to overwrite existing files (as opposed to throwing an error).' }
@ -72,15 +73,16 @@ filePost = (req, res) ->
options = req.body
tv4 = require('tv4').tv4
valid = tv4.validate(options, postFileSchema)
hasSource = options.url or options.postName
hasSource = options.url or options.postName or options.b64png
return returnBadInput(res) if (not valid) or (not hasSource)
return saveURL(req, res) if options.url
return saveFile(req, res) if options.postName
return savePNG(req, res) if options.b64png
saveURL = (req, res) ->
options = createPostOptions(req)
checkExistence options, res, req.body.force, (err) ->
return if err
return returnServerError(res) if err
writestream = Grid.gfs.createWriteStream(options)
request(req.body.url).pipe(writestream)
handleStreamEnd(res, writestream)
@ -95,6 +97,18 @@ saveFile = (req, res) ->
fileStream.pipe(writestream)
handleStreamEnd(res, writestream)
savePNG = (req, res) ->
options = createPostOptions(req)
checkExistence options, res, req.body.force, (err) ->
return returnServerError(res) if err
writestream = Grid.gfs.createWriteStream(options)
img = new Buffer(req.body.b64png, 'base64')
streamBuffers = require 'stream-buffers'
myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({frequency: 10,chunkSize: 2048})
myReadableStreamBuffer.put(img)
myReadableStreamBuffer.pipe(writestream)
handleStreamEnd(res, writestream)
checkExistence = (options, res, force, done) ->
q = {
filename: options.filename