Skip to content

fix: resize image throws and error if no definition matched #5088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/services/assets-generation/assets-generation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export class AssetsGenerationService implements IAssetsGenerationService {
const outputPath = assetItem.path;
const width = assetItem.width * scale;
const height = assetItem.height * scale;

if (!width || !height) {
this.$logger.warn(`Image ${assetItem.filename} is skipped as its width and height are invalid.`);
continue;
}

let image: Jimp;
switch (operation) {
case Operations.OverlayWith:
Expand Down
28 changes: 18 additions & 10 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,36 @@ export class ProjectDataService implements IProjectDataService {
private async getIOSAssetSubGroup(dirPath: string): Promise<IAssetSubGroup> {
const pathToContentJson = path.join(dirPath, AssetConstants.iOSResourcesFileName);
const content = this.$fs.exists(pathToContentJson) && <IAssetSubGroup>this.$fs.readJson(pathToContentJson) || { images: [] };
const finalContent: IAssetSubGroup = { images: [] };

const imageDefinitions = this.getImageDefinitions().ios;

_.each(content && content.images, image => {
let foundMatchingDefinition = false;
// In some cases the image may not be available, it will just be described.
// When this happens, the filename will be empty.
// So we'll keep the path empty as well.
if (image.filename) {
image.path = path.join(dirPath, image.filename);
}

if (image.size) {
// size is basically <width>x<height>
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
if (width && height) {
image.width = +width;
image.height = +height;
}
}

// Find the image size based on the hardcoded values in the image-definitions.json
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
const assetItem = _.find(assetSubGroup, assetElement =>
assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)
);

if (image.size) {
// size is basically <width>x<height>
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
if (width && height) {
image.width = +width;
image.height = +height;
}
}

if (assetItem) {
foundMatchingDefinition = true;
if (!image.width || !image.height) {
image.width = assetItem.width;
image.height = assetItem.height;
Expand All @@ -273,13 +276,18 @@ export class ProjectDataService implements IProjectDataService {
image.overlayImageScale = image.overlayImageScale || assetItem.overlayImageScale;
image.scale = image.scale || assetItem.scale;
image.rgba = assetItem.rgba;
finalContent.images.push(image);
// break each
return false;
}
});

if (!foundMatchingDefinition && image.filename) {
this.$logger.warn(`Didn't find a matching image definition for file ${path.join(path.basename(dirPath), image.filename)}. This file will be skipped from reources generation.`);
}
});

return content;
return finalContent;
}

private getAndroidAssetSubGroup(assetItems: IAssetItem[], realPaths: string[]): IAssetSubGroup {
Expand Down