Add moveFileAndValidate

make-only-subtitle-default
HaveAGitGat 2 years ago
parent cf1b27c26d
commit d7c16474cf

@ -40,7 +40,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.plugin = exports.details = void 0; exports.plugin = exports.details = void 0;
var fs_1 = require("fs");
var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils"); var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils");
var normJoinPath_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/normJoinPath")); var normJoinPath_1 = __importDefault(require("../../../../FlowHelpers/1.0.0/normJoinPath"));
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ /* 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("Input path: ".concat(args.inputFileObj._id));
args.jobLog("Output path: ".concat(ouputFilePath)); args.jobLog("Output path: ".concat(ouputFilePath));
args.deps.fsextra.ensureDirSync(outputPath); 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: case 1:
_a.sent(); _a.sent();
return [2 /*return*/, { return [2 /*return*/, {

@ -37,6 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.plugin = exports.details = void 0; exports.plugin = exports.details = void 0;
var fileUtils_1 = require("../../../../FlowHelpers/1.0.0/fileUtils");
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ /* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
var details = function () { return ({ var details = function () { return ({
name: 'Replace Original File', 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)) { if (fs.existsSync(newPath)) {
fs.unlinkSync(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 // delete original file
if (fs.existsSync(args.originalLibraryFile._id)) { if (fs.existsSync(args.originalLibraryFile._id)) {
fs.unlinkSync(args.originalLibraryFile._id); fs.unlinkSync(args.originalLibraryFile._id);
} }
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 2000); })]; 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(); _a.sent();
fs.renameSync(newPathTmp, newPath);
return [2 /*return*/, { return [2 /*return*/, {
outputFileObj: { outputFileObj: {
_id: newPath, _id: newPath,

@ -1,6 +1,43 @@
"use strict"; "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 }); 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 getContainer = function (filePath) {
var parts = filePath.split('.'); var parts = filePath.split('.');
return parts[parts.length - 1]; return parts[parts.length - 1];
@ -24,3 +61,82 @@ var getSubStem = function (_a) {
return parts.join('/'); return parts.join('/');
}; };
exports.getSubStem = getSubStem; 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;

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

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

@ -1,3 +1,6 @@
import { promises as fs } from 'fs';
import { IpluginInputArgs } from './interfaces/interfaces';
export const getContainer = (filePath: string): string => { export const getContainer = (filePath: string): string => {
const parts = filePath.split('.'); const parts = filePath.split('.');
return parts[parts.length - 1]; return parts[parts.length - 1];
@ -26,3 +29,66 @@ export const getSubStem = ({
return parts.join('/'); 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);
}
}
};

@ -117,5 +117,9 @@ export interface IpluginInputArgs {
requireFromString: (pluginText: string, relativePath: string) => Record<string, unknown>, requireFromString: (pluginText: string, relativePath: string) => Record<string, unknown>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
upath: 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
}, },
} }

Loading…
Cancel
Save