Created
January 28, 2014 07:36
-
-
Save srikumarks/8663615 to your computer and use it in GitHub Desktop.
Example of doxbee code written using thread.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var thread = require('./thread'); // Refers to https://gist.github.com/srikumarks/8662298 | |
| // call upload like this - | |
| // thread.fork(upload(stream, idOrPath, tag), optionalResume) | |
| function upload(stream, idOrPath, tag) { | |
| return function* (resume) { | |
| var blob = blobManager.create(account); | |
| var tx = db.begin(); | |
| try { | |
| var blobId = yield blob.put(stream, resume); | |
| var file = self.byUuidOrPath(idOrPath).get(resume); | |
| var previousId = file ? file.version : null; | |
| var version = { | |
| userAccountId: userAccount.id, | |
| date: new Date(), | |
| blobId: blobId, | |
| creatorId: userAccount.id, | |
| previousId: previousId | |
| }; | |
| version.id = Version.createHash(version); | |
| yield Version.insert(version).execWithin(tx, resume); | |
| if (!file) { | |
| var splitPath = idOrPath.split('/'); | |
| var fileName = splitPath[splitPath.length - 1]; | |
| var newId = uuid.v1(); | |
| var q = yield self.createQuery(idOrPath, { | |
| id: newId, | |
| userAccountId: userAccount.id, | |
| name: fileName, | |
| version: version.id | |
| }, resume); | |
| yield q.execWithin(tx, resume); | |
| yield afterFileExists(null, newId); | |
| } else { | |
| yield afterFileExists(null, file.id); | |
| } | |
| function afterFileExists(err, fileId) { | |
| return thread.fork(function *(resume) { | |
| yield FileVersion.insert({ | |
| fileId: fileId, | |
| versionId: version.id | |
| }).execWithin(tx, resume); | |
| yield File.whereUpdate({id: fileId}, { | |
| version: version.id | |
| }).execWithin(tx, resume); | |
| yield tx.commit(resume); | |
| }, resume); | |
| } | |
| } catch (e) { | |
| tx.rollback(); | |
| throw e; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment