Merge "restbase: Use thumbnail when generating thumbnail"

This commit is contained in:
jenkins-bot 2017-03-01 22:29:56 +00:00 committed by Gerrit Code Review
commit 2631e27d6c
4 changed files with 49 additions and 24 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1,7 +1,6 @@
var RESTBASE_ENDPOINT = '/api/rest_v1/page/summary/',
RESTBASE_PROFILE = 'https://www.mediawiki.org/wiki/Specs/Summary/1.0.0',
createModel = require( '../preview/model' ).createModel,
$ = window.jQuery,
mw = window.mediaWiki;
/**
@ -50,23 +49,37 @@ function createRESTBaseGateway( ajax, config ) {
}
/**
* Takes the original thumbnail and ensure it fits within limits of THUMBNAIL_SIZE
* Resizes the thumbnail to the requested width, preserving its aspect ratio.
*
* @param {Object} original image
* @param {int} thumbSize expected thumbnail size
* The requested width is limited to that of the original image unless the image
* is an SVG, which can be scaled infinitely.
*
* This function is only intended to mangle the pretty thumbnail URLs used on
* Wikimedia Commons. Once [an official thumb API](https://phabricator.wikimedia.org/T66214)
* is fully specified and implemented, this function can be made more general.
*
* @param {Object} thumbnail The thumbnail image
* @param {Object} original The original image
* @param {int} thumbSize The requested size
* @returns {Object}
*/
function generateThumbnailData( original, thumbSize ) {
var parts = original.source.split( '/' ),
filename = parts[ parts.length - 1 ];
function generateThumbnailData( thumbnail, original, thumbSize ) {
var parts = thumbnail.source.split( '/' ),
filename = parts[ parts.length - 2 ];
if ( thumbSize > original.width && filename.indexOf( '.svg' ) === -1 ) {
thumbSize = original.width;
}
return $.extend( {}, original, {
source: parts.join( '/' ) + '/' + thumbSize + 'px-' + filename
} );
parts[ parts.length - 1 ] = thumbSize + 'px-' + filename;
return {
source: parts.join( '/' ),
// Scale the thumbnail's dimensions, preserving its aspect ratio.
width: thumbSize,
height: ( thumbSize / thumbnail.width ) * thumbnail.height
};
}
/**
@ -83,7 +96,7 @@ function convertPageToModel( page, thumbSize ) {
page.lang,
page.dir,
page.extract,
page.originalimage ? generateThumbnailData( page.originalimage, thumbSize ) : undefined
page.thumbnail ? generateThumbnailData( page.thumbnail, page.originalimage, thumbSize ) : undefined
);
}

View file

@ -3,16 +3,11 @@ var createModel = require( '../../../src/preview/model' ).createModel,
DEFAULT_CONSTANTS = {
THUMBNAIL_SIZE: 512
},
SVG_ORIGINAL_IMAGE = {
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg',
width: 800,
height: 1000
},
RESTBASE_RESPONSE = {
title: 'Barack Obama',
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
thumbnail: {
source: 'https://upload.wikimedia.org/someImage.jpg',
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/200px-President_Barack_Obama.jpg',
width: 200,
height: 250
},
@ -26,6 +21,24 @@ var createModel = require( '../../../src/preview/model' ).createModel,
timestamp: '2017-01-30T10:17:41Z',
description: '44th President of the United States of America'
},
SVG_RESTBASE_RESPONSE = {
title: 'Barack Obama',
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
thumbnail: {
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.svg/200px-President_Barack_Obama.svg',
width: 200,
height: 250
},
originalimage: {
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.svg',
width: 800,
height: 1000
},
lang: 'en',
dir: 'ltr',
timestamp: '2017-01-30T10:17:41Z',
description: '44th President of the United States of America'
},
RESTBASE_RESPONSE_WITHOUT_IMAGE = {
title: 'Barack Obama',
extract: 'Barack Hussein Obama II born August 4, 1961) ...',
@ -41,9 +54,9 @@ var createModel = require( '../../../src/preview/model' ).createModel,
'ltr',
'Barack Hussein Obama II born August 4, 1961) ...',
{
source: 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/512px-President_Barack_Obama.jpg',
width: 800,
height: 1000
source: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/512px-President_Barack_Obama.jpg',
width: 512,
height: 640
}
);
@ -91,7 +104,7 @@ QUnit.test( 'RESTBase gateway doesn\'t stretch thumbnails', function ( assert )
assert.equal(
model.thumbnail.source,
'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/800px-President_Barack_Obama.jpg',
'If the requested thumbnail size is bigger than the originalimage width the originalimage width is used'
);
} );
@ -100,12 +113,11 @@ QUnit.test( 'RESTBase gateway stretches SVGs', function ( assert ) {
var model,
gateway = createRESTBaseGateway();
model = gateway.convertPageToModel( $.extend( {}, RESTBASE_RESPONSE, { originalimage: SVG_ORIGINAL_IMAGE } ),
2000 );
model = gateway.convertPageToModel( SVG_RESTBASE_RESPONSE, 2000 );
assert.equal(
model.thumbnail.source,
'https://upload.wikimedia.org/wikipedia/commons/8/8d/Ceiling_cat.svg/2000px-Ceiling_cat.svg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.svg/2000px-President_Barack_Obama.svg',
'If the requested thumbnail size is bigger than the originalimage and its an SVG all is good'
);
} );