Skip to content
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

fix saving file extension #29

Merged
merged 2 commits into from
Oct 14, 2017
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
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