From b28f57e6f26619545063711b34329eb116eb60b9 Mon Sep 17 00:00:00 2001 From: szTheory Date: Fri, 10 Jul 2020 19:53:32 -0400 Subject: [PATCH] Fix Windows UTF-8 filename bug (tested) --- src/common/args_file.ts | 14 ----------- src/common/temp_file.ts | 33 -------------------------- src/renderer/add_files.ts | 49 ++++++++++++++------------------------- 3 files changed, 17 insertions(+), 79 deletions(-) delete mode 100644 src/common/args_file.ts delete mode 100644 src/common/temp_file.ts diff --git a/src/common/args_file.ts b/src/common/args_file.ts deleted file mode 100644 index 3c2efd6..0000000 --- a/src/common/args_file.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { withTempFile } from "./temp_file"; -import { promises as fs } from "fs"; - -export async function withArgsTempFile( - args: string[], - fn: (argsTempFilePath: string) => Promise -): Promise { - return withTempFile(tempFilePath => { - const buffer = args.join("\n"); - - fs.writeFile(tempFilePath, buffer); - return fn(tempFilePath); - }); -} diff --git a/src/common/temp_file.ts b/src/common/temp_file.ts deleted file mode 100644 index 1af1fe7..0000000 --- a/src/common/temp_file.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { promises as fs } from "fs"; -import os from "os"; -import path from "path"; - -// wrap a function around a valid tempfile path that -// will be cleaned up when the function teminates -export async function withTempFile( - fn: (tempFilePath: string) => any -): Promise { - return withTempDir(dir => { - const tempFilePath = path.join(dir, "file"); - - return fn(tempFilePath); - }); -} - -// wrap a function around a tempdir that is automatically -// created for the duration of the function and cleaned up -// after when the function terminates -async function withTempDir(fn: (tempDirPath: string) => any): Promise { - // create tempdir - const tempDirPath = await fs.mkdtemp( - (await fs.realpath(os.tmpdir())) + path.sep - ); - - try { - // call wrapper function - return await fn(tempDirPath); - } finally { - // cleanup - fs.rmdir(tempDirPath, { recursive: true }); - } -} diff --git a/src/renderer/add_files.ts b/src/renderer/add_files.ts index 9c652ef..f99360a 100644 --- a/src/renderer/add_files.ts +++ b/src/renderer/add_files.ts @@ -5,7 +5,6 @@ import { } from "./table"; import exiftool, { ExiftoolProcess } from "node-exiftool"; import { exiftoolBinPath } from "../common/binaries"; -import { withArgsTempFile } from "../common/args_file"; export async function addFiles({ filePaths }: { filePaths: string[] }) { for (const filePath of filePaths) { @@ -136,13 +135,9 @@ async function removeExif({ .open() // .then((pid) => console.log('Started exiftool process %s', pid)) .then(() => { - const args = ["-charset filename=UTF8", "overwrite_original"]; + const args = ["charset filename=UTF8", "overwrite_original"]; - return withArgsTempFile(args, argsTempFilePath => { - return ep.writeMetadata(filePath, { all: "" }, [ - `-@ ${argsTempFilePath}` - ]); - }); + return ep.writeMetadata(filePath, { all: "" }, args); }) .catch(console.error); @@ -162,31 +157,21 @@ async function getExif({ .open() // .then((pid) => console.log('Started exiftool process %s', pid)) .then(() => { - const args = [ - "-charset filename=UTF8", - "-File:all", - "-ExifToolVersion", - "-x FileSize", - "-x SourceFile" - ]; - - return withArgsTempFile(args, argsTempFilePath => { - return exiftoolProcess - .readMetadata(filePath, [`-@ ${argsTempFilePath}`]) - .then( - exifData => { - if (exifData.data === null) { - return {}; - } - - const hash = exifData.data[0]; - return cleanExifData(hash); - }, - err => { - console.error(err); - } - ); - }); + const args = ["charset filename=UTF8", "-File:all", "-ExifToolVersion"]; + + return exiftoolProcess.readMetadata(filePath, args).then( + exifData => { + if (exifData.data === null) { + return {}; + } + + const hash = exifData.data[0]; + return cleanExifData(hash); + }, + err => { + console.error(err); + } + ); }) .catch(console.error);