From d5f99bf5208ab094a5d9f4d05590b0ceedad8772 Mon Sep 17 00:00:00 2001 From: Meowtec Date: Sat, 14 Oct 2017 21:52:55 +0800 Subject: [PATCH 1/2] fix: use correct file extension if save with another image type --- modules/__tests__/common/file-utils.test.ts | 8 ++++++++ modules/backend/controller.ts | 14 +++++++++---- modules/backend/save.ts | 2 +- modules/common/file-utils.ts | 22 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/modules/__tests__/common/file-utils.test.ts b/modules/__tests__/common/file-utils.test.ts index 053c0c0..897141f 100644 --- a/modules/__tests__/common/file-utils.test.ts +++ b/modules/__tests__/common/file-utils.test.ts @@ -10,6 +10,7 @@ import { saveFilesTmp, unoccupiedFile, flattenFiles, + reext, } from '../../common/file-utils' import { IImageFile, SupportedExt } from '../../common/constants' @@ -103,3 +104,10 @@ test('flattenFiles', async () => { '../_tools/image-diff.ts', ].map(relPath)) }) + +test('reext', () => { + expect(reext('a/b/c.png', SupportedExt.jpg)).toBe('a/b/c.jpg') + expect(reext('a/b/c.jpg', SupportedExt.webp)).toBe('a/b/c.webp') + expect(reext('a/b/c.old', SupportedExt.webp)).toBe('a/b/c.old.webp') + expect(reext('a/b/c.PNG', SupportedExt.png)).toBe('a/b/c.PNG') +}) diff --git a/modules/backend/controller.ts b/modules/backend/controller.ts index 23e7c40..c9ac10f 100644 --- a/modules/backend/controller.ts +++ b/modules/backend/controller.ts @@ -109,7 +109,7 @@ class Controller { } handleIpcFileSave = (event: Electron.IpcMessageEvent, images: IImageFile[], type: SaveType) => { - const save = async (dirname?: string) => { + const saveToDir = async (dirname?: string) => { await saveFiles(images, type, dirname) event.sender.send(IpcChannel.SAVED) } @@ -121,19 +121,25 @@ class Controller { }, async filePaths => { if (!filePaths || !filePaths.length) return const dirpath = filePaths[0] - await save(dirpath) + await saveToDir(dirpath) shell.openItem(dirpath) }) } else if (type === SaveType.SAVE_AS) { + const image = images[0] + dialog.showSaveDialog({ title: 'Save files', - defaultPath: images[0].originalName, + defaultPath: fu.reext(image.originalName, image.ext), + filters: [{ + name: 'Images', + extensions: [image.ext], + }], }, async filePath => { await saveFile(images[0], filePath) event.sender.send(IpcChannel.SAVED) }) } else { - save() + saveToDir() } } diff --git a/modules/backend/save.ts b/modules/backend/save.ts index fed3ee1..8870812 100644 --- a/modules/backend/save.ts +++ b/modules/backend/save.ts @@ -10,7 +10,7 @@ export async function saveFiles(images: IImageFile[], type: SaveType, dirname?: for (const image of images) { if (!image) continue - let savePath: string = image.originalName + let savePath: string = fu.reext(image.originalName, image.ext) switch (type) { case SaveType.OVER: diff --git a/modules/common/file-utils.ts b/modules/common/file-utils.ts index fe9809d..d9a5ad0 100644 --- a/modules/common/file-utils.ts +++ b/modules/common/file-utils.ts @@ -109,3 +109,25 @@ export const flattenFiles = async (filePaths: string[]) => { return list } + +/** + * 'path/to/image.png' + jpg -> 'path/to/image.jpg' + * @param filename - 'path/to/image.png' + * @param ext - jpg + */ +export const reext = (filename: string, ext: SupportedExt) => { + return filename.replace(/(?:\.(\w+))?$/i, ($0, $1: string) => { + $1 = $1.toLowerCase() + + // make sure `x.PNG` not be transformed to `x.png` + if ($1 === ext) { + return $0 + } + + if ($1 in SupportedExt) { + return '.' + ext + } else { + return $0 + '.' + ext + } + }) +} From f83004d3fd793aeae2dbdd0899d32bffc408deeb Mon Sep 17 00:00:00 2001 From: Meowtec Date: Sat, 14 Oct 2017 21:55:32 +0800 Subject: [PATCH 2/2] v0.3.2 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 01e6f41..a61fbcc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "Imagine", - "version": "0.3.0", + "version": "0.3.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f536788..dab25c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Imagine", - "version": "0.3.1", + "version": "0.3.2", "description": "PNG/JPEG optimization app", "homepage": "https://github.com/meowtec/Imagine", "repository": {