Skip to content

Instantly share code, notes, and snippets.

@algesten
Last active August 29, 2015 14:15
Show Gist options
  • Save algesten/06261ef230b0baa5712c to your computer and use it in GitHub Desktop.
Save algesten/06261ef230b0baa5712c to your computer and use it in GitHub Desktop.
module.exports = class Bundlr
FORMAT_TTNITF: 'ttnitf'
FORMAT_PICTURE: 'bild' # deliberate to match OFP
FORMAT_TTNINJS: 'ttninjs'
FORMAT_INDESIGN: 'indesign'
FORMAT_CUTPASTE: 'cutpaste' # txt-format in media-moj
FORMAT_TTNITFXML: 'ttnitfxml'
# ofp spec is "ttninf", "ttnintf+bild", "ttinjs" etc
bundleForOfpSpec: (ofpSpec) -> bundleFor ofpSpec.split /\+/g
# array of [FORMAT_XYZ], returns a Bundle instance
bundleFor: (arr) ->
# factory method to instantiate a bundlr from search result.
# the seach result is either a single or an array of ttninjs
# entries as output by search-service 'item' request.
Bundlr.fromSearchResult = (result) ->
Bundlr.Bundle = class Bundle
# the bundle filename.
# 150219-blocketbluff-64381
filename: ->
# Returns an array of Entry. Caller must loop
# over entries and decide how to deal with them.
entries: ->
Bundlr.Entry = class Entry
format: # one of Bundlr.FORMAT_XYZ
uri: # uri to entry on the form:
# http://tt.se/media/text/150219-blocketbluff-64381/a000_NormalHires # association
# http://tt.se/media/text/150219-blocketbluff-64381/body_ttnitf # body_ttnitf
# http://tt.se/media/text/150219-blocketbluff-64381 # ttninjs
# each entry have either href or content. for pictures we have a href pointing to the
# actual file. for ttnitf, xml, ttninjs etc the actual text content is in content.
href: # where the actual file can be found (if not content)
# http://spix-bildbank01.driften.net:80/images/dist-fs/ttninjs/hires/705/170/0CC4DC684027411BA2C50E72CC39BE66.jpg
content: # the textual content of this entry (if not href)
filename: # the proposed filename for this entry:
# blocketbluff_a000_NormalHires.jpg picture
# 150219-blocketbluff-64381.ttt ttnitf
# 150219-blocketbluff-64381-indesign.txt indesign
# 150219-blocketbluff-64381.txt cutpaste
# ...
# returns a promise for the byte size of the contents. for 'href'
# entries this either looks in the ttninjs for size or
# falls back on a HEAD request to the originating server.
# for 'content' style entries. it is the string contents
# size if turned into a Buffer.
size: ->
# returns a promise for the mimetype. same rules as for
# size() in that it may fall back on a HEAD request to
# the originating server.
mimetype: ->
# return a promise for a readstream with the contents. can be used
# as an alternative way of getting the contents. works for both
# href and content.
readstream: ->
# ######################## EXAMPLE ################
Bundlr = require 'bundlr'
# sigh. global.
bundle = zip = null
@amqpc.rcp('search-service', 'item', msg).then (item) ->
bundlr = Bundlr.fromSearchSearch item
# create a specific bundle
bundle = bundlr.forOfpSpec 'ttnitf+bild'
# loop over entries, pick out stuff
bundle.entries().map (ent) -> Q.all([ent.filename(), ent.readstream()])
.all (todo) ->
zip = new Zip()
todo.forEach ([filename, readstream]) ->
f = zip.addFile filename
readstream.pipe f
.then >
# shipping to the browser...
response.set 'Content-Length', zip.size()
response.set 'Content-Type', 'application/zip'
response.set 'Content-Disposition', "attachment; filename=\"#{bundle.filename()}\";"
zip.pipe response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment