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

Refactor: 上書き防止処理を関数化し共通化 #1478

Merged
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
40 changes: 15 additions & 25 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ function parseTextFile(
return audioItems;
}

async function changeFileTailToNonExistent(filePath: string) {
const EXTENSION = "wav";
let tail = 1;
const name = filePath.slice(0, filePath.length - 1 - EXTENSION.length);
while (await window.electron.checkFileExists(filePath)) {
filePath = `${name}[${tail}].${EXTENSION}`;
tail += 1;
}
return filePath;
}

export async function writeTextFile(obj: {
filePath: string;
text: string;
Expand Down Expand Up @@ -1336,12 +1347,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

let blob = await dispatch("GET_AUDIO_CACHE", { audioKey });
Expand Down Expand Up @@ -1489,12 +1495,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

const encodedBlobs: string[] = [];
Expand Down Expand Up @@ -1613,7 +1614,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
CONNECT_AND_EXPORT_TEXT: {
action: createUILockAction(
async (
{ state, dispatch, getters },
{ state, getters },
{ filePath }: { filePath?: string }
): Promise<SaveResultObject> => {
const defaultFileName = buildProjectFileName(state, "txt");
Expand All @@ -1634,12 +1635,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

const characters = new Map<string, string>();
Expand Down Expand Up @@ -1864,12 +1860,6 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}
},
},

CHECK_FILE_EXISTS: {
action(_, { file }: { file: string }) {
return window.electron.checkFileExists(file);
},
},
});

export const audioCommandStoreState: AudioCommandStoreState = {};
Expand Down
4 changes: 0 additions & 4 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,6 @@ export type AudioStoreTypes = {
STOP_CONTINUOUSLY_AUDIO: {
action(): void;
};

CHECK_FILE_EXISTS: {
action(payload: { file: string }): Promise<boolean>;
};
};

/*
Expand Down