Skip to content

Commit

Permalink
dodan dio 4. labosa
Browse files Browse the repository at this point in the history
  • Loading branch information
michelh-fer committed Jan 16, 2021
1 parent 7678919 commit 53218b0
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion or-lab3/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
return pool.query(text, params)
.then(res => {
const duration = Date.now() - start;
console.log('executed query', {text, params, duration, rows: res.rows});
//console.log('executed query', {text, params, duration, rows: res.rows});
return res;
});
}
Expand Down
26 changes: 26 additions & 0 deletions or-lab3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions or-lab3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"author": "MichelH",
"license": "CC0-1.0",
"dependencies": {
"axios": "^0.21.1",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-hateoas-links": "^1.2.0",
"express-validator": "^6.9.0",
"node-cache": "^5.1.2",
"pg": "^8.5.1"
}
}
94 changes: 94 additions & 0 deletions or-lab3/routes/index.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ var videogame = require('../models/VideogameModel');
var proizvodac = require('../models/ProizvodacModel');
var lik = require('../models/LikoviModel');
var izdavac = require('../models/IzdavaciModel');
const axios = require('axios')
const fs = require('fs');
var path = require('path');

//za cache
const NodeCache = require( "node-cache" );
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 120 } );

router.get('/videoigre', async function(req, res, next) {

Expand Down Expand Up @@ -66,6 +73,9 @@ router.get('/videoigre/:videogameID', async function(req, res, next) {
try {
let game = await videogame.fetchByVideogameId(req.params.videogameID);

//dodaj link na sliku
game.slika = req.protocol + '://' + req.get('host') + req.originalUrl + '/picture';

if (game != null) {

let videogameSchema = {
Expand Down Expand Up @@ -111,6 +121,90 @@ router.get('/videoigre/:videogameID', async function(req, res, next) {

});

router.get('/videoigre/:videogameID/picture', async function(req, res, next) {

const imagePath = path.join(__dirname, `../public/images/${req.params.videogameID}.png`);

//provjerava je li isteklo vrijeme trajanja, ako je onda value == undefined
var value = myCache.get( req.params.videogameID );

//ako slika ne postoji onda je dohvati ili ako je isteklo vrijeme trajanja
if (!fs.existsSync(imagePath) || value == undefined) {

var game = await videogame.fetchByVideogameId(req.params.videogameID);

if (game == null) {
res.setHeader('Content-Type', 'application/json');

res.status(404).json(
{
"status": "Not Found",
"message": "Videoigra sa danim ID-om nije pronađena",
"reponse": null
}
);
return;
}

//spremi kljuc u cache -> trenutno postavljeno vrijeme od 20 sek
var success = myCache.set( game.videoigraid, game.nazivvideoigra, 20 ); //key, val, vrijeme trajanja (u sek)

//ako je uspjesno cacheirano onda ispisi
if (success) {
var today = new Date();
var date = today.getDate() + '.' + (today.getMonth() + 1) + '.' + today.getFullYear() + '.';
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;

console.log(dateTime + " keširano " + game.nazivvideoigra + " (ID: " + game.videoigraid + ")");
}

let wikHandle = game.wikipedia_stranica;

const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${wikHandle}`;

const getData = async (url) => {
try {
const response = await axios.get(url);
const data = response.data;
//console.log(data);
return data;
} catch (error) {
console.log(error);
}
}

const download_image = async (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(
response =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(image_path))
.on('finish', () => resolve())
.on('error', e => reject(e));
}),
);


var data = await getData(url);

var image = data.thumbnail.source;

await download_image(image, `./public/images/${req.params.videogameID}.png`);

}

//postavljanje headera
res.setHeader('content-type', 'image/png');
res.setHeader('content-disposition','inline');

res.sendFile(imagePath);

});

router.post('/videoigre', async function(req, res, next) {

try {
Expand Down

0 comments on commit 53218b0

Please sign in to comment.