Skip to content

Commit

Permalink
fix: correctly rename file after modification
Browse files Browse the repository at this point in the history
  • Loading branch information
markokajzer committed Nov 29, 2020
1 parent 7ea7496 commit ad3084b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/commands/manage/ModifyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,44 @@ export class ModifyCommand extends Command {
}
}

private modifyVolume({ currentFile, newFile }: FileInfo, ...params: string[]): Promise<void> {
private modifyVolume({ currentFile, tempFile }: FileInfo, ...params: string[]): Promise<void> {
const [value] = params;
const ffmpegCommand = ffmpeg(currentFile)
.audioFilters([{ filter: 'volume', options: value }])
.output(newFile);
.output(tempFile);

return new Promise((resolve, reject) =>
ffmpegCommand.on('end', resolve).on('error', reject).run()
);
}

private clipSound({ currentFile, newFile }: FileInfo, ...params: string[]): Promise<void> {
private clipSound({ currentFile, tempFile }: FileInfo, ...params: string[]): Promise<void> {
const [startTime, endTime] = params;

// NOTE: We checked params already, so start is definitely here
const start = getSecondsFromTime(startTime)!;
const end = getSecondsFromTime(endTime);

let ffmpegCommand = ffmpeg(currentFile).output(newFile).setStartTime(start);
let ffmpegCommand = ffmpeg(currentFile).output(tempFile).setStartTime(start);
if (end) ffmpegCommand = ffmpegCommand.setDuration(end - start);

return new Promise((resolve, reject) =>
ffmpegCommand.on('end', resolve).on('error', reject).run()
);
}

private replace({ currentFile, newFile }: FileInfo) {
return rename(currentFile, newFile);
private replace({ currentFile, tempFile }: FileInfo): Promise<void> {
return rename(tempFile, currentFile);
}

private getFileNameFor(sound: string): FileInfo {
const extension = getExtensionForSound(sound);
const currentFile = `./sounds/${sound}.${extension}`;

const timestamp = Date.now();
const newFile = `./sounds/${sound}-${timestamp}.${extension}`;
const tempFile = `./sounds/${sound}-${timestamp}.${extension}`;

return { currentFile, newFile };
return { currentFile, tempFile };
}

private handleError(message: Message, error: Error, { modifier, sound }: ErrorParams) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/manage/modify/FileInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface FileInfo {
currentFile: string;
newFile: string;
tempFile: string;
}

0 comments on commit ad3084b

Please sign in to comment.