Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add v3.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickRe committed Jun 18, 2020
1 parent 654f1b9 commit 80b5fe5
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 28 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

11 changes: 8 additions & 3 deletions core/server/api/canary/oembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {extract, hasProvider} = require('oembed-parser');
const Promise = require('bluebird');
const cheerio = require('cheerio');
const _ = require('lodash');
const {CookieJar} = require('tough-cookie');
const config = require('../../../shared/config');
const {i18n} = require('../../lib/common');
const externalRequest = require('../../lib/request-external');
Expand All @@ -23,7 +24,8 @@ async function fetchBookmarkData(url, html) {

try {
if (!html) {
const response = await externalRequest(url);
const cookieJar = new CookieJar();
const response = await externalRequest(url, {cookieJar});
html = response.body;
}
scraperResponse = await metascraper({html, url});
Expand Down Expand Up @@ -132,10 +134,12 @@ function fetchOembedData(_url) {

// url not in oembed list so fetch it in case it's a redirect or has a
// <link rel="alternate" type="application/json+oembed"> element
const cookieJar = new CookieJar();
return externalRequest(url, {
method: 'GET',
timeout: 2 * 1000,
followRedirect: true
followRedirect: true,
cookieJar
}).then((pageResponse) => {
// url changed after fetch, see if we were redirected to a known oembed
if (pageResponse.url !== url) {
Expand Down Expand Up @@ -164,7 +168,8 @@ function fetchOembedData(_url) {
method: 'GET',
json: true,
timeout: 2 * 1000,
followRedirect: true
followRedirect: true,
cookieJar
}).then((oembedResponse) => {
// validate the fetched json against the oembed spec to avoid
// leaking non-oembed responses
Expand Down
12 changes: 6 additions & 6 deletions core/server/lib/image/image-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ function _imageSizeFromBuffer(buffer) {

// use probe-image-size to download enough of an image to get it's dimensions
// returns promise which resolves dimensions
function _probeImageSizeFromUrl(url) {
function _probeImageSizeFromUrl(imageUrl) {
// probe-image-size uses `request` npm module which doesn't have our `got`
// override with custom URL validation so it needs duplicating here
if (_.isEmpty(url) || !validator.isURL(url)) {
if (_.isEmpty(imageUrl) || !validator.isURL(imageUrl)) {
return Promise.reject(new errors.InternalServerError({
message: 'URL empty or invalid.',
code: 'URL_MISSING_INVALID',
context: url
context: imageUrl
}));
}

return probeSizeOf(url, REQUEST_OPTIONS);
return probeSizeOf(imageUrl, REQUEST_OPTIONS);
}

// download full image then use image-size to get it's dimensions
// returns promise which resolves dimensions
function _fetchImageSizeFromUrl(url) {
return request(url, REQUEST_OPTIONS).then((response) => {
function _fetchImageSizeFromUrl(imageUrl) {
return request(imageUrl, REQUEST_OPTIONS).then((response) => {
return _imageSizeFromBuffer(response.body);
});
}
Expand Down
80 changes: 80 additions & 0 deletions core/server/lib/mobiledoc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const path = require('path');
const errors = require('@tryghost/errors');
const imageTransform = require('@tryghost/image-transform');
const logging = require('../../shared/logging');
const config = require('../../shared/config');
const storage = require('../adapters/storage');

let cardFactory;
let cards;
Expand Down Expand Up @@ -78,6 +81,83 @@ module.exports = {
}
},

// used when force-rerendering post content to ensure that old image card
// payloads contain width/height values to be used when generating srcsets
populateImageSizes: async function (mobiledocJson) {
// do not require image-size until it's requested to avoid circular dependencies
// shared/url-utils > server/lib/mobiledoc > server/lib/image/image-size > server/adapters/storage/utils
const imageSize = require('./image/image-size');
const urlUtils = require('../../shared/url-utils');
const storageInstance = storage.getStorage();

async function getUnsplashSize(url) {
const parsedUrl = new URL(url);
parsedUrl.searchParams.delete('w');
parsedUrl.searchParams.delete('fit');
parsedUrl.searchParams.delete('crop');
parsedUrl.searchParams.delete('dpr');

return await imageSize.getImageSizeFromUrl(parsedUrl.href);
}

// TODO: extract conditional logic lifted from handle-image-sizes.js
async function getLocalSize(url) {
// skip local images if adapter doesn't support size transforms
if (typeof storageInstance.saveRaw !== 'function') {
return;
}

// local storage adapter's .exists() expects image paths without any prefixes
const imageUrlPrefix = urlUtils.urlJoin(urlUtils.getSubdir(), urlUtils.STATIC_IMAGE_URL_PREFIX);
const storagePath = url.replace(imageUrlPrefix, '');

const {dir, name, ext} = path.parse(storagePath);
const [imageNameMatched, imageName, imageNumber] = name.match(/^(.+?)(-\d+)?$/) || [null];

if (!imageNameMatched
|| !imageTransform.canTransformFileExtension(ext)
|| !(await storageInstance.exists(storagePath))
) {
return;
}

// get the original/unoptimized image if it exists as that will have
// the maximum dimensions that srcset/handle-image-sizes can use
const originalImagePath = path.join(dir, `${imageName}_o${imageNumber || ''}${ext}`);
const imagePath = await storageInstance.exists(originalImagePath) ? originalImagePath : storagePath;

return await imageSize.getImageSizeFromStoragePath(imagePath);
}

const mobiledoc = JSON.parse(mobiledocJson);

const sizePromises = mobiledoc.cards.map(async (card) => {
const [cardName, payload] = card;

const needsFilling = cardName === 'image' && (!payload.width || !payload.height);
if (!needsFilling) {
return;
}

const isUnsplash = payload.src.match(/images\.unsplash\.com/);
try {
const size = isUnsplash ? await getUnsplashSize(payload.src) : await getLocalSize(payload.src);

if (size && size.width && size.height) {
payload.width = size.width;
payload.height = size.height;
}
} catch (e) {
// TODO: use debug instead?
logging.error(e);
}
});

await Promise.all(sizePromises);

return JSON.stringify(mobiledoc);
},

// allow config changes to be picked up - useful in tests
reload() {
cardFactory = null;
Expand Down
6 changes: 6 additions & 0 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ Post = ghostBookshelf.Model.extend({
}
});

// If we're force re-rendering we want to make sure that all image cards
// have original dimensions stored in the payload for use by card renderers
if (options.force_rerender) {
this.set('mobiledoc', mobiledocLib.populateImageDimensions(this.get('mobiledoc')));
}

// CASE: mobiledoc has changed, generate html
// CASE: ?force_rerender=true passed via Admin API
// CASE: html is null, but mobiledoc exists (only important for migrations & importing)
Expand Down
6 changes: 3 additions & 3 deletions core/server/web/admin/views/default-prod.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


<link rel="stylesheet" href="assets/vendor.min-59d06862d7441e413f92fcc5c41f807e.css">
<link rel="stylesheet" href="assets/ghost.min-0fa703e24ef9be3766e2936266be350a.css" title="light">
<link rel="stylesheet" href="assets/ghost.min-5939ca7bf2b88e36ee65616ee7a850c6.css" title="light">



Expand All @@ -52,8 +52,8 @@
<div id="ember-basic-dropdown-wormhole"></div>


<script src="assets/vendor.min-8d88bef11b1a278f73afc4c9fb07fcd5.js"></script>
<script src="assets/ghost.min-c021022b248b68d40bbd85069d1ef638.js"></script>
<script src="assets/vendor.min-f56780de5125e2a9ff22d96dd6d15441.js"></script>
<script src="assets/ghost.min-cd9630caf9bb4691d80481efca1c4f4b.js"></script>

</body>
</html>
6 changes: 3 additions & 3 deletions core/server/web/admin/views/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


<link rel="stylesheet" href="assets/vendor.min-59d06862d7441e413f92fcc5c41f807e.css">
<link rel="stylesheet" href="assets/ghost.min-0fa703e24ef9be3766e2936266be350a.css" title="light">
<link rel="stylesheet" href="assets/ghost.min-5939ca7bf2b88e36ee65616ee7a850c6.css" title="light">



Expand All @@ -52,8 +52,8 @@
<div id="ember-basic-dropdown-wormhole"></div>


<script src="assets/vendor.min-8d88bef11b1a278f73afc4c9fb07fcd5.js"></script>
<script src="assets/ghost.min-c021022b248b68d40bbd85069d1ef638.js"></script>
<script src="assets/vendor.min-f56780de5125e2a9ff22d96dd6d15441.js"></script>
<script src="assets/ghost.min-cd9630caf9bb4691d80481efca1c4f4b.js"></script>

</body>
</html>
4 changes: 3 additions & 1 deletion core/shared/url-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const UrlUtils = require('@tryghost/url-utils');
const config = require('./config');
const mobiledoc = require('../server/lib/mobiledoc');

const urlUtils = new UrlUtils({
url: config.get('url'),
Expand All @@ -11,6 +10,9 @@ const urlUtils = new UrlUtils({
redirectCacheMaxAge: config.get('caching:301:maxAge'),
baseApiPath: '/ghost/api',
get cardTransformers() {
// do not require mobiledoc until it's requested to avoid circular dependencies
// shared/url-utils > server/lib/mobiledoc > server/lib/image/image-size > server/adapters/storage/utils
const mobiledoc = require('../server/lib/mobiledoc');
return mobiledoc.cards;
}
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ghost",
"version": "3.20.0",
"version": "3.20.1",
"description": "The professional publishing platform",
"author": "Ghost Foundation",
"homepage": "https://ghost.org",
Expand Down Expand Up @@ -127,6 +127,7 @@
"rss": "1.2.2",
"sanitize-html": "1.26.0",
"semver": "7.3.2",
"tough-cookie": "4.0.0",
"uuid": "8.1.0",
"validator": "6.3.0",
"xml": "1.0.1",
Expand Down
Loading

0 comments on commit 80b5fe5

Please sign in to comment.