Skip to content

Commit

Permalink
Merge pull request #29 from meowtec/fix/ext
Browse files Browse the repository at this point in the history
fix saving file extension
  • Loading branch information
meowtec authored Oct 14, 2017
2 parents 5e96032 + f83004d commit b6df0f4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
8 changes: 8 additions & 0 deletions modules/__tests__/common/file-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
saveFilesTmp,
unoccupiedFile,
flattenFiles,
reext,
} from '../../common/file-utils'
import { IImageFile, SupportedExt } from '../../common/constants'

Expand Down Expand Up @@ -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')
})
14 changes: 10 additions & 4 deletions modules/backend/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/backend/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions modules/common/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit b6df0f4

Please sign in to comment.