Skip to content

Instantly share code, notes, and snippets.

@Raynos

Raynos/gens.js Secret

Last active December 20, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/2d1e0d7e55e4553249db to your computer and use it in GitHub Desktop.
Save Raynos/2d1e0d7e55e4553249db to your computer and use it in GitHub Desktop.
var async = require("gens")
var parallel = require("gens/parallel")
function upload(stream, idOrPath, tag, done) {
var tx = db.begin()
uploadTransaction(tx, stream, idOrPath, tag, function (err) {
if (err) tx.rollback()
callback.apply(this, arguments)
})
}
var uploadTransaction = async(function upload(tx, stream, idOrPath, tag) {
var blob = blobManager.create(account)
var results = yield parallel({
file: self.byUuidOrPath.bind(self, idOrPath),
blobId: blob.put.bind(null, stream)
})
var previousId = results.file ? results.file.version : null
var version = {
userAccountId: userAccountId,
date: new Date(),
blobId: results.blobId,
creatorId: userAccountId.id,
previousId: previousId
}
version.id = Version.createHash(version)
yield Version.insert.bind(Version, version)
var fileId = results.file.id
if (!results.file) {
var splitPath = idOrPath.split("/")
var fileName = splitPath[splitPath.length - 1]
fileId = uuid.v1()
var query = yield self.createQuery.bind(self, idOrPath, {
id: fileId,
userAccountId: userAccount.id,
name: fileName,
version: version.id
})
yield query.execWithin.bind(query, tx)
}
yield parallel({
version: FileVersion.insert({ fileId: fileId, versionId: version.id })
.execWithin.bind(null, tx)
file: File.whereUpdate({ id: fileId }, { version: version.id })
.execWithin.bind(null, tx)
})
yield tx.commit()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment