diff --git a/FlowPlugins/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.js index ed841d4..82b22e0 100644 --- a/FlowPlugins/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.js @@ -40,7 +40,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = exports.details = void 0; -var fs_1 = require("fs"); var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var normJoinPath_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/normJoinPath")); /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ @@ -126,7 +125,11 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function args.jobLog("Input path: ".concat(args.inputFileObj._id)); args.jobLog("Output path: ".concat(ouputFilePath)); args.deps.fsextra.ensureDirSync(outputPath); - return [4 /*yield*/, fs_1.promises.rename(args.inputFileObj._id, ouputFilePath)]; + return [4 /*yield*/, (0, fileUtils_1.moveFileAndValidate)({ + inputPath: args.inputFileObj._id, + outputPath: ouputFilePath, + args: args, + })]; case 1: _a.sent(); return [2 /*return*/, { diff --git a/FlowPlugins/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.js b/FlowPlugins/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.js index 0b210ac..ca1d8ca 100644 --- a/FlowPlugins/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.js +++ b/FlowPlugins/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.js @@ -37,6 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = exports.details = void 0; +var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ var details = function () { return ({ name: 'Replace Original File', @@ -99,15 +100,27 @@ var plugin = function (args) { return __awaiter(void 0, void 0, void 0, function if (fs.existsSync(newPath)) { fs.unlinkSync(newPath); } - fs.renameSync(currentPath, newPathTmp); + return [4 /*yield*/, (0, fileUtils_1.moveFileAndValidate)({ + inputPath: currentPath, + outputPath: newPathTmp, + args: args, + })]; + case 2: + _a.sent(); // delete original file if (fs.existsSync(args.originalLibraryFile._id)) { fs.unlinkSync(args.originalLibraryFile._id); } return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })]; - case 2: + case 3: + _a.sent(); + return [4 /*yield*/, (0, fileUtils_1.moveFileAndValidate)({ + inputPath: newPathTmp, + outputPath: newPath, + args: args, + })]; + case 4: _a.sent(); - fs.renameSync(newPathTmp, newPath); return [2 /*return*/, { outputFileObj: { _id: newPath, diff --git a/FlowPlugins/FlowHelpers/1.0.0/fileUtils.js b/FlowPlugins/FlowHelpers/1.0.0/fileUtils.js index d03516e..8f1a349 100644 --- a/FlowPlugins/FlowHelpers/1.0.0/fileUtils.js +++ b/FlowPlugins/FlowHelpers/1.0.0/fileUtils.js @@ -1,6 +1,43 @@ "use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (g && (g = 0, op[0] && (_ = 0)), _) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getSubStem = exports.getFfType = exports.getFileName = exports.getContainer = void 0; +exports.moveFileAndValidate = exports.getSubStem = exports.getFfType = exports.getFileName = exports.getContainer = void 0; +var fs_1 = require("fs"); var getContainer = function (filePath) { var parts = filePath.split('.'); return parts[parts.length - 1]; @@ -24,3 +61,82 @@ var getSubStem = function (_a) { return parts.join('/'); }; exports.getSubStem = getSubStem; +var getFileSize = function (file) { return __awaiter(void 0, void 0, void 0, function () { + var stats, size; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, fs_1.promises.stat(file)]; + case 1: + stats = _a.sent(); + size = stats.size; + return [2 /*return*/, size]; + } + }); +}); }; +var moveFileAndValidate = function (_a) { + var inputPath = _a.inputPath, outputPath = _a.outputPath, args = _a.args; + return __awaiter(void 0, void 0, void 0, function () { + var inputSize, res1, outputSize, err_1, res2, errMessage; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, getFileSize(inputPath)]; + case 1: + inputSize = _b.sent(); + args.jobLog("Attempt 1: Moving file from ".concat(inputPath, " to ").concat(outputPath)); + return [4 /*yield*/, new Promise(function (resolve) { + args.deps.gracefulfs.rename(inputPath, outputPath, function (err) { + if (err) { + args.jobLog("Failed to move file from ".concat(inputPath, " to ").concat(outputPath)); + args.jobLog(JSON.stringify(err)); + resolve(false); + } + else { + resolve(true); + } + }); + })]; + case 2: + res1 = _b.sent(); + outputSize = 0; + _b.label = 3; + case 3: + _b.trys.push([3, 5, , 6]); + return [4 /*yield*/, getFileSize(outputPath)]; + case 4: + outputSize = _b.sent(); + return [3 /*break*/, 6]; + case 5: + err_1 = _b.sent(); + args.jobLog(JSON.stringify(err_1)); + return [3 /*break*/, 6]; + case 6: + if (!(!res1 || inputSize !== outputSize)) return [3 /*break*/, 9]; + args.jobLog("Attempt 1 failed: Moving file from ".concat(inputPath, " to ").concat(outputPath)); + args.jobLog("Attempt 2: Moving file from ".concat(inputPath, " to ").concat(outputPath)); + return [4 /*yield*/, new Promise(function (resolve) { + args.deps.mvdir(inputPath, outputPath, { overwrite: true }) + .then(function () { + resolve(true); + }).catch(function (err) { + args.jobLog("Failed to move file from ".concat(inputPath, " to ").concat(outputPath)); + args.jobLog(JSON.stringify(err)); + resolve(false); + }); + })]; + case 7: + res2 = _b.sent(); + return [4 /*yield*/, getFileSize(outputPath)]; + case 8: + outputSize = _b.sent(); + if (!res2 || inputSize !== outputSize) { + errMessage = "Failed to move file from ".concat(inputPath, " to ").concat(outputPath, ", check errors above"); + args.jobLog(errMessage); + throw new Error(errMessage); + } + _b.label = 9; + case 9: return [2 /*return*/]; + } + }); + }); +}; +exports.moveFileAndValidate = moveFileAndValidate; diff --git a/FlowPluginsTs/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.ts index ce95604..cd276c6 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/file/moveToDirectory/2.0.0/index.ts @@ -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 => { 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: { diff --git a/FlowPluginsTs/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.ts b/FlowPluginsTs/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.ts index ce6e5c7..f38e7f6 100644 --- a/FlowPluginsTs/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.ts +++ b/FlowPluginsTs/CommunityFlowPlugins/file/replaceOriginalFile/1.0.0/index.ts @@ -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 => { 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 => { } await new Promise((resolve) => setTimeout(resolve, 2000)); - fs.renameSync(newPathTmp, newPath); + + await moveFileAndValidate({ + inputPath: newPathTmp, + outputPath: newPath, + args, + }); return { outputFileObj: { diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/fileUtils.ts b/FlowPluginsTs/FlowHelpers/1.0.0/fileUtils.ts index dc89478..d0d615c 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/fileUtils.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/fileUtils.ts @@ -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 => { + 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 => { + 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); + } + } +}; diff --git a/FlowPluginsTs/FlowHelpers/1.0.0/interfaces/interfaces.ts b/FlowPluginsTs/FlowHelpers/1.0.0/interfaces/interfaces.ts index 45d4fd5..29d4ebe 100644 --- a/FlowPluginsTs/FlowHelpers/1.0.0/interfaces/interfaces.ts +++ b/FlowPluginsTs/FlowHelpers/1.0.0/interfaces/interfaces.ts @@ -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) => Promise, - requireFromString: (pluginText: string, relativePath:string) => Record, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - upath:any, + requireFromString: (pluginText: string, relativePath: string) => Record, + // 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 }, }