Skip to content

Commit b6df0f4

Browse files
authored
Merge pull request #29 from meowtec/fix/ext
fix saving file extension
2 parents 5e96032 + f83004d commit b6df0f4

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

modules/__tests__/common/file-utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
saveFilesTmp,
1111
unoccupiedFile,
1212
flattenFiles,
13+
reext,
1314
} from '../../common/file-utils'
1415
import { IImageFile, SupportedExt } from '../../common/constants'
1516

@@ -103,3 +104,10 @@ test('flattenFiles', async () => {
103104
'../_tools/image-diff.ts',
104105
].map(relPath))
105106
})
107+
108+
test('reext', () => {
109+
expect(reext('a/b/c.png', SupportedExt.jpg)).toBe('a/b/c.jpg')
110+
expect(reext('a/b/c.jpg', SupportedExt.webp)).toBe('a/b/c.webp')
111+
expect(reext('a/b/c.old', SupportedExt.webp)).toBe('a/b/c.old.webp')
112+
expect(reext('a/b/c.PNG', SupportedExt.png)).toBe('a/b/c.PNG')
113+
})

modules/backend/controller.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Controller {
109109
}
110110

111111
handleIpcFileSave = (event: Electron.IpcMessageEvent, images: IImageFile[], type: SaveType) => {
112-
const save = async (dirname?: string) => {
112+
const saveToDir = async (dirname?: string) => {
113113
await saveFiles(images, type, dirname)
114114
event.sender.send(IpcChannel.SAVED)
115115
}
@@ -121,19 +121,25 @@ class Controller {
121121
}, async filePaths => {
122122
if (!filePaths || !filePaths.length) return
123123
const dirpath = filePaths[0]
124-
await save(dirpath)
124+
await saveToDir(dirpath)
125125
shell.openItem(dirpath)
126126
})
127127
} else if (type === SaveType.SAVE_AS) {
128+
const image = images[0]
129+
128130
dialog.showSaveDialog({
129131
title: 'Save files',
130-
defaultPath: images[0].originalName,
132+
defaultPath: fu.reext(image.originalName, image.ext),
133+
filters: [{
134+
name: 'Images',
135+
extensions: [image.ext],
136+
}],
131137
}, async filePath => {
132138
await saveFile(images[0], filePath)
133139
event.sender.send(IpcChannel.SAVED)
134140
})
135141
} else {
136-
save()
142+
saveToDir()
137143
}
138144
}
139145

modules/backend/save.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export async function saveFiles(images: IImageFile[], type: SaveType, dirname?:
1010
for (const image of images) {
1111
if (!image) continue
1212

13-
let savePath: string = image.originalName
13+
let savePath: string = fu.reext(image.originalName, image.ext)
1414

1515
switch (type) {
1616
case SaveType.OVER:

modules/common/file-utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,25 @@ export const flattenFiles = async (filePaths: string[]) => {
109109

110110
return list
111111
}
112+
113+
/**
114+
* 'path/to/image.png' + jpg -> 'path/to/image.jpg'
115+
* @param filename - 'path/to/image.png'
116+
* @param ext - jpg
117+
*/
118+
export const reext = (filename: string, ext: SupportedExt) => {
119+
return filename.replace(/(?:\.(\w+))?$/i, ($0, $1: string) => {
120+
$1 = $1.toLowerCase()
121+
122+
// make sure `x.PNG` not be transformed to `x.png`
123+
if ($1 === ext) {
124+
return $0
125+
}
126+
127+
if ($1 in SupportedExt) {
128+
return '.' + ext
129+
} else {
130+
return $0 + '.' + ext
131+
}
132+
})
133+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Imagine",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "PNG/JPEG optimization app",
55
"homepage": "https://github.com/meowtec/Imagine",
66
"repository": {

0 commit comments

Comments
 (0)