From 296b0c9952c6d697a3b5952dcc7dd370b0182470 Mon Sep 17 00:00:00 2001 From: moom0o <48740106+moom0o@users.noreply.github.com> Date: Wed, 5 Apr 2023 19:32:37 -0400 Subject: [PATCH] Update index.js --- index.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index a0feeec..202ee71 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ app.use( app.get('/', function (req, res) { res.sendFile(__dirname + '/views/index.html') }) -app.get('/upload', function(req, res){ +app.get('/upload', function (req, res) { res.sendFile(__dirname + '/views/upload.html') }) app.post('/upload', function (req, res) { @@ -74,14 +74,9 @@ app.post('/upload', function (req, res) { console.log(error) && res.status(500).send("Error with uploading file to uploadUrl"); } else { //PART 3: Set metadata! - const options = { - 'method': 'POST', - 'url': `https://streetviewpublish.googleapis.com/v1/photo?key=${apiKey}`, - 'headers': { - 'Authorization': `Bearer ${key}`, - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ + let body; + if (req.body["lat"] && req.body["long"]) { + body = JSON.stringify({ "uploadReference": { "uploadUrl": uploadUrl }, @@ -93,13 +88,32 @@ app.post('/upload', function (req, res) { "heading": 0 } }) + } else { + body = JSON.stringify({ + "uploadReference": { + "uploadUrl": uploadUrl + }, + }) + } + const options = { + 'method': 'POST', + 'url': `https://streetviewpublish.googleapis.com/v1/photo?key=${apiKey}`, + 'headers': { + 'Authorization': `Bearer ${key}`, + 'Content-Type': 'application/json' + }, + body: body }; request(options, function (error, response) { if (error) { console.log(error) && res.status(500).send("Error with setting metadata of file"); } else { - res.status(200).send(`Status: ${JSON.parse(response.body)["mapsPublishStatus"]}
Link: ${JSON.parse(response.body)["shareLink"]}
You may have to wait awhile after uploading for Google to process the image.
Upload another?`) + if (JSON.parse(response.body)["error"]) { + res.status(JSON.parse(response.body)["error"]["code"]).send(`Status: ${JSON.parse(response.body)["error"]["status"]}
Error message: ${JSON.parse(response.body)["error"]["message"]}
Try adding the latitude and longitude coordinates.
Upload another?`) + } else { + res.status(200).send(`Status: ${JSON.parse(response.body)["mapsPublishStatus"]}
Link: ${JSON.parse(response.body)["shareLink"]}
You may have to wait awhile after uploading for Google to process the image.
Upload another?`) + } } }); } @@ -119,7 +133,10 @@ app.get('/auth', function (req, res) { }; request(options, function (error, response) { if (error) console.log(error) && res.send("Error: Check console"); - res.cookie('oauth', JSON.parse(response.body)["access_token"], {maxAge: 900000, httpOnly: true}); + res.cookie('oauth', JSON.parse(response.body)["access_token"], { + maxAge: JSON.parse(response.body)["expires_in"] * 1000, + httpOnly: true + }); res.sendFile(__dirname + '/views/upload.html') });