Commit 842ff209 authored by Laurent's avatar Laurent

fetch and cache thumbnails when portal sends redirections to another serveur on image request

parent 4d5eaee5
import * as http from 'http';
import {ImageSource} from 'tns-core-modules/image-source'
import {Cookies,
NSHTTPClient,
Account,
Database,
ItemOperation,
Loan,
Item,
Hold} from '../../models';
......@@ -143,6 +145,41 @@ export abstract class PortalAdapter {
}
protected _fetchThumbnails<T extends ItemOperation>(account: Account,
items: Array<T>): Promise<Array<T>> {
return Promise.all( items.map( (item) => {
return this._fetchThumbnail(account, item);
}))
}
protected _fetchThumbnail<T extends ItemOperation>(account: Account, item: T): Promise<T> {
if ( !item.getRecordThumbnail() )
return Promise.resolve(item);
return this
.request(account,
{
url: item.getRecordThumbnail(),
headers: { Referer: account.getUrl() }
})
.then( (response) => {
if (response.statusCode == 301 || response.statusCode == 302) {
let location = <any>response.headers['Location']
item.setRecordThumbnail(location)
return this._fetchThumbnail(account, item)
}
return response.content
.toImage()
.then( (image_source: ImageSource) => {
return item.setRecordThumbnail('data:image/jpg;base64,' + image_source.toBase64String('jpg'));
})
})
}
protected _encodeForm(form: Object): string {
let datas = [];
for(let key in form)
......
import {PortalAdapter} from './adapter'
import * as cheerio from 'cheerio'
import {Account, Loan, Hold} from '../../models'
import {Account, ItemOperation, Loan, Hold} from '../../models'
import {WrongLoginPassword} from './wrong-login-password'
import * as papa from 'papaparse'
......@@ -145,6 +145,12 @@ export class Ermes extends PortalAdapter {
.then( (response) => {
return this._parseLoansHTML(account, response)
})
.then( (loans) => {
return this._fetchThumbnails(account, loans);
})
.then( (loans) => {
return loans
})
}
......@@ -202,6 +208,12 @@ export class Ermes extends PortalAdapter {
.then( (response) => {
return this._parseHoldsHTML(account, response)
})
.then( (holds) => {
return this._fetchThumbnails(account, holds);
})
.then( (holds) => {
return holds
})
}
......
......@@ -65,7 +65,6 @@ export class IguanaV3 extends PortalAdapter {
.setDateDue(this._formatDate(item.DueDate._text))
.setLibrary(item.LoanLocation._text.split('^')[0])
.setRecordId(item.Link._text)
.setRecordThumbnail(item.Image._text)
})
})
}
......@@ -98,7 +97,6 @@ export class IguanaV3 extends PortalAdapter {
.setStatus(item.AvailableSince._text ? L('hold_available') : L('hold_not_available'))
.setLibrary(item.PickupLocations.Location.Name._text)
.setRecordId(item.Link._text)
.setRecordThumbnail(item.Image._text)
})
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment