Skip to content

Instantly share code, notes, and snippets.

@reginaldojunior
Last active April 19, 2016 13:29
Show Gist options
  • Save reginaldojunior/c48cab4d9517ce60c579c8838d1753a5 to your computer and use it in GitHub Desktop.
Save reginaldojunior/c48cab4d9517ce60c579c8838d1753a5 to your computer and use it in GitHub Desktop.
get image
var express = require('express');
var router = express.Router();
var Product = require('../models/product').Product;
var common = require('../aux/common');
var url = require('url');
var http = require('http');
var sizeOf = require('image-size');
var score = 0;
var verifyProductById = function(req, res) {
console.log(req);
};
var verifyAllProducts = function(req, res) {
var ProductQuery = Product.find({});
ProductQuery.where('inspected', false);
ProductQuery.limit(1);
ProductQuery.skip(100);
ProductQuery.exec(function (err, docs) {
if (!err && docs) {
response = [];
for (i in docs) {
if (!verifyProduct(res, req, docs[i])) {
response.push({'error': 'An error ocurrered', 'doc': docs[i]});
}
}
common.returnJsonSucess(res, req, response, 200);
} else if (err) {
if (err.name == 'CastError'){
common.returnJsonError(res, req, 404, 'not_found', 'Product not found.', err);
} else {
common.returnJsonError(res, req, 400, 'query_error', 'An error occurred, see details for more information.', err);
}
} else {
common.returnJsonError(res, req, 404, 'not_found', 'Product not found.', err);
}
});
};
var verifyProduct = function(res, req, product) {
var response = [];
if (product['name'] != undefined && typeof product['name'] != undefined) {
score += 1;
response.push({
'field' : 'name',
'message': 'Name is OK',
'valid' : true
});
} else {
response.push({
'field' : 'name',
'message': 'Name is empty',
'valid' : false
});
}
if (product['sku'] != undefined && typeof product['sku'] != undefined) {
score += 1;
response.push({
'field' : 'sku',
'message': 'SKU is OK',
'valid' : true
});
} else {
response.push({
'field' : 'sku',
'message': 'SKU is empty',
'valid' : false
});
}
if (product['ean'] != undefined && typeof product['ean'] != undefined) {
score += 1;
response.push({
'field' : 'ean',
'message': 'EAN is OK',
'valid' : true
});
} else {
response.push({
'field' : 'ean',
'message': 'EAN is empty',
'valid' : false
});
}
if (product['categories'] != undefined && typeof product['categories'] != undefined) {
score += 1;
response.push({
'field' : 'categories',
'message': 'Categories is OK',
'valid' : true
});
} else {
response.push({
'field' : 'categories',
'message': 'Categories is empty',
'valid' : false
});
}
if (product['photos'] != undefined && typeof product['photos'] != undefined) {
score += 1;
for (i in product['photos']) {
if (product['photos'][i]['url']) {
var options = url.parse(product['photos'][i]['url']);
var result_image = {};
http.get(options, function (response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function() {
var buffer = Buffer.concat(chunks);
if (sizeOf(buffer)['height'] < 240) {
score -= 1;
}
if (sizeOf(buffer)['width'] < 240) {
score -= 1;
}
score -= 1;
if (sizeOf(buffer)['type'] != 'jpg' && sizeOf(buffer)['type'] != 'jpeg') {
score -= 1;
}
console.log(score);
});
});
}
}
response.push({
'field' : 'photos',
'message': 'Photos is OK',
'valid' : true
});
} else {
response.push({
'field' : 'photos',
'message': 'Photos is empty',
'valid' : false
});
}
if (product['url'] != undefined && typeof product['url'] != undefined) {
score += 1;
response.push({
'field' : 'url',
'message': 'URL is OK',
'valid' : true
});
} else {
response.push({
'field' : 'url',
'message': 'URL is empty',
'valid' : false
});
}
if (product['description'] != undefined && typeof product['description'] != undefined) {
score += 1;
if (product['description'].match(/br/g) === null) {
score -= 1;
response.push({
'field' : 'description',
'message': 'Description not contains the tag br',
'valid' : false
});
}
if (product['description'].match(/style/g) !== null) {
score -= 1;
response.push({
'field' : 'description',
'message': 'Description not can style css inline',
'valid' : false
});
}
response.push({
'field' : 'description',
'message': 'Description is OK',
'valid' : true
});
} else {
response.push({
'field' : 'description',
'message': 'Description is empty',
'valid' : false
});
}
console.log(score);
// Product.update({_id: product['id']}, { $set: { inspected: true } }, { multi: true }, function(err, doc){});
// Product.update({_id: product['id']}, { $set: { inspected_fields: response }}, { multi: true }, function(err, doc){});
};
router.get('/all', verifyAllProducts);
router.get('/:id', verifyProductById);
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment