Skip to content

Commit

Permalink
feat: rework pre & post-processing
Browse files Browse the repository at this point in the history
1. allow uploading models without embedded processed archive
2. do not subtract models during post-processing
  • Loading branch information
AVVS committed Mar 26, 2016
1 parent 0fec858 commit 94d70bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 68 deletions.
69 changes: 14 additions & 55 deletions src/custom/cappasity-postprocess.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const Promise = require('bluebird');
const Errors = require('common-errors');
const { FILES_PROCESS_ERROR_FIELD, FILES_DATA } = require('../constant.js');

const TYPE_MAP = {
'c-preview': 'preview',
Expand All @@ -10,59 +8,20 @@ const TYPE_MAP = {
};

module.exports = function extractMetadata(data) {
const { amqp, config, redis } = this;
const { users: { audience, getMetadata, updateMetadata } } = config;
const username = data.owner;

return amqp
.publishAndWait(getMetadata, { username, audience })
.get(audience)
.then(metadata => {
if (metadata.roles && metadata.roles.indexOf('admin') >= 0) {
return null;
return Promise.try(function parseMeta() {
const files = JSON.parse(data.files);
const output = {};

let textures = 0;
files.forEach(({ type, filename }) => {
const responsibility = TYPE_MAP[type];
if (responsibility === 'texture') {
output[`texture_${textures++}`] = filename;
} else {
output[responsibility] = filename;
}
});

const message = {
username,
audience,
metadata: {
$incr: {
models: -1,
},
},
};

return amqp
.publishAndWait(updateMetadata, message, { timeout: 5000 })
.then(result => {
if (result.$incr.models >= 0) {
return null;
}

// revert back
message.metadata.$incr.models = 1;

// publish
return amqp
.publish(updateMetadata, message)
.then(() => redis.hset(`${FILES_DATA}:${data.uploadId}`, FILES_PROCESS_ERROR_FIELD, '402'))
.throw(new Errors.HttpStatusError(402, 'no more models are available'));
});
})
.then(() => Promise.try(function parseMeta() {
const files = JSON.parse(data.files);
const output = {};

let textures = 0;
files.forEach(({ type, filename }) => {
const responsibility = TYPE_MAP[type];
if (responsibility === 'texture') {
output[`texture_${textures++}`] = filename;
} else {
output[responsibility] = filename;
}
});

return output;
}));
return output;
});
};
15 changes: 2 additions & 13 deletions src/custom/cappasity-upload-pre.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const Promise = require('bluebird');
const Errors = require('common-errors');
const assert = require('assert');

module.exports = function extractMetadata(files, username) {
const { amqp, config } = this;
const { users: { audience, getMetadata } } = config;

module.exports = function extractMetadata(files) {
return Promise
.try(function verifyUploadData() {
const fileTypes = {};
Expand All @@ -15,15 +11,8 @@ module.exports = function extractMetadata(files, username) {

assert.equal(fileTypes['c-bin'], 1, 'must contain exactly one binary upload');
assert.equal(fileTypes['c-preview'], 1, 'must contain preview');
assert.equal(fileTypes['c-archive'], 1, 'must contain prepared archive');
assert.ok(fileTypes['c-archive'] <= 1, 'must contain not more than 1 prepared archive');
assert.ok(fileTypes['c-texture'] >= 1, 'must contain at least one texture');
})
.then(() => amqp.publishAndWait(getMetadata, { username, audience }))
.get(audience)
.then(({ models = 0, roles = [] }) => {
if (models <= 0 && roles.indexOf('admin') === -1) {
throw new Errors.HttpStatusError(402, 'no more models are available');
}
})
.return(files);
};

0 comments on commit 94d70bf

Please sign in to comment.