'use strict';

const Challenge = require('../../models/challenge');
const request = require('request');

const newPost = (req, res) => {
    const baseHostingUrl = 'http://ec2-52-35-85-119.us-west-2.compute.amazonaws.com:8080/';
    const url = `${baseHostingUrl}picture`;
    const body = req.body;
    const imageBuffer = new Buffer(body.image, 'base64');
    const formData = {
        file: {
            value: imageBuffer,
            options: {
                filename: `travelAndHelp${imageBuffer.length}`,
                contentType: 'image'
            }
        }
    };
    request.post(
        {
            url,
            formData
        },
        (error, httpResponse, respBody) => {
            if (!error && respBody && respBody.path) {
                body.image = `${baseHostingUrl}${respBody.path}`;
                const model = new Challenge(body);

                model.save((err, challenge) => {
                    if (!err) {
                        res.json(challenge);
                    } else {
                        res.status(400).send(err);
                    }
                });
            }
            res.json('bad response from external hosting');
        }
    );
};

module.exports = newPost;

@@ -138,148 +138,10 @@
     
-const baseHostingUrl = 'http://ec2-52-35-85-119.us-west-2.compute.amazonaws.com:8080/';%0A    const url = %60$%7BbaseHostingUrl%7Dpicture%60;%0A    cons
+le
 t bo
@@ -439,16 +439,104 @@
 %0A    %7D;%0A
+    const url = 'http://ec2-52-35-85-119.us-west-2.compute.amazonaws.com:8080/picture';%0A
     requ
@@ -739,24 +739,69 @@
 = %60$
-%7BbaseHostingUrl%7D
+http://ec2-52-35-85-119.us-west-2.compute.amazonaws.com:8080/
 $%7Bre

test fixed