From 8ea7a1f82587e938a75c705f60006f40477593dd Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Fri, 15 Nov 2019 15:28:30 +0100 Subject: [PATCH] HW7: done task 7 --- hw7/Claudio_Maggioni/package.json | 3 +- hw7/Claudio_Maggioni/public/js/views.js | 2 +- hw7/Claudio_Maggioni/public/scripts/app.js | 24 +++++++++ hw7/Claudio_Maggioni/public/style.css | 5 +- hw7/Claudio_Maggioni/routes/root/router.js | 58 +++++++++++++++++++++- hw7/Claudio_Maggioni/views/index.dust | 7 +++ hw7/Claudio_Maggioni/yarn.lock | 21 +++++++- 7 files changed, 114 insertions(+), 6 deletions(-) diff --git a/hw7/Claudio_Maggioni/package.json b/hw7/Claudio_Maggioni/package.json index 4eb3b3d..a5f5ab2 100644 --- a/hw7/Claudio_Maggioni/package.json +++ b/hw7/Claudio_Maggioni/package.json @@ -20,9 +20,10 @@ "license": "MIT", "dependencies": { "body-parser": "^1.19.0", - "debug": "^3.1.0", + "debug": "^4.1.1", "dustjs-linkedin": "^2.7.5", "express": "^4.16.2", + "fetch-base64": "^2.1.2", "form-data": "^3.0.0", "klei-dust": "^1.0.0", "method-override": "^3.0.0", diff --git a/hw7/Claudio_Maggioni/public/js/views.js b/hw7/Claudio_Maggioni/public/js/views.js index 1bd0158..e136826 100644 --- a/hw7/Claudio_Maggioni/public/js/views.js +++ b/hw7/Claudio_Maggioni/public/js/views.js @@ -2,5 +2,5 @@ (function(dust){dust.register("favourite",body_0);function body_0(chk,ctx){return chk.w("").f(ctx.get(["name"], false),ctx,"h").w("").p("favourite_partial",ctx,ctx,{}).w("Favourites list");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourite_partial",body_0);function body_0(chk,ctx){return chk.w("

").f(ctx.get(["name"], false),ctx,"h").w("

\"").f(ctx.get(["name"],").x(ctx.get(["b"], false),ctx,{"block":body_1},{}).x(ctx.get(["details"], false),ctx,{"else":body_2,"block":body_5},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("

Bookmarked

");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("




").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).w("






");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("Details");}body_5.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("favourites",body_0);function body_0(chk,ctx){return chk.w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_1,"block":body_2},{}).w("").x(ctx.get(["bookmarked"], false),ctx,{"else":body_3,"block":body_4},{}).s(ctx.get(["favs"], false),ctx,{"else":body_5,"block":body_6},{}).w("");}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("Favourites");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("Bookmarked");}body_2.__dustBody=!0;function body_3(chk,ctx){return chk.w("

Favourites

");}body_3.__dustBody=!0;function body_4(chk,ctx){return chk.w("

Bookmarked

");}body_4.__dustBody=!0;function body_5(chk,ctx){return chk.w("No favourites.");}body_5.__dustBody=!0;function body_6(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false),"details":"true"}).w("
");}body_6.__dustBody=!0;return body_0}(dust)); -(function(dust){dust.register("index",body_0);function body_0(chk,ctx){return chk.w("OO-JS Exercise - Web Atelier 2017

OO-JS Exercise: Canvas



");}body_0.__dustBody=!0;return body_0}(dust)); +(function(dust){dust.register("index",body_0);function body_0(chk,ctx){return chk.w("OO-JS Exercise - Web Atelier 2017

OO-JS Exercise: Canvas




");}body_0.__dustBody=!0;return body_0}(dust)); (function(dust){dust.register("server_side",body_0);function body_0(chk,ctx){return chk.s(ctx.get(["favs"], false),ctx,{"block":body_1},{});}body_0.__dustBody=!0;function body_1(chk,ctx){return chk.w("
").p("favourite_partial",ctx,ctx,{"name":ctx.get(["name"], false),"dataURL":ctx.get(["dataURL"], false),"_id":ctx.get(["_id"], false),"bookmarked":ctx.get(["bookmarked"], false)}).w("
");}body_1.__dustBody=!0;function body_2(chk,ctx){return chk.w("bookmarked");}body_2.__dustBody=!0;return body_0}(dust)); diff --git a/hw7/Claudio_Maggioni/public/scripts/app.js b/hw7/Claudio_Maggioni/public/scripts/app.js index b631e39..30cc546 100644 --- a/hw7/Claudio_Maggioni/public/scripts/app.js +++ b/hw7/Claudio_Maggioni/public/scripts/app.js @@ -292,6 +292,30 @@ class App { nameFilter.onkeyup = onSearch; bookmarkFilter.onclick = onSearch; + const imgurSearch = document.getElementById('imgur-search'); + const imgurSearchBtn = document.getElementById('imgur-search-btn'); + const imgurSearchDetails = document.getElementById('imgur-search-details'); + + imgurSearchBtn.onclick = async () => { + imgurSearch.classList.remove('ok'); + imgurSearch.classList.remove('err'); + imgurSearchDetails.innerHTML = '
'; + + const e = await doJSONRequest('POST', '/imgur/search', {}, + { name: imgurSearch.value }); + + if (e.success) { + imgurSearch.classList.add('ok'); + } else { + imgurSearch.classList.add('err'); + + if(e.error) { + imgurSearchDetails.innerHTML = ` + Error: ${e.error}
`; + } + } + }; + if (typeof conf.buttons === 'object' && conf.buttons) { this.buttons = {} for (const b of ['clear', 'undo', 'camera']) diff --git a/hw7/Claudio_Maggioni/public/style.css b/hw7/Claudio_Maggioni/public/style.css index be93057..151183a 100644 --- a/hw7/Claudio_Maggioni/public/style.css +++ b/hw7/Claudio_Maggioni/public/style.css @@ -118,7 +118,10 @@ body { margin: 5px; float: left; width: 200px; - font-size: 0.7em; +} + +#favorites, #imgur-search-details { + font-size: 0.7rem; } #favourites img { diff --git a/hw7/Claudio_Maggioni/routes/root/router.js b/hw7/Claudio_Maggioni/routes/root/router.js index c1dd0e9..df96c14 100644 --- a/hw7/Claudio_Maggioni/routes/root/router.js +++ b/hw7/Claudio_Maggioni/routes/root/router.js @@ -10,6 +10,8 @@ const Favorite = mongoose.model('Favorite'); const fetch = require('node-fetch'); const querystring = require('querystring'); const promiseAny = require('promise-any'); +const eventBus = require('../../pubsub'); +const fetch64 = require('fetch-base64'); const { error } = require('../utils'); @@ -103,7 +105,6 @@ router.post('/imgur/ordeal', async (req, res) => { return; } - console.log(req.body.favorites); if (req.body.favorites) { if (!await fetchImgur(req, res, 'POST', '/album/' + albumJson.data.id + '/favorite', {})) { @@ -222,7 +223,60 @@ router.post('/imgur/ordeal', async (req, res) => { res.json(ordealResponse); } catch(e) { console.error(e); - res.status(500).json(e); + res.status(500).json({ error: e.toString() }); + } +}); + +router.post('/imgur/search', async (req, res) => { + try { + if (!req.body.name) { + res.status(400).json({ error: 'Bad parameters for /imgur/search' }); + return; + } + + const results = await fetchImgur(req, res, 'GET', '/gallery/search/time/top/1?q=' + + encodeURIComponent(req.body.name) + '&q_type=png'); + + if (!results) { + return; + } + + if (results.data.length == 0) { + res.status(404).json({ error: 'No image found' }); + return; + } + + let image = results.data[0]; + if (image.is_album) { + image = image.images[0]; + } + + if (!await fetchImgur(req, res, 'POST', '/gallery/' + results.data[0].id + + '/vote/up', {})) { + return; + } + + if (!await fetchImgur(req, res, 'POST', '/image/' + image.id + '/favorite', + {})) { + return; + } + + const fetch64res = await fetch64.remote(image.link); + + const fav = { + _id: mongoose.Types.ObjectId(), + name: req.body.name, + dataURL: fetch64res[1], + bookmarked: false, + }; + + await new Favorite(fav).save(); + eventBus.emit('favorite.created', fav); + + res.json({ success: true }); + } catch(e) { + console.error(e); + res.status(500).json({ error: e.toString() }); } }); diff --git a/hw7/Claudio_Maggioni/views/index.dust b/hw7/Claudio_Maggioni/views/index.dust index 1f5dd4e..72fa951 100644 --- a/hw7/Claudio_Maggioni/views/index.dust +++ b/hw7/Claudio_Maggioni/views/index.dust @@ -29,6 +29,13 @@
+ + + +
+
+
+
diff --git a/hw7/Claudio_Maggioni/yarn.lock b/hw7/Claudio_Maggioni/yarn.lock index 087d9f5..7f23c82 100644 --- a/hw7/Claudio_Maggioni/yarn.lock +++ b/hw7/Claudio_Maggioni/yarn.lock @@ -472,7 +472,7 @@ debug@^3.1.0, debug@^3.2.6: dependencies: ms "^2.1.1" -debug@~4.1.0: +debug@^4.1.1, debug@~4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== @@ -751,6 +751,13 @@ fast-json-stable-stringify@^2.0.0: resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2" integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I= +fetch-base64@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/fetch-base64/-/fetch-base64-2.1.2.tgz#460e581c823633aade6fa7fc8dc3f2cdb1da15c4" + integrity sha512-APL3vK1yYXUZ5spJFmojP/xXepjpsJUy/4oCijsvOlHBm9lEJtqOHgFJD8AMP/Rlr/1DlGxLc5InA0SqFf05zA== + dependencies: + mime-types "^2.1.17" + filename-regex@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" @@ -1433,6 +1440,11 @@ mime-db@1.40.0: resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32" integrity sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA== +mime-db@1.42.0: + version "1.42.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.42.0.tgz#3e252907b4c7adb906597b4b65636272cf9e7bac" + integrity sha512-UbfJCR4UAVRNgMpfImz05smAXK7+c+ZntjaA26ANtkXLlOe947Aag5zdIcKQULAiF9Cq4WxBi9jUs5zkA84bYQ== + mime-types@^2.1.12, mime-types@~2.1.19, mime-types@~2.1.24: version "2.1.24" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81" @@ -1440,6 +1452,13 @@ mime-types@^2.1.12, mime-types@~2.1.19, mime-types@~2.1.24: dependencies: mime-db "1.40.0" +mime-types@^2.1.17: + version "2.1.25" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.25.tgz#39772d46621f93e2a80a856c53b86a62156a6437" + integrity sha512-5KhStqB5xpTAeGqKBAMgwaYMnQik7teQN4IAzC7npDv6kzeU6prfkR67bc87J1kWMPGkoaZSq1npmexMgkmEVg== + dependencies: + mime-db "1.42.0" + mime@1.6.0, mime@^1.4.1: version "1.6.0" resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"