Add moveFileAndValidate

This commit is contained in:
HaveAGitGat 2023-08-29 12:13:23 +01:00
parent cf1b27c26d
commit d7c16474cf
7 changed files with 237 additions and 18 deletions

View file

@ -1,5 +1,6 @@
import { promises as fs } from 'fs';
import { getContainer, getFileName, getSubStem } from '../../../../FlowHelpers/1.0.0/fileUtils';
import {
getContainer, getFileName, getSubStem, moveFileAndValidate,
} from '../../../../FlowHelpers/1.0.0/fileUtils';
import {
IpluginDetails,
IpluginInputArgs,
@ -97,7 +98,13 @@ const plugin = async (args:IpluginInputArgs):Promise<IpluginOutputArgs> => {
args.jobLog(`Output path: ${ouputFilePath}`);
args.deps.fsextra.ensureDirSync(outputPath);
await fs.rename(args.inputFileObj._id, ouputFilePath);
await moveFileAndValidate({
inputPath: args.inputFileObj._id,
outputPath: ouputFilePath,
args,
});
return {
outputFileObj: {

View file

@ -1,3 +1,4 @@
import { moveFileAndValidate } from '../../../../FlowHelpers/1.0.0/fileUtils';
import {
IpluginDetails,
IpluginInputArgs,
@ -72,7 +73,11 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
fs.unlinkSync(newPath);
}
fs.renameSync(currentPath, newPathTmp);
await moveFileAndValidate({
inputPath: currentPath,
outputPath: newPathTmp,
args,
});
// delete original file
if (fs.existsSync(args.originalLibraryFile._id)) {
@ -80,7 +85,12 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
}
await new Promise((resolve) => setTimeout(resolve, 2000));
fs.renameSync(newPathTmp, newPath);
await moveFileAndValidate({
inputPath: newPathTmp,
outputPath: newPath,
args,
});
return {
outputFileObj: {

View file

@ -1,9 +1,12 @@
export const getContainer = (filePath: string):string => {
import { promises as fs } from 'fs';
import { IpluginInputArgs } from './interfaces/interfaces';
export const getContainer = (filePath: string): string => {
const parts = filePath.split('.');
return parts[parts.length - 1];
};
export const getFileName = (filePath: string):string => {
export const getFileName = (filePath: string): string => {
const parts = filePath.split('/');
const fileNameAndContainer = parts[parts.length - 1];
const parts2 = fileNameAndContainer.split('.');
@ -11,7 +14,7 @@ export const getFileName = (filePath: string):string => {
return parts2.join('.');
};
export const getFfType = (codecType:string):string => (codecType === 'video' ? 'v' : 'a');
export const getFfType = (codecType: string): string => (codecType === 'video' ? 'v' : 'a');
export const getSubStem = ({
inputPathStem,
@ -19,10 +22,73 @@ export const getSubStem = ({
}: {
inputPathStem: string,
inputPath: string,
}):string => {
}): string => {
const subStem = inputPath.substring(inputPathStem.length);
const parts = subStem.split('/');
parts.pop();
return parts.join('/');
};
const getFileSize = async (file:string):Promise<number> => {
const stats = await fs.stat(file);
const { size } = stats;
return size;
};
export const moveFileAndValidate = async ({
inputPath,
outputPath,
args,
}: {
inputPath: string,
outputPath: string,
args: IpluginInputArgs
}):Promise<void> => {
const inputSize = await getFileSize(inputPath);
args.jobLog(`Attempt 1: Moving file from ${inputPath} to ${outputPath}`);
const res1 = await new Promise((resolve) => {
args.deps.gracefulfs.rename(inputPath, outputPath, (err: Error) => {
if (err) {
args.jobLog(`Failed to move file from ${inputPath} to ${outputPath}`);
args.jobLog(JSON.stringify(err));
resolve(false);
} else {
resolve(true);
}
});
});
let outputSize = 0;
try {
outputSize = await getFileSize(outputPath);
} catch (err) {
args.jobLog(JSON.stringify(err));
}
if (!res1 || inputSize !== outputSize) {
args.jobLog(`Attempt 1 failed: Moving file from ${inputPath} to ${outputPath}`);
args.jobLog(`Attempt 2: Moving file from ${inputPath} to ${outputPath}`);
const res2 = await new Promise((resolve) => {
args.deps.mvdir(inputPath, outputPath, { overwrite: true })
.then(() => {
resolve(true);
}).catch((err: Error) => {
args.jobLog(`Failed to move file from ${inputPath} to ${outputPath}`);
args.jobLog(JSON.stringify(err));
resolve(false);
});
});
outputSize = await getFileSize(outputPath);
if (!res2 || inputSize !== outputSize) {
const errMessage = `Failed to move file from ${inputPath} to ${outputPath}, check errors above`;
args.jobLog(errMessage);
throw new Error(errMessage);
}
}
};

View file

@ -114,8 +114,12 @@ export interface IpluginInputArgs {
importFresh(path: string): any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
axiosMiddleware: (endpoint: string, data: Record<string, unknown>) => Promise<any>,
requireFromString: (pluginText: string, relativePath:string) => Record<string, unknown>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
upath:any,
requireFromString: (pluginText: string, relativePath: string) => Record<string, unknown>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
upath: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gracefulfs: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mvdir: any
},
}