feat: Update .gitignore and refine TypeScript configuration; clean up commented code and enhance dataset validation; npm updates
Some checks failed
CI / container-job (push) Failing after 35s
Some checks failed
CI / container-job (push) Failing after 35s
- Updated .gitignore to include new patterns - Refined TypeScript configuration for better performance and readability - Cleaned up commented code in several files - Enhanced dataset validation logic - Updated npm dependencies to the latest versions
This commit is contained in:
parent
a5e0a36327
commit
8d47a58d29
22 changed files with 1315 additions and 4273 deletions
|
@ -3,37 +3,16 @@
|
|||
| Preloaded File - node ace make:preload rules/fileScan
|
||||
|--------------------------------------------------------------------------
|
||||
|*/
|
||||
|
||||
import { FieldContext } from '@vinejs/vine/types';
|
||||
import vine, { errors } from '@vinejs/vine';
|
||||
|
||||
// import { VineString } from '@vinejs/vine';
|
||||
import { VineMultipartFile, isBodyParserFile } from '#providers/vinejs_provider';
|
||||
import type { MultipartFile } from '@adonisjs/core/bodyparser';
|
||||
import ClamScan from 'clamscan';
|
||||
|
||||
/**
|
||||
* Options accepted by the unique rule
|
||||
*/
|
||||
// type Options = {
|
||||
// mainLanguageField: string;
|
||||
// typeField: string;
|
||||
// };
|
||||
type Options = {
|
||||
// size: string | number;
|
||||
// extnames: string[];
|
||||
removeInfected: boolean;
|
||||
// debugMode?: boolean;
|
||||
// scanRecursively?: boolean;
|
||||
host?: string;
|
||||
port?: number;
|
||||
// clamdscan: {
|
||||
// active: boolean;
|
||||
// host: string;
|
||||
// port: number;
|
||||
// multiscan: boolean;
|
||||
// };
|
||||
// preference: string;
|
||||
};
|
||||
|
||||
async function fileScan(file: VineMultipartFile | unknown, options: Options, field: FieldContext) {
|
||||
|
@ -44,42 +23,47 @@ async function fileScan(file: VineMultipartFile | unknown, options: Options, fie
|
|||
return;
|
||||
}
|
||||
const validatedFile = file as MultipartFile;
|
||||
|
||||
|
||||
try {
|
||||
await scanFileForViruses(validatedFile.tmpPath, options.host, options.port); //, 'gitea.lan', 3310);
|
||||
// await this.scanFileForViruses("/tmp/testfile.txt");
|
||||
await scanFileForViruses(validatedFile.tmpPath, options);
|
||||
} catch (error) {
|
||||
// If the file is infected or there's an error scanning the file, throw a validation exception
|
||||
// throw error;
|
||||
field.report(`Upload error. Code: ${error.code} message: ${error.messages.uploadError}`, 'fileScan', field);
|
||||
}
|
||||
}
|
||||
async function scanFileForViruses(filePath: string | undefined, host?: string, port?: number): Promise<void> {
|
||||
// const clamscan = await (new ClamScan().init());
|
||||
|
||||
async function scanFileForViruses(filePath: string | undefined, options: Options): Promise<void> {
|
||||
if (!filePath) {
|
||||
throw new errors.E_VALIDATION_ERROR({ uploadError: 'File path is undefined!' });
|
||||
}
|
||||
const opts: ClamScan.Options = {
|
||||
removeInfected: true, // If true, removes infected files
|
||||
debugMode: false, // Whether or not to log info/debug/error msgs to the console
|
||||
removeInfected: options.removeInfected, // If true, removes infected files
|
||||
debugMode: false, // If true, deep scan folders recursively
|
||||
scanRecursively: true, // If true, deep scan folders recursively
|
||||
clamdscan: {
|
||||
active: true, // If true, this module will consider using the clamdscan binary
|
||||
host,
|
||||
port,
|
||||
host: options.host,
|
||||
port: options.port,
|
||||
multiscan: true, // Scan using all available cores! Yay!
|
||||
},
|
||||
preference: 'clamdscan', // If clamdscan is found and active, it will be used by default
|
||||
};
|
||||
|
||||
const clamscan = await new ClamScan().init(opts);
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const clamscan = await new ClamScan().init(opts);
|
||||
// You can re-use the `clamscan` object as many times as you want
|
||||
// const version = await clamscan.getVersion();
|
||||
// console.log(`ClamAV Version: ${version}`);
|
||||
const { file, isInfected, viruses } = await clamscan.isInfected(filePath);
|
||||
const result = await clamscan.isInfected(filePath);
|
||||
if (!result || typeof result.isInfected === 'undefined') {
|
||||
reject(new errors.E_VALIDATION_ERROR({ uploadError: 'Unexpected response from virus scan!' }));
|
||||
return;
|
||||
}
|
||||
const { file, isInfected, viruses } = result;
|
||||
if (isInfected) {
|
||||
console.log(`${file} is infected with ${viruses}!`);
|
||||
// reject(new ValidationException(true, { 'upload error': `File ${file} is infected!` }));
|
||||
reject(new errors.E_VALIDATION_ERROR({ uploadError: `File ${file} is infected!` }));
|
||||
console.log(`${file} is infected with ${viruses}!`); // reject(new ValidationException(true, { 'upload error': `File ${file} is infected!` }));
|
||||
reject(new errors.E_VALIDATION_ERROR({ uploadError: `File ${file} is infected with ${viruses}!` }));
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue