fix: backup restore formatting

This commit is contained in:
Eli Bosley
2025-01-28 15:01:01 -05:00
parent 8d905974be
commit 0cfdd5a61b

View File

@@ -1,6 +1,7 @@
import { BadRequestException, ExecutionContext, Logger, UnauthorizedException } from '@nestjs/common'; import { BadRequestException, ExecutionContext, Logger, UnauthorizedException } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql'; import { GqlExecutionContext } from '@nestjs/graphql';
import { copyFile, unlink } from 'node:fs/promises'; import { access, constants, copyFile, unlink } from 'node:fs/promises';
import { dirname } from 'node:path';
import strftime from 'strftime'; import strftime from 'strftime';
@@ -245,9 +246,6 @@ export function handleAuthError(
throw new UnauthorizedException(`${operation}: ${errorMessage}`); throw new UnauthorizedException(`${operation}: ${errorMessage}`);
} }
import { access, constants } from 'node:fs/promises';
import { dirname } from 'node:path';
/** /**
* Helper method to allow backing up a single file to a .bak file. * Helper method to allow backing up a single file to a .bak file.
* @param path the file to backup, creates a .bak file in the same directory * @param path the file to backup, creates a .bak file in the same directory
@@ -259,13 +257,13 @@ export const backupFile = async (path: string, throwOnMissing = true): Promise<v
if (!path) { if (!path) {
throw new Error('File path cannot be empty'); throw new Error('File path cannot be empty');
} }
// Check if source file exists and is readable // Check if source file exists and is readable
await access(path, constants.R_OK); await access(path, constants.R_OK);
// Check if backup directory is writable // Check if backup directory is writable
await access(dirname(path), constants.W_OK); await access(dirname(path), constants.W_OK);
const backupPath = path + '.bak'; const backupPath = path + '.bak';
await copyFile(path, backupPath); await copyFile(path, backupPath);
} catch (err) { } catch (err) {
@@ -275,8 +273,8 @@ export const backupFile = async (path: string, throwOnMissing = true): Promise<v
error.code === 'ENOENT' error.code === 'ENOENT'
? `File does not exist: ${path}` ? `File does not exist: ${path}`
: error.code === 'EACCES' : error.code === 'EACCES'
? `Permission denied: ${path}` ? `Permission denied: ${path}`
: `Failed to backup file: ${error.message}` : `Failed to backup file: ${error.message}`
); );
} }
} }
@@ -293,15 +291,15 @@ export const restoreFile = async (path: string, throwOnMissing = true): Promise<
if (!path) { if (!path) {
throw new Error('File path cannot be empty'); throw new Error('File path cannot be empty');
} }
const backupPath = path + '.bak'; const backupPath = path + '.bak';
try { try {
// Check if backup file exists and is readable // Check if backup file exists and is readable
await access(backupPath, constants.R_OK); await access(backupPath, constants.R_OK);
// Check if target directory is writable // Check if target directory is writable
await access(dirname(path), constants.W_OK); await access(dirname(path), constants.W_OK);
await copyFile(backupPath, path); await copyFile(backupPath, path);
await unlink(backupPath); await unlink(backupPath);
return true; return true;
@@ -312,8 +310,8 @@ export const restoreFile = async (path: string, throwOnMissing = true): Promise<
error.code === 'ENOENT' error.code === 'ENOENT'
? `Backup file does not exist: ${backupPath}` ? `Backup file does not exist: ${backupPath}`
: error.code === 'EACCES' : error.code === 'EACCES'
? `Permission denied: ${path}` ? `Permission denied: ${path}`
: `Failed to restore file: ${error.message}` : `Failed to restore file: ${error.message}`
); );
} }
return false; return false;