Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. app.post('/api/image', function(req, res, next){
  2. const contract_address = req.body.contract_address;
  3. const artist_name = req.body.artist_name;
  4. const artist_address = req.body.artist_address;
  5. const name = req.body.name;
  6. const description = req.body.description;
  7. const price = req.body.price;
  8. const supply = req.body.supply;
  9. const nb_rows = req.body.nb_rows;
  10. const nb_cols = req.body.nb_cols;
  11. const original_width = req.body.original_width;
  12. const tile_height = req.body.tile_height;
  13. const tile_width = req.body.tile_width;
  14.  
  15. const matches = req.body.uploadedImage.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
  16. response = {};
  17.  
  18. if (matches.length !== 3) {
  19. return new Error('Invalid input string');
  20. }
  21.  
  22. response.type = matches[1];
  23. response.data = new Buffer(matches[2], 'base64');
  24. const imageBuffer = response.data;
  25. const id = '_' + Math.random().toString(36).substr(2, 9);
  26. const fileName = id + '.jpg';
  27.  
  28. try {
  29. console.log('tente linput')
  30. fs.writeFileSync("./images/" + fileName, imageBuffer, 'utf8');
  31. const queryString = " INSERT INTO images(id, contract_address, artiste_name, artiste_address, name,description, price, supply, nb_rows, nb_cols, original_width,original_height, tile_height, tile_width) VALUES (1, '23443','azer', '2344', 'tyu', 'ghtgr', 23, 2, 12, 1, 2, 2, 3)"
  32. //const queryString = "INSERT INTO images(id, contract_address, artiste_name, artiste_address, name,description, price, supply, nb_rows, nb_cols, original_width,original_height, tile_height, tile_width) VALUES (?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
  33. //connection.query(queryString, [3, contract_address, artist_name, artist_address, name, description, price, supply, nb_rows, nb_cols, original_width,3, tile_height, tile_width], (err, results, fields) => {
  34. connection.query(queryString,(err, results, fields) => {
  35. if(err){
  36. console.log("Failed to insert new user.");
  37. res.sendStatus(5400);
  38. return;
  39. }
  40.  
  41. console.log('Inserted a new user with id : ', results.insertId);
  42. res.end();
  43. });
  44.  
  45. } catch (e) {
  46. next(e);
  47. }
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement