Template versioning and "includes"

Archived from the Transloadit support forum. Originally posted on June 4, 2014; details may be outdated, see our docs for current behavior. Original thread

Hi Guys,
I have quite a lot of templates, and management of these templates and version control is becoming a hassle. I am currently keeping them in my github repo and sync them using transloadit API.

Couple of things would be of use to someone like me:
1. Ability to reference or “include” common template steps. I would be able to create some kind of code reuse.
2. Template version control (rollback to previous version). I would advise to integrate with github here and allow us to store our templates in github. Although, there are other alternatives here obviously.

I created a nodejs coffeescript to sync up my templates stored in local files to transloadit. It is not the most brilliant piece of engineering, but I will share it here for anyone to use.

Run via coffee script

coffee myscript.coffee

Code:

_ = require 'lodash'
request = require 'request'
url = require("url")
crypto = require("crypto")
moment = require('moment')
fs = require('fs')
path = require('path')


authKey = "mykey"
authSecret = "mysecret"


getTemplates = (next = ->)->
  expires = moment().add('hours', 3).utc().format("YYYY/MM/DD HH:mm:ssZ")
  params =
    auth:
      expires: expires
      key: authKey
  paramsString = JSON.stringify(params)
  signature = crypto.createHmac("sha1", authSecret).update(new Buffer(paramsString, "utf-8")).digest("hex")
  request.get
    url: "https://api2.transloadit.com/templates?params=#{encodeURIComponent(paramsString)}&signature=#{signature}"
    json: true
  , (err, r, body)->
    return next(err) if err
    if r.statusCode is 200
      next(null, body)
    else
      next(body)


updateTemplate = (templateId, templateName, templateBody, next=(->))->
  expires = moment().add('hours', 3).utc().format("YYYY/MM/DD HH:mm:ssZ")
  params =
    auth:
      expires: expires
      key: authKey
    name: templateName
    template: templateBody
  paramsString = JSON.stringify(params)
  signature = crypto.createHmac("sha1", authSecret).update(new Buffer(paramsString, "utf-8")).digest("hex")
  request.put
    url: "https://api2.transloadit.com/templates/#{templateId}"
    json:
      signature: signature
      params: paramsString
  , (err, r, body)->
    return next(err) if err
    if r.statusCode is 200
      console.log "[OK] #{templateId} -", body.ok
      next(null, body)
    else
      console.log "[ERROR] #{templateId} -", body
      next(body)


templateFiles = fs.readdirSync("templates")
getTemplates (err, templatesResponse)->
  if err
    return console.log "Error loading templates", err
  for templateFile in templateFiles
    try
      template = JSON.parse(fs.readFileSync(path.join("templates", templateFile), "utf8"))
      templateName = templateFile.replace(path.extname(templateFile), '')
      existingTemplate = _.find templatesResponse.items, (item)->item.name is templateName
      if existingTemplate
        console.log "UPDATING", existingTemplate.id, existingTemplate.name
        updateTemplate(existingTemplate.id, existingTemplate.name, template)
    catch err
      console.log "Cannot process file #{templateFile}", err

Hey Sergey,

thank you for sharing this! You should add this to a public repository so that we can link it in our community projects section.

Regarding your advice about template includes and versioning: Yes, that is on our radar as a few people have requested this. It’s almost certain that something like this will be available in the future. We cannot tell you when, though.

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

Hey Sergey,

have you added this to a public repository yet?

Kind regards,
Tim

Co-Founder Transloadit
@tim_kos

This discussion has been inactive for 30 days and has been marked as resolved automatically.
Feel free to reply to this ticket to re-open it and we will continue the investigation.

Kind regards,
Tim

Co-Founder Transloadit