Skip to content

Commit

Permalink
feat: allow images in post/tag/author view settings (#869430ztq)
Browse files Browse the repository at this point in the history
  • Loading branch information
dziudek committed Mar 16, 2024
1 parent de593cb commit f0854b6
Show file tree
Hide file tree
Showing 16 changed files with 274 additions and 48 deletions.
17 changes: 15 additions & 2 deletions app/back-end/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class Author extends Model {
featuredImage = path.parse(this.additionalData.featuredImage).base;
}

// If post is cancelled - get the previous featured image
// If author is cancelled - get the previous featured image
if (cancelEvent && this.id !== 0) {
let featuredImageSqlQuery = `SELECT additional_data FROM authors WHERE id = @id`;

Expand All @@ -313,6 +313,12 @@ class Author extends Model {
authorDir = 'temp';
}

let imagesInAuthorViewSettings = [];

if (this.additionalData && this.additionalData.viewConfig) {
imagesInAuthorViewSettings = Object.values(this.additionalData.viewConfig).filter(item => item.type === "image").map(item => item.value);
}

// Iterate through images
for (let i in images) {
let imagePath = images[i];
Expand All @@ -323,7 +329,14 @@ class Author extends Model {
continue;
}

if ((cancelEvent && authorDir === 'temp') || featuredImage !== imagePath) {
// Remove files which does not exist as featured image and authorViewSettings
if(
(cancelEvent && authorDir === 'temp') ||
(
imagesInAuthorViewSettings.indexOf(imagePath) === -1 &&
featuredImage !== imagePath
)
) {
try {
fs.unlinkSync(fullPath);
} catch(e) {
Expand Down
4 changes: 2 additions & 2 deletions app/back-end/builddata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.45.2",
"build": 16609
}
"build": 16629
}
13 changes: 12 additions & 1 deletion app/back-end/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Image extends Model {

if (imageData.id === 'website') {
this.id = 'website';
} else if (imageData.id === 'defaults') {
this.id = 'defaults';
}

// App instance
Expand Down Expand Up @@ -95,7 +97,16 @@ class Image extends Model {
let galleryDirPath = '';
let responsiveDirPath = '';

if (this.imageType === 'pluginImages') {
if (this.id === 'defaults' && this.imageType === 'contentImages') {
dirPath = path.join(this.siteDir, 'input', 'media', 'posts', 'defaults');
responsiveDirPath = path.join(this.siteDir, 'input', 'media', 'posts', 'defaults', 'responsive');
} else if (this.id === 'defaults' && this.imageType === 'tagImages') {
dirPath = path.join(this.siteDir, 'input', 'media', 'tags', 'defaults');
responsiveDirPath = path.join(this.siteDir, 'input', 'media', 'tags', 'defaults', 'responsive');
} else if (this.id === 'defaults' && this.imageType === 'authorImages') {
dirPath = path.join(this.siteDir, 'input', 'media', 'authors', 'defaults');
responsiveDirPath = path.join(this.siteDir, 'input', 'media', 'authors', 'defaults', 'responsive');
} else if (this.imageType === 'pluginImages') {
dirPath = path.join(this.siteDir, 'input', 'media', 'plugins', this.pluginDir);
} else if (this.id === 'website') {
dirPath = path.join(this.siteDir, 'input', 'media', 'website');
Expand Down
2 changes: 1 addition & 1 deletion app/back-end/modules/render-html/helpers/diffCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class DiffCopy {
postIDs = postIDs.map(id => (id).toString());

for (let i = 0; i < allPostFolders.length; i++) {
if (allPostFolders[i] === '.' || allPostFolders[i] === '..') {
if (allPostFolders[i] === '.' || allPostFolders[i] === '..' || allPostFolders[i] === 'defaults') {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion app/back-end/modules/render-html/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Files {
static async copyMediaFiles (inputDir, outputDir, postIDs) {
let basePathInput = path.join(inputDir, 'media');
let basePathOutput = path.join(outputDir, 'media');
let dirs = ['website', 'files', 'tags', 'authors'];
let dirs = ['website', 'files', 'tags', 'authors', 'posts/defaults'];

if (postIDs[0] === 0) {
postIDs[0] = 'temp';
Expand Down
116 changes: 89 additions & 27 deletions app/back-end/modules/render-html/helpers/view-settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const sizeOf = require('image-size');

class ViewSettings {
static override(viewSettings, defaultViewConfig) {
static override(viewSettings, defaultViewConfig, itemData, rendererInstance) {
let outputConfig = {};

// Generate default settings structure
Expand Down Expand Up @@ -31,6 +33,36 @@ class ViewSettings {
outputConfig[defaultViewFields[i]] = defaultField;
}
}
} else if (typeof field !== 'undefined' && field.type === 'image') {
let dirName = 'posts';

if (itemData.type === 'tag') {
dirName = 'tags';
} else if (itemData.type === 'author') {
dirName = 'authors';
}

let imageDimensions = false;
let imagePath = '/media/' + dirName + '/defaults/' + defaultField;

if (defaultField) {
try {
imageDimensions = sizeOf(rendererInstance.inputDir + imagePath);
} catch(e) {
imageDimensions = {
height: false,
width: false
};
}

outputConfig[defaultViewFields[i]] = {
url: rendererInstance.siteConfig.domain + imagePath,
width: imageDimensions.width,
height: imageDimensions.height
};
} else {
outputConfig[defaultViewFields[i]] = false;
}
} else {
if (defaultField === '0') {
defaultField = 0;
Expand All @@ -50,34 +82,64 @@ class ViewSettings {
for(let i = 0; i < viewFields.length; i++) {
let field = viewSettings[viewFields[i]];

if(typeof field !== 'undefined' && field.value) {
if(field.value !== "") {
outputConfig[viewFields[i]] = field.value;
if (field.type === 'image') {
if (field.value) {
let dirName = 'posts';

if (itemData.type === 'tag') {
dirName = 'tags';
} else if (itemData.type === 'author') {
dirName = 'authors';
}

let imageDimensions = false;
let imagePath = '/media/' + dirName + '/' + itemData.id + '/' + field.value;

try {
imageDimensions = sizeOf(rendererInstance.inputDir + imagePath);
} catch(e) {
imageDimensions = {
height: false,
width: false
};
}

outputConfig[defaultViewFields[i]] = {
url: rendererInstance.siteConfig.domain + imagePath,
width: imageDimensions.width,
height: imageDimensions.height
};
}
} else {
if(typeof field !== 'undefined' && field.value) {
if(field.value !== "") {
outputConfig[viewFields[i]] = field.value;
}
} else if(typeof field === 'string' && field !== "") {
outputConfig[viewFields[i]] = field;
}
} else if(typeof field === 'string' && field !== "") {
outputConfig[viewFields[i]] = field;
}

if((field.type && field.type === 'select') || !field.type) {
if(
field === 0 ||
field === '0' ||
field.value === 0 ||
field.value === '0'
) {
outputConfig[viewFields[i]] = false;
} else if(
field === 1 ||
field === '1' ||
field.value === 1 ||
field.value === '1'
) {
outputConfig[viewFields[i]] = true;
} else {
if (typeof field.value !== 'undefined' && field.value !== '') {
outputConfig[viewFields[i]] = JSON.stringify(field.value).replace(/"/g, '');
} else if (typeof field !== 'object' && field !== '') {
outputConfig[viewFields[i]] = JSON.stringify(field).replace(/"/g, '');
if((field.type && field.type === 'select') || !field.type) {
if(
field === 0 ||
field === '0' ||
field.value === 0 ||
field.value === '0'
) {
outputConfig[viewFields[i]] = false;
} else if(
field === 1 ||
field === '1' ||
field.value === 1 ||
field.value === '1'
) {
outputConfig[viewFields[i]] = true;
} else {
if (typeof field.value !== 'undefined' && field.value !== '') {
outputConfig[viewFields[i]] = JSON.stringify(field.value).replace(/"/g, '');
} else if (typeof field !== 'object' && field !== '') {
outputConfig[viewFields[i]] = JSON.stringify(field).replace(/"/g, '');
}
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions app/back-end/modules/render-html/renderer-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class RendererCache {
let tagViewConfigObject = JSON.parse(JSON.stringify(this.themeConfig.tagConfig));

tags = tags.map(tag => {
let tagViewConfig = this.getViewSettings(tagViewConfigObject, tag);
let tagViewConfig = this.getViewSettings(tagViewConfigObject, tag, {
type: 'tag',
id: tag.id
});
let newTag = new Tag(tag, this.renderer, mainTagIDs);
newTag.setTagViewConfig(tagViewConfig);
return newTag;
Expand Down Expand Up @@ -186,7 +189,10 @@ class RendererCache {
let authorViewConfigObject = JSON.parse(JSON.stringify(this.themeConfig.authorConfig));

authors = authors.map(author => {
let authorViewConfig = this.getViewSettings(authorViewConfigObject, author);
let authorViewConfig = this.getViewSettings(authorViewConfigObject, author, {
type: 'author',
id: author.id
});
let newAuthor = new Author(author, this.renderer);
newAuthor.setAuthorViewConfig(authorViewConfig);
return newAuthor;
Expand Down Expand Up @@ -397,7 +403,10 @@ class RendererCache {
postViewSettings = JSON.parse(postViewData.value);
}

return ViewSettingsHelper.override(postViewSettings, defaultPostViewConfig);
return ViewSettingsHelper.override(postViewSettings, defaultPostViewConfig, {
type: 'post',
id: postID
}, this.renderer);
}

/**
Expand All @@ -408,7 +417,7 @@ class RendererCache {
*
* @returns {object}
*/
getViewSettings(defaultViewConfig, itemData) {
getViewSettings(defaultViewConfig, itemData, itemConfig) {
let viewSettings = {};

if (itemData && itemData.additional_data) {
Expand All @@ -423,7 +432,7 @@ class RendererCache {
}
}

return ViewSettingsHelper.override(viewSettings, defaultViewConfig);
return ViewSettingsHelper.override(viewSettings, defaultViewConfig, itemConfig, this.renderer);
}
}

Expand Down
13 changes: 8 additions & 5 deletions app/back-end/modules/render-html/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class Renderer {

await FilesHelper.copyAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyDynamicAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, [this.itemID]);
await FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, [this.itemID]);
FilesHelper.copyPluginFiles(this.inputDir, this.outputDir, this.pluginsDir);

this.triggerEvent('afterRender');
Expand All @@ -313,7 +313,7 @@ class Renderer {
let postIDs = Object.keys(this.cachedItems.posts);
await FilesHelper.copyAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyDynamicAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDs);
await FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDs);
FilesHelper.copyPluginFiles(this.inputDir, this.outputDir, this.pluginsDir);

this.triggerEvent('afterRender');
Expand All @@ -339,7 +339,7 @@ class Renderer {

await FilesHelper.copyAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyDynamicAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDsToRender);
await FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDsToRender);
FilesHelper.copyPluginFiles(this.inputDir, this.outputDir, this.pluginsDir);

this.triggerEvent('afterRender');
Expand All @@ -365,7 +365,7 @@ class Renderer {

await FilesHelper.copyAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyDynamicAssetsFiles(this.themeDir, this.outputDir, this.themeConfig);
FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDsToRender);
await FilesHelper.copyMediaFiles(this.inputDir, this.outputDir, postIDsToRender);
FilesHelper.copyPluginFiles(this.inputDir, this.outputDir, this.pluginsDir);

this.triggerEvent('afterRender');
Expand Down Expand Up @@ -973,7 +973,10 @@ class Renderer {
}
}

return ViewSettingsHelper.override(postViewSettings, defaultPostViewConfig);
return ViewSettingsHelper.override(postViewSettings, defaultPostViewConfig, {
type: 'post',
id: postID
});
}

/*
Expand Down
14 changes: 12 additions & 2 deletions app/back-end/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,12 @@ class Post extends Model {
postDir = 'temp';
}

let imagesInPostViewSettings = [];

if (this.postViewSettings) {
imagesInPostViewSettings = Object.values(this.postViewSettings).filter(item => item.type === "image").map(item => item.value);
}

// Iterate through images
for (let i in images) {
let imagePath = images[i];
Expand All @@ -641,10 +647,14 @@ class Post extends Model {
continue;
}

// Remove files which does not exist in the post text
// Remove files which does not exist in the post text, as featured image and postViewSettings
if(
(cancelEvent && postDir === 'temp') ||
(this.text.indexOf(imagePath) === -1 && featuredImage !== imagePath)
(
this.text.indexOf(imagePath) === -1 &&
imagesInPostViewSettings.indexOf(imagePath) === -1 &&
featuredImage !== imagePath
)
) {
try {
fs.unlinkSync(fullPath);
Expand Down
15 changes: 14 additions & 1 deletion app/back-end/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ class Tag extends Model {
tagDir = 'temp';
}

let imagesInTagViewSettings = [];

if (this.additionalData && this.additionalData.viewConfig) {
imagesInTagViewSettings = Object.values(this.additionalData.viewConfig).filter(item => item.type === "image").map(item => item.value);
}

// Iterate through images
for (let i in images) {
let imagePath = images[i];
Expand All @@ -303,7 +309,14 @@ class Tag extends Model {
continue;
}

if ((cancelEvent && tagDir === 'temp') || featuredImage !== imagePath) {
// Remove files which does not exist as featured image and authorViewSettings
if(
(cancelEvent && tagDir === 'temp') ||
(
imagesInTagViewSettings.indexOf(imagePath) === -1 &&
featuredImage !== imagePath
)
) {
try {
fs.unlinkSync(fullPath);
} catch(e) {
Expand Down
Loading

0 comments on commit f0854b6

Please sign in to comment.