mirror of
https://github.com/gabehf/Tdarr_Plugins.git
synced 2026-03-15 02:05:54 -07:00
Use async fileExists
This commit is contained in:
parent
99160eadb3
commit
28192d7723
5 changed files with 21 additions and 15 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import fs from 'fs';
|
import {
|
||||||
import { getContainer, getFileAbosluteDir, getFileName } from '../../../../FlowHelpers/1.0.0/fileUtils';
|
fileExists, getContainer, getFileAbosluteDir, getFileName,
|
||||||
|
} from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||||
import {
|
import {
|
||||||
IpluginDetails,
|
IpluginDetails,
|
||||||
IpluginInputArgs,
|
IpluginInputArgs,
|
||||||
|
|
@ -57,7 +58,7 @@ const details = (): IpluginDetails => ({
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
|
const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
const lib = require('../../../../../methods/lib')();
|
const lib = require('../../../../../methods/lib')();
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
||||||
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
||||||
|
|
@ -71,9 +72,9 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
|
||||||
fileToCheck = fileToCheck.replace(/\${container}/g, getContainer(args.inputFileObj._id));
|
fileToCheck = fileToCheck.replace(/\${container}/g, getContainer(args.inputFileObj._id));
|
||||||
fileToCheck = `${directory}/${fileToCheck}`;
|
fileToCheck = `${directory}/${fileToCheck}`;
|
||||||
|
|
||||||
let fileExists = false;
|
let fileDoesExist = false;
|
||||||
if (fs.existsSync(fileToCheck)) {
|
if (await fileExists(fileToCheck)) {
|
||||||
fileExists = true;
|
fileDoesExist = true;
|
||||||
args.jobLog(`File exists: ${fileToCheck}`);
|
args.jobLog(`File exists: ${fileToCheck}`);
|
||||||
} else {
|
} else {
|
||||||
args.jobLog(`File does not exist: ${fileToCheck}`);
|
args.jobLog(`File does not exist: ${fileToCheck}`);
|
||||||
|
|
@ -81,7 +82,7 @@ const plugin = (args: IpluginInputArgs): IpluginOutputArgs => {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
outputFileObj: args.inputFileObj,
|
outputFileObj: args.inputFileObj,
|
||||||
outputNumber: fileExists ? 1 : 2,
|
outputNumber: fileDoesExist ? 1 : 2,
|
||||||
variables: args.variables,
|
variables: args.variables,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy';
|
import fileMoveOrCopy from '../../../../FlowHelpers/1.0.0/fileMoveOrCopy';
|
||||||
import {
|
import {
|
||||||
|
fileExists,
|
||||||
getContainer, getFileAbosluteDir, getFileName,
|
getContainer, getFileAbosluteDir, getFileName,
|
||||||
} from '../../../../FlowHelpers/1.0.0/fileUtils';
|
} from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||||
import {
|
import {
|
||||||
|
|
@ -76,7 +77,7 @@ const plugin = async (args: IpluginInputArgs): Promise<IpluginOutputArgs> => {
|
||||||
|
|
||||||
// delete original file
|
// delete original file
|
||||||
if (
|
if (
|
||||||
fs.existsSync(args.originalLibraryFile._id)
|
await fileExists(args.originalLibraryFile._id)
|
||||||
&& args.originalLibraryFile._id !== currentPath
|
&& args.originalLibraryFile._id !== currentPath
|
||||||
) {
|
) {
|
||||||
args.jobLog(`Deleting original file:${args.originalLibraryFile._id}`);
|
args.jobLog(`Deleting original file:${args.originalLibraryFile._id}`);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import {
|
||||||
IpluginInputArgs,
|
IpluginInputArgs,
|
||||||
IpluginOutputArgs,
|
IpluginOutputArgs,
|
||||||
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
|
} from '../../../../FlowHelpers/1.0.0/interfaces/interfaces';
|
||||||
|
import { fileExists } from '../../../../FlowHelpers/1.0.0/fileUtils';
|
||||||
|
|
||||||
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
|
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */
|
||||||
const details = ():IpluginDetails => ({
|
const details = ():IpluginDetails => ({
|
||||||
|
|
@ -47,7 +48,7 @@ const details = ():IpluginDetails => ({
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const plugin = (args:IpluginInputArgs):IpluginOutputArgs => {
|
const plugin = async (args:IpluginInputArgs):Promise<IpluginOutputArgs> => {
|
||||||
const lib = require('../../../../../methods/lib')();
|
const lib = require('../../../../../methods/lib')();
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-param-reassign
|
||||||
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
args.inputs = lib.loadDefaultValues(args.inputs, details);
|
||||||
|
|
@ -55,7 +56,7 @@ const plugin = (args:IpluginInputArgs):IpluginOutputArgs => {
|
||||||
const oldFile = args.inputFileObj._id;
|
const oldFile = args.inputFileObj._id;
|
||||||
const newFile = `${args.inputFileObj._id}.tmp`;
|
const newFile = `${args.inputFileObj._id}.tmp`;
|
||||||
|
|
||||||
if (fs.existsSync(newFile)) {
|
if (await fileExists(newFile)) {
|
||||||
fs.unlinkSync(newFile);
|
fs.unlinkSync(newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import {
|
||||||
} from './cliParsers';
|
} from './cliParsers';
|
||||||
import { Ilog, IupdateWorker } from './interfaces/interfaces';
|
import { Ilog, IupdateWorker } from './interfaces/interfaces';
|
||||||
import { IFileObject, Istreams } from './interfaces/synced/IFileObject';
|
import { IFileObject, Istreams } from './interfaces/synced/IFileObject';
|
||||||
|
import { fileExists } from './fileUtils';
|
||||||
|
|
||||||
const fancyTimeFormat = (time: number) => {
|
const fancyTimeFormat = (time: number) => {
|
||||||
// Hours, minutes and seconds
|
// Hours, minutes and seconds
|
||||||
|
|
@ -92,7 +93,7 @@ class CLI {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateETA = (perc: number): void => {
|
updateETA = async (perc: number): Promise<void> => {
|
||||||
if (perc > 0) {
|
if (perc > 0) {
|
||||||
if (this.lastProgCheck === 0) {
|
if (this.lastProgCheck === 0) {
|
||||||
this.lastProgCheck = new Date().getTime();
|
this.lastProgCheck = new Date().getTime();
|
||||||
|
|
@ -124,7 +125,7 @@ class CLI {
|
||||||
let outputFileSizeInGbytes;
|
let outputFileSizeInGbytes;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync(this.config.outputFilePath)) {
|
if (await fileExists(this.config.outputFilePath)) {
|
||||||
let singleFileSize = fs.statSync(this.config.outputFilePath);
|
let singleFileSize = fs.statSync(this.config.outputFilePath);
|
||||||
// @ts-expect-error type
|
// @ts-expect-error type
|
||||||
singleFileSize = singleFileSize.size;
|
singleFileSize = singleFileSize.size;
|
||||||
|
|
@ -180,7 +181,7 @@ class CLI {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (percentage > 0) {
|
if (percentage > 0) {
|
||||||
this.updateETA(percentage);
|
void this.updateETA(percentage);
|
||||||
this.config.updateWorker({
|
this.config.updateWorker({
|
||||||
percentage,
|
percentage,
|
||||||
});
|
});
|
||||||
|
|
@ -236,7 +237,7 @@ class CLI {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (percentage > 0) {
|
if (percentage > 0) {
|
||||||
this.updateETA(percentage);
|
void this.updateETA(percentage);
|
||||||
this.config.updateWorker({
|
this.config.updateWorker({
|
||||||
percentage,
|
percentage,
|
||||||
});
|
});
|
||||||
|
|
@ -246,7 +247,7 @@ class CLI {
|
||||||
str,
|
str,
|
||||||
});
|
});
|
||||||
if (percentage > 0) {
|
if (percentage > 0) {
|
||||||
this.updateETA(percentage);
|
void this.updateETA(percentage);
|
||||||
this.config.updateWorker({
|
this.config.updateWorker({
|
||||||
percentage,
|
percentage,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { promises as fsp } from 'fs';
|
import { promises as fsp } from 'fs';
|
||||||
import { IpluginInputArgs } from './interfaces/interfaces';
|
import { IpluginInputArgs } from './interfaces/interfaces';
|
||||||
|
|
||||||
|
export const fileExists = async (path:string): Promise<boolean> => !!(await fsp.stat(path).catch(() => false));
|
||||||
|
|
||||||
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];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue