Skip to content

Instantly share code, notes, and snippets.

@jfensign
Created February 7, 2014 05:15
Show Gist options
  • Select an option

  • Save jfensign/8857639 to your computer and use it in GitHub Desktop.

Select an option

Save jfensign/8857639 to your computer and use it in GitHub Desktop.

Revisions

  1. Joseph Ensign created this gist Feb 7, 2014.
    53 changes: 53 additions & 0 deletions simple_thumbnail
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    Thumbnailer = ->

    Thumbnailer = this

    fs = require "fs"

    cp = require "child_process"

    path = require "path"

    exec = cp.exec

    enctype = "binary"

    ###
    doc_to_image opts, cb

    opts =
    file_path: "/location/of/file/to/convert" #required
    write_path
    ###

    @doc_to_image: (opts, cb) ->

    filename = opts.file_path.replace path.extname opts.file_path, ".#{opts.convert_to}"

    is_pdf = path.extname opts.file_path is "pdf"

    commands =
    un: "unoconv --stdout #{opts.file_path} | "
    gs: "gs -q -dNOPAUSE -dBATCH \
    -dPDFFitPage -sDEVICE=#{opts.convert_to} \
    -r144 -sOutputFile=%stdout \
    #{if is_pdf then opts.file_path else '-_'}"

    writable = fs.createWriteStream path.resolve(opts.write_to, filename),
    flags: "w"
    encoding: enctype
    mode: 0o0777

    child_process = exec "#{commands.un}#{commands.gs}",
    encoding: enctype
    maxBuffer: 5000 * 1024

    writable.on "close", -> cb null, filename, writable

    child_process.stdout.on "data", (chunk) -> writable.write chunk, enctype

    child_process.stdout.on "close", -> writable.close()

    child_process.stderr.on "data", (d) -> console.log d

    module.exports = new Thumbnailer()