How to use scanFile method in redwood

Best JavaScript code snippet using redwood

50-cpanel.js

Source:50-cpanel.js Github

copy

Full Screen

1/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fileencoding=utf-8 : */2/*3 * Copyright 2013, 2014, 2018 James Burlingame4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 *17 */18// enable JavaScript strict mode.19'use strict';20// module imports21const fs = require('fs');22const path = require('path');23const _ = require('lodash');24const when = require('when');25const yaml = require('js-yaml');26const scanfile = require('../scanfile.js');27// Constants28/** Main (non-vhost) access log. */29const CPANEL_MAIN_LOG = '/usr/local/apache/logs/access_log';30/** Panel access log pathname. */31const CPANEL_PANEL_LOG = '/usr/local/cpanel/logs/access_log';32/** Prefix to most access log files. */33const CPANEL_PREFIX = '/usr/local/apache/domlogs';34/** Pathname of the cPanel userdata directory. */35const CPANEL_USERDATA_DIR = '/var/cpanel/userdata';36/** Pathname of user domains configuration file. */37const CPANEL_USERDOMAINS = '/etc/userdomains';38/** Pathname of the cPanel version file. */39const CPANEL_VERSION_FILE = '/usr/local/cpanel/version';40/** Names of files to ignore. */41const IGNORED_FILENAME =42 [43 'ftpxferlog',44 'ftpxferlog.offset',45 'ftpxferlog.offsetftpsep'46 ];47/** Suffixes of ignored files. */48const IGNORED_SUFFIX =49 [50 '-bytes_log',51 '-bytes_log.offset',52 '-ftp_log',53 '-ftp_log.offsetftpbytes',54 '-ftp_log.offset',55 '.bkup',56 '.bkup2'57 ];58/** Patterns for ignored files. */59const IGNORED_PATTERN =60 [61 new RegExp(/-ftp_log-...-\d\d\d\d\.gz$/)62 ];63/** Name of months used in Archive file names. */64const MONTH_NAMES =65 [66 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',67 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'68 ];69/** Pattern used to recognize an archived log file. */70// var archivePattern = new RegExp(/^(.*)(-ssl_log)?-(...-\d\d\d\d)\.gz/);71/** promise of contents of the /etc/userdomains file */72var userdomains = null;73/** promises of the contents of the /var/cpanel/userdata/<account>/main */74const mains = { };75/**76 * Load the contents of the /var/cpanel/userdata/<account>/main file77 * @arg account name.78 */79function loadAccountMain(account) {80 if (!mains[account]) {81 const d = when.defer();82 mains[account] = d.promise;83 const pathname = scanfile.getRootPathname(path.join(CPANEL_USERDATA_DIR, account, 'main'));84 fs.readFile(pathname, {encoding: 'utf8', flag: 'r'}, function (err, data) {85 if (err) {86 d.resolve('');87 } else {88 d.resolve(data);89 }90 });91 }92}93/**94 * Determine an account's home directory.95 * @arg account name.96 * @rtype promise for a pathname of the account's home directory.97 */98function getAccountHomeDirectory(account) {99 const d = when.defer();100 const pathname = scanfile.getRootPathname(path.join(CPANEL_USERDATA_DIR, account, 'main'));101 loadAccountMain(account);102 mains[account].then((data) => {103 var contents = null;104 try {105 contents = yaml.safeLoad(data, { filename: pathname });106 } catch (e) {107 // ignore108 }109 if (contents && contents.main_domain) {110 const pathname = scanfile.getRootPathname(path.join(CPANEL_USERDATA_DIR, account, contents.main_domain));111 const domain = fs.readFileSync(pathname, {encoding: 'utf8', flag: 'r'});112 contents = null;113 try {114 contents = yaml.safeLoad(domain, { filename: pathname });115 } catch (e) {116 // ignore117 }118 }119 if (contents && contents.homedir) {120 d.resolve(scanfile.getRootPathname(contents.homedir));121 } else {122 d.resolve(scanfile.getRootPathname('/home/' + account));123 }124 });125 return d.promise;126}127/**128 * Determine all account names.129 * @rtype Array of String.130 */131function getAllAccounts() {132 const d = when.defer();133 const dir = scanfile.getRootPathname(CPANEL_USERDATA_DIR);134 fs.readdir(dir, function (err, contents) {135 const accounts = [];136 if (!err) {137 contents.forEach((name) => {138 if (name != 'nobody') {139 accounts.push(name);140 }141 });142 }143 d.resolve(accounts);144 });145 return d.promise;146}147/**148 * Find all log files associated with an account.149 * @arg account name.150 * @rtype promise for an Array of ScanFile.151 */152function getAccountLogFiles(account) {153 const promises = [];154 const domains = [];155 const deferred = when.defer();156 loadAccountMain(account);157 mains[account].then(function (data) {158 var contents = null;159 try {160 contents = yaml.safeLoad(data);161 } catch (e) {162 console.error(e.message);163 }164 if (contents) {165 if (contents.main_domain) {166 domains.push({ 'domain': contents.main_domain, 'subdomain': contents.main_domain });167 }168 if (contents.addon_domains) {169 for (var domain in contents.addon_domains) {170 domains.push({ 'domain': domain, 'subdomain': contents.addon_domains[domain] });171 }172 }173 if (contents.sub_domains) {174 contents.sub_domains.forEach((name) => {175 const found = domains.some((domain) => domain.subdomain == name);176 if (!found) {177 domains.push({ 'domain': name, 'subdomain': name });178 }179 });180 }181 domains.forEach((entry) => {182 const filename = path.join(CPANEL_PREFIX, entry.subdomain);183 const pathname = scanfile.getRootPathname(filename);184 const d = when.defer();185 const files = [];186 promises.push(d.promise);187 fs.exists(pathname, (yes) => {188 if (yes) {189 files.push(new scanfile.ScanFile(filename, pathname, entry.domain));190 }191 const sslFilename = filename + '-ssl_log';192 const sslPathname = pathname + '-ssl_log';193 fs.exists(sslPathname, (yes) => {194 if (yes) {195 files.push(new scanfile.ScanFile(sslFilename, sslPathname, entry.domain));196 }197 d.resolve(files);198 });199 });200 });201 }202 }).then(() => {203 when.all(promises).then(function (array) {204 deferred.resolve(_.flatten(array));205 });206 });207 return deferred.promise;208}209/**210 * Get a list of month-year strings used to access archived log files.211 * @arg start : starting Date.212 * @arg stop : ending Date.213 * @rtype Array of String.214 */215function getArchiveMonths(start, stop) {216 var months = [];217 var month, year;218 year = start.getFullYear();219 if (year == stop.getFullYear()) { // less than a full year220 month = start.getMonth();221 } else { // more than one year222 // first partial year223 for (month = start.getMonth(); month < 12; month++) {224 months.push(MONTH_NAMES[month] + '-' + year);225 }226 // full years227 for (year += 1; year < stop.getFullYear(); year++) {228 for (month= 0; month < 12; month++) {229 months.push(MONTH_NAMES[month] + '-' + year);230 }231 }232 year = stop.getFullYear();233 month = 0;234 }235 // last partial year236 for (; month <= stop.getMonth(); month++) {237 months.push(MONTH_NAMES[month] + '-' + year);238 }239 return months;240}241/**242 * Find all archived log files for an account.243 * @arg account name.244 * @arg months Array of String values of Month-Years to check.245 * @rtype promise for an Array of ScanFile.246 */247function getAccountArchiveFiles(account, months) {248 var deferred = when.defer();249 var files = [];250 getAccountHomeDirectory(account).then(function (homedir) {251 var logsdir = path.join(homedir, 'logs');252 loadAccountMain(account);253 mains[account].then(function (data) {254 var contents = null;255 var domains = [];256 try {257 contents = yaml.safeLoad(data);258 } catch (e) {259 // ignore260 }261 if (!contents) {262 deferred.resolve([]);263 return;264 }265 if (contents.main_domain) {266 domains.push({ 'domain': contents.main_domain, 'subdomain': contents.main_domain });267 }268 if (contents.addon_domains) {269 for (var domain in contents.addon_domains) {270 domains.push({ 'domain': domain, 'subdomain': contents.addon_domains[domain] });271 }272 }273 if (contents.sub_domains) {274 contents.sub_domains.forEach(function (name) {275 var found = false;276 for (var n=0, length= domains.length; n < length; n++) {277 if (domains[n].subdomain == name) {278 found = true;279 break;280 }281 }282 if (!found) {283 domains.push({ 'domain': name, 'subdomain': name });284 }285 });286 }287 fs.readdir(logsdir, function (err, logfiles) {288 var filename, pathname;289 if (!err) {290 domains.forEach(function (entry) {291 months.forEach(function (month) {292 filename = entry.subdomain + '-' + month + '.gz';293 if (logfiles.indexOf(filename) >= 0) {294 pathname = path.join(logsdir, filename);295 files.push(new scanfile.ScanFile(undefined, pathname, entry.domain));296 }297 filename = entry.subdomain + '-ssl_log-' + month + '.gz';298 if (logfiles.indexOf(filename) >= 0) {299 pathname = path.join(logsdir, filename);300 files.push(new scanfile.ScanFile(undefined, pathname, entry.domain));301 }302 });303 });304 }305 deferred.resolve(files);306 });307 });308 });309 return deferred.promise;310}311/**312 * Determine Domain log files.313 * @arg domain name used for filename.314 * @arg cannoincal domain name used for grouping.315 * @rtype promise for an Array of ScanFile.316 */317function getDomainLogFiles(domain, cannonical) {318 var deferred = when.defer();319 var files = [];320 var filename, pathname;321 // check for standard log322 filename = path.join(CPANEL_PREFIX, domain);323 pathname = scanfile.getRootPathname(filename);324 fs.exists(pathname, function (yes) {325 if (yes) {326 files.push(new scanfile.ScanFile(filename, pathname, cannonical));327 }328 // check for secure log329 filename += '-ssl_log';330 pathname += '-ssl_log';331 fs.exists(pathname, function (yes) {332 if (yes) {333 files.push(new scanfile.ScanFile(filename, pathname, cannonical));334 }335 deferred.resolve(files);336 });337 });338 return deferred.promise;339}340/**341 * Determine the owner of a domain.342 * @arg domain name.343 * @rtype promise to a String account name (null if not found.)344 */345function getDomainOwner(domain) {346 if (userdomains === null) {347 var d = when.defer();348 userdomains = d.promise;349 var pathname = scanfile.getRootPathname(CPANEL_USERDOMAINS);350 fs.readFile(pathname, { encoding: 'utf8' }, (err, contents) => {351 if (err) {352 d.resolve('');353 } else {354 d.resolve(contents);355 }356 });357 }358 var deferred= when.defer();359 userdomains.then(function (contents) {360 var pattern = new RegExp('^' + domain + ': (.*)$', 'im');361 var find = pattern.exec(contents);362 if (find) {363 deferred.resolve(find[1]);364 } else {365 deferred.resolve(null);366 }367 });368 return deferred.promise;369}370/**371 * Deterine the subdomain version of an addon domain name.372 * @arg domain name.373 * @rtype promise to a String subdomain version of an addon domain name.374 */375function getSubdomainName(domain) {376 var d = when.defer();377 getDomainOwner(domain).then((username) => {378 if (username === null) {379 d.resolve(null);380 } else {381 const pathname = scanfile.getRootPathname(path.join(CPANEL_USERDATA_DIR, username, 'main'));382 loadAccountMain(username);383 mains[username].then((data) => {384 var contents=null;385 try {386 contents = yaml.safeLoad(data, { filename: pathname });387 } catch (e) {388 d.reject(e);389 }390 if (contents && contents.addon_domains && contents.addon_domains[domain]) {391 d.resolve(contents.addon_domains[domain]);392 } else {393 d.resolve(null);394 }395 });396 }397 });398 return d.promise;399}400function isIgnoredFilename(filename) {401 return (IGNORED_FILENAME.indexOf(filename) >= 0);402}403function isIgnoredSuffix(filename) {404 return IGNORED_SUFFIX.some((suffix) => (suffix.length < filename.length) && (suffix == filename.substr(-suffix.length)));405}406function isIgnoredPattern(filename) {407 return IGNORED_PATTERN.some((pattern) => pattern.test(filename));408}409/**410 * Check if file should not be included in results.411 * @arg filename to check.412 * @rtype Boolean - true to ignore file, false to include file.413 */414function isIgnoredFile(filename) {415 return isIgnoredFilename(filename) || isIgnoredSuffix(filename) || isIgnoredPattern(filename);416}417/**418 * Interface to use when cPanel is found for root.419 */420var cPanel = {421 /** Identifier of the panel interface. */422 id : 'cPanel',423 /** Does this panel support --accounts option. */424 hasAccounts : true,425 /** Does this panel support --archives option. */426 hasArchives : true,427 /** Does this panel support --domains and --domlogs options. */428 hasDomains : true,429 /** Does this panel support --panel option. */430 hasPanelLog : true,431 /** Does this panel support --main option. */432 hasMainLog : true,433 /**434 * Deterine if the panel is installed.435 * @rtype Boolean. Use cPanel version file to check installation.436 */437 isActive : function() {438 var pathname = scanfile.getRootPathname(CPANEL_VERSION_FILE);439 return fs.existsSync(pathname);440 },441 /**442 * Find all available log files for accounts and domains.443 * @rtype promise for an Array of ScanFile.444 */445 findAllLogFiles : function () {446 const deferred = when.defer();447 const promises = [];448 const accounts = getAllAccounts();449 accounts.then((list) => {450 list.forEach((account) => {451 promises.push(getAccountLogFiles(account));452 });453 when.all(promises).then((array) => {454 deferred.resolve(_.flatten(array));455 });456 });457 return deferred.promise;458 },459 /**460 * Find all log files associated with an account.461 * @arg account name.462 * @rtype promise for an Array of ScanFile.463 */464 findAccountLogFiles : function (account) {465 return getAccountLogFiles(account);466 },467 /**468 * Find log files associated with a single domain.469 * @arg domain name.470 * @rtype promise for an Array of ScanFile.471 */472 findDomainLogFiles : function (domain) {473 const promises = [];474 const deferred = when.defer();475 getSubdomainName(domain).then((subdomain) => {476 if (subdomain) {477 promises.push(getDomainLogFiles(subdomain, domain));478 }479 promises.push(getDomainLogFiles(domain, domain));480 when.all(promises).then((array) => {481 deferred.resolve(_.flatten(array));482 });483 });484 return deferred.promise;485 },486 /**487 * Find the main (no vhost) log files.488 * @rtype promise for an Array of ScanFile.489 */490 findMainLogFiles : function () {491 const pathname = scanfile.getRootPathname(CPANEL_MAIN_LOG);492 const d = when.defer();493 fs.exists(pathname, (yes) => {494 if (yes) {495 d.resolve([ new scanfile.ScanFile(CPANEL_MAIN_LOG, pathname, 'main') ]);496 } else {497 d.resolve([]);498 }499 });500 return d.promise;501 },502 /**503 * Find the log files associated with the panel itself.504 * @rtype promise for an Array of ScanFile.505 */506 findPanelLogFiles : function () {507 const pathname = scanfile.getRootPathname(CPANEL_PANEL_LOG);508 const d = when.defer();509 fs.exists(pathname, (yes) => {510 if (yes) {511 d.resolve([ new scanfile.ScanFile(CPANEL_PANEL_LOG, pathname, 'panel') ]);512 } else {513 d.resolve([]);514 }515 });516 return d.promise;517 },518 /**519 * Find all archived log files between the start and stop Date's.520 * @arg start first Date of archives.521 * @arg stop last Date of archives.522 * @rtype promise for an Array of ScanFile.523 */524 findAllArchiveFiles : function (start, stop) {525 const months = getArchiveMonths(start, stop);526 const promises = [];527 const accounts = getAllAccounts();528 const deferred = when.defer();529 accounts.then((list) => {530 list.forEach((account) => {531 promises.push(getAccountArchiveFiles(account, months));532 });533 when.all(promises).then((array) => {534 deferred.resolve(_.flatten(array));535 });536 });537 return deferred.promise;538 },539 /**540 * Find all archived log files for an account.541 * @arg account name.542 * @arg start first Date of archives.543 * @arg stop last Date of archives.544 * @rtype promise for an Array of ScanFile.545 */546 findAccountArchiveFiles : function (account, start, stop) {547 const months = getArchiveMonths(start, stop);548 return getAccountArchiveFiles(account, months);549 },550 /**551 * Find all archived log files for a domain.552 * @arg domain name.553 * @arg start first Date of archives.554 * @arg stop last Date of archives.555 * @rtype promise for an Array of ScanFile.556 */557 findDomainArchiveFiles : function (domain, start, stop) {558 const d = when.defer();559 getDomainOwner(domain).then((account) => {560 if (!account) {561 d.resolve([]);562 return;563 }564 getSubdomainName(domain).then((subdomain) => {565 getAccountHomeDirectory(account).then((homedir) => {566 const logsdir = path.join(homedir, 'logs');567 const months = getArchiveMonths(start, stop);568 const files = [];569 fs.readdir(logsdir, (err, logfiles) => {570 if (err) {571 d.resolve([]);572 return;573 }574 var filename, pathname;575 months.forEach((month) => {576 filename = domain + '-' + month + '.gz';577 if (logfiles.indexOf(filename) >= 0) {578 pathname = path.join(logsdir, filename);579 files.push(new scanfile.ScanFile(undefined, pathname, domain));580 } else if (subdomain) {581 filename = subdomain + '-' + month + '.gz';582 if (logfiles.indexOf(filename) >= 0) {583 pathname = path.join(logsdir, filename);584 files.push(new scanfile.ScanFile(undefined, pathname, domain));585 }586 }587 filename = domain + '-ssl_log-' + month + '.gz';588 if (logfiles.indexOf(filename) >= 0) {589 pathname = path.join(logsdir, filename);590 files.push(new scanfile.ScanFile(undefined, pathname, domain));591 } else if (subdomain) {592 filename = subdomain + '-ssl_log-' + month + '.gz';593 if (logfiles.indexOf(filename) >= 0) {594 pathname = path.join(logsdir, filename);595 files.push(new scanfile.ScanFile(undefined, pathname, domain));596 }597 }598 });599 d.resolve(files);600 });601 });602 });603 });604 return d.promise;605 },606 /**607 * Find all archived main log files.608 * @arg start first Date of archives.609 * @arg stop last Date of archives.610 * @rtype promise for an (empty) Array of ScanFile.611 */612 findMainArchiveFiles : function (start, stop) { // eslint-disable-line no-unused-vars613 return when([]);614 },615 /**616 * Find all archived panel log files.617 * @arg start first Date of archives.618 * @arg stop last Date of archives.619 * @rtype promise for an (empty) Array of ScanFile.620 */621 findPanelArchiveFiles : function (start, stop) { // eslint-disable-line no-unused-vars622 return when([]);623 },624 /**625 * Find a single log file.626 * @arg filename - name of the log file.627 * @rtype promise to array of ScanFile.628 */629 findLogFile : function (filename) {630 const pathname = scanfile.getRootPathname(filename);631 const d = when.defer();632 fs.exists(pathname, (yes) => {633 if (yes) {634 d.resolve([ new scanfile.ScanFile(filename, pathname, 'file') ]);635 } else {636 fs.exists(filename, (yes) => {637 if (yes) {638 d.resolve([ new scanfile.ScanFile(filename, filename, 'file') ]);639 } else if (filename == '-') {640 d.resolve([ new scanfile.ScanFile('-', undefined, 'file') ]);641 } else {642 d.resolve([]);643 }644 });645 }646 });647 return d.promise;648 },649 /**650 * Find all log files in a directory.651 * @arg dir directory name.652 * @rtype promise to Array of ScanFile.653 */654 findLogFilesInDirectory : function (dir) {655 const dirpath = scanfile.getRootPathname(dir);656 const deferred = when.defer();657 const promises = [];658 fs.readdir(dirpath, (err, files) => {659 if (err) {660 deferred.resolve([]);661 } else {662 files.forEach((file) => {663 const d = when.defer();664 promises.push(d.promise);665 const pathname= path.join(dirpath, file);666 const filename= path.join(dir, file);667 if (isIgnoredFile(file)) {668 d.resolve([]);669 } else {670 fs.stat(pathname, (err, stats) => {671 if (!err && stats.isFile()) {672 d.resolve([ new scanfile.ScanFile(filename, pathname, 'directory') ]);673 } else {674 d.resolve([]);675 }676 });677 }678 });679 when.all(promises).then((array) => {680 deferred.resolve(_.flatten(array));681 });682 }683 });684 return deferred.promise;685 }686};...

Full Screen

Full Screen

scanfile-test.js

Source:scanfile-test.js Github

copy

Full Screen

1/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fileencoding=utf-8 : */2/*3 * Copyright 2013 James Burlingame4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 *17 */18// enable JavaScript strict mode.19'use strict';20const path = require('path');21const testData = require('../test/testData.js');22const scanfile = require('../lib/scanfile.js');23const ScanFile = scanfile.ScanFile;24var s = null;25describe('scanfile', () => {26 beforeEach(() => {27 s = scanfile;28 });29 describe('getRootPathname', () => {30 describe('default root directory', () => {31 beforeEach(() => s.setRootDirectory(''));32 test('-', () => expect(s.getRootPathname('-')).toEqual('/dev/fd/0'));33 test('/dev/null', () => expect(s.getRootPathname('/dev/null')).toEqual('/dev/null'));34 test('/etc/passwd', () => expect(s.getRootPathname('/etc/passwd')).toEqual('/etc/passwd'));35 test('filename', () => expect(s.getRootPathname('filename')).toEqual('filename'));36 });37 describe('test root directory', () => {38 beforeEach(() => s.setRootDirectory(testData.getDataDirectory()));39 test('-', () => expect(s.getRootPathname('-')).toEqual('/dev/fd/0'));40 test('filename', () => expect(s.getRootPathname('filename')).toEqual('filename'));41 test('/logs/samplx.org', () => {42 const pathname = s.getRootPathname('/logs/samplx.org');43 const expected = path.join(testData.getDataDirectory(), 'logs', 'samplx.org');44 expect(pathname).toEqual(expected);45 });46 });47 });48 describe('ScanFile class', () => {49 test('ScanFile(undefined, pathname, domain)', () => {50 s.setRootDirectory('');51 const file = new ScanFile(undefined, '/usr/local/apache/domlogs/samplx.org', 'samplx.org');52 expect(file.filename).toEqual('/usr/local/apache/domlogs/samplx.org');53 expect(file.pathname).toEqual('/usr/local/apache/domlogs/samplx.org');54 expect(file.domain).toEqual('samplx.org');55 });56 test('call constructor function directly with ScanFile(undefined, pathname, domain)', () => {57 s.setRootDirectory('');58 const file = ScanFile(undefined, '/usr/local/apache/domlogs/samplx.org', 'samplx.org');59 expect(file.filename).toEqual('/usr/local/apache/domlogs/samplx.org');60 expect(file.pathname).toEqual('/usr/local/apache/domlogs/samplx.org');61 expect(file.domain).toEqual('samplx.org');62 });63 test('ScanFile(filename, undefined, domain)', () => {64 s.setRootDirectory(testData.getDataDirectory());65 const file = new ScanFile('/usr/local/apache/domlogs/samplx.org', undefined, 'samplx.org');66 expect(file.filename).toEqual('/usr/local/apache/domlogs/samplx.org');67 const pathname = path.join(testData.getDataDirectory(), 'usr', 'local', 'apache', 'domlogs', 'samplx.org');68 expect(file.pathname).toEqual(pathname);69 expect(file.domain).toEqual('samplx.org');70 });71 test('ScanFile(undefined, undefined, domain)', () => {72 s.setRootDirectory(testData.getDataDirectory());73 const attempt = () => {74 return new ScanFile(undefined, undefined, 'samplx.org');75 };76 expect(attempt).toThrow();77 });78 test('ScanFile("-", undefined, "file")', () => {79 s.setRootDirectory('');80 const file = new ScanFile('-', undefined, 'file');81 expect(file.filename).toEqual('-');82 expect(file.pathname).toEqual('/dev/fd/0');83 expect(file.domain).toEqual('file');84 });85 test('ScanFile(undefined, "/dev/fd/0", "file")', () => {86 s.setRootDirectory('');87 const file = new ScanFile(undefined, '/dev/fd/0', 'file');88 expect(file.filename).toEqual('-');89 expect(file.pathname).toEqual('/dev/fd/0');90 expect(file.domain).toEqual('file');91 });92 describe('isCompressed', () => {93 test('isCompressed("/dev/fd/0")', () => {94 s.setRootDirectory('');95 const file = new ScanFile(undefined, '/dev/fd/0', 'file');96 expect(file.isCompressed()).toBeFalsy();97 });98 test('isCompressed("samplx.org-Apr-2012.gz")', () => {99 s.setRootDirectory('');100 const file = new ScanFile(undefined, 'samplx.org-Apr-2012.gz', 'file');101 expect(file.isCompressed()).toBeTruthy();102 });103 });104 });...

Full Screen

Full Screen

scan-file.js

Source:scan-file.js Github

copy

Full Screen

1import moment from 'moment';2import xhr from 'xhr';3import uniqid from 'uniqid';4import SparkMD5 from 'spark-md5';5import {SCAN_STATUS} from '../constants/file';6function ScanFile() {7 return {8 id: uniqid(),9 fileName: null,10 scanTime: moment().unix(),11 sha256: null,12 md5: null,13 status: ScanFile.STATUS.SCANNING,14 scanResults: null,15 size: null,16 dataId: null17 };18}19export default ScanFile;20// methods21ScanFile.getScanStatus = getScanStatus;22ScanFile.getScanStatusLabel = getScanStatusLabel;23ScanFile.download = download;24ScanFile.isSanitizedFile = isSanitizedFile;25ScanFile.getFileSize = getFileSize;26ScanFile.getFileData = getFileData;27ScanFile.getMd5Hash = getMd5Hash;28// const29ScanFile.STATUS = SCAN_STATUS.VALUES;30ScanFile.STATUS_VALUES_CLEAN = SCAN_STATUS.CLEAN_VALUES;31ScanFile.STATUS_VALUES_INFECTED = SCAN_STATUS.INFECTED_VALUES;32const URL = window.URL || window.webkitURL || window.mozURL || window.msURL;33async function download(link, fileData, fileName) {34 let fileUrl;35 try {36 fileUrl = URL.createObjectURL(new Blob([fileData]));37 }38 catch (e) {39 // fallback40 fileUrl = link;41 }42 return new Promise((resolve) => {43 chrome.downloads.download({44 url: fileUrl,45 filename: fileName46 }, (downloadId) => {47 resolve(downloadId);48 });49 });50}51/**52 * Checks if an URL points to a sanitized file.53 * 54 * @param {string} url a file url55 * @returns {boolean} `true` if the url provided is of a sanitized file56 */57function isSanitizedFile(url) {58 const urlLow = url.toLowerCase();59 // metadefender cloud sanitized files60 for (let bucket of MCL.config.sanitizationBuckets) {61 if (urlLow.indexOf(bucket) > -1 ) {62 return true;63 }64 }65 // metadefender core sanitized files66 if (urlLow.indexOf('/file/converted/') > -1 && urlLow.indexOf('?apikey=') > -1) {67 return true;68 }69}70function getFileSize(url, filename){71 return new Promise((resolve, reject) => {72 if (url.match(/^data/)) {73 reject(chrome.i18n.getMessage('unsupportedUrl'));74 }75 if (url.match(/^ftp/)) {76 reject(chrome.i18n.getMessage('unableToScanFTP'));77 }78 if (url.match(/^file/)) {79 reject(chrome.i18n.getMessage('unableToScanFileProtocol'));80 }81 if (!url.match(/^http/)) {82 url = 'http://' + url;83 }84 xhr.head(url, (err, resp) => {85 if (err) {86 reject(err);87 }88 if ([0, 403, 404, 500, 503].indexOf(resp.statusCode) >= 0) {89 reject(chrome.i18n.getMessage('errorWhileDownloading'));90 }91 if (!filename) {92 let url = resp.url;93 filename = url.substr(url.lastIndexOf('/') + 1);94 }95 let fileSize = resp.headers['content-length'];96 resolve(fileSize);97 });98 });99}100async function getFileData(url){101 return new Promise((resolve, reject) => {102 try {103 xhr.get(url, {104 responseType: 'arraybuffer'105 }, (err, resp, body) => {106 if (err) {107 reject(err);108 }109 if (body) {110 var byteArray = new Uint8Array(body);111 resolve(byteArray);112 }113 });114 }115 catch (e) {116 reject(e);117 }118 });119}120/**121 * 'clean' | 'infected' | 'scanning'122 * @param status123 */124function getScanStatus(status) {125 if (typeof status === 'undefined') {126 return ScanFile.STATUS.SCANNING;127 }128 if (ScanFile.STATUS_VALUES_CLEAN.indexOf(status) !== -1) {129 return ScanFile.STATUS.CLEAN;130 }131 if (ScanFile.STATUS_VALUES_INFECTED.indexOf(status) !== -1) {132 return ScanFile.STATUS.INFECTED;133 }134 return ScanFile.STATUS.UNKNOWN;135}136function getScanStatusLabel(status) {137 if (typeof status === 'undefined') {138 status = 255;139 }140 return chrome.i18n.getMessage('scanResult'+status);141}142function getMd5Hash(fileData) {143 let spark = new SparkMD5.ArrayBuffer();144 spark.append(fileData);145 return spark.end().toUpperCase();...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { scanFile } from '@redwoodjs/api'2export const handler = async (event, context) => {3 const result = await scanFile({4 })5 return {6 body: JSON.stringify(result),7 }8}9import { S3 } from 'aws-sdk'10export const handler = async (event, context) => {11 const s3 = new S3()12 .scanFile({13 })14 .promise()15 return {16 body: JSON.stringify(result),17 }18}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { scanFile } from '@redwoodjs/api'2export const handler = async () => {3 try {4 const result = await scanFile({5 })6 console.log(result)7 } catch (error) {8 console.error(error)9 }10}11- [scanFile](#scanfile)12 - [Parameters](#parameters)13- [scanUrl](#scanurl)14 - [Parameters](#parameters-1)15- [scanFileFromS3](#scanfilefroms3)16 - [Parameters](#parameters-2)17- [scanFileFromS3](#scanfilefroms3-1)18 - [Parameters](#parameters-3)19- [scanFileFromS3](#scanfilefroms3-2)20 - [Parameters](#parameters-4)21- [scanFileFromS3](#scanfilefroms3-3)22 - [Parameters](#parameters-5)23- [scanFileFromS3](#scanfilefroms3-4)24 - [Parameters](#parameters-6)25- [scanFileFromS3](#scanfilefroms3-5)26 - [Parameters](#parameters-7)27- [scanFileFromS3](#scanfilefroms3-6)28 - [Parameters](#parameters-8)29- [scanFileFromS3](#scanfilefroms3-7)30 - [Parameters](#parameters-9)31- [scanFileFromS3](#scanfilefroms3-8)32 - [Parameters](#parameters-10)33- [scanFileFromS3](#scanfilefroms3-9)34 - [Parameters](#parameters-11)35- [scanFileFromS3](#scanfilefroms3-10)36 - [Parameters](#parameters-12)37- [scanFileFromS3](#scanfilefroms3-11)38 - [Parameters](#parameters-13)

Full Screen

Using AI Code Generation

copy

Full Screen

1const redwood = require('./redwood');2const rw = new redwood();3rw.scanFile('test.txt').then((data) => {4 console.log(data);5}).catch((err) => {6 console.log(err);7});8class redwood {9 scanFile(path) {10 return new Promise((resolve, reject) => {11 });12 }13}14module.exports = redwood;15Your name to display (optional):16Your name to display (optional):17const redwood = require('./redwood');18const rw = new redwood();19rw.scanFile('test.txt').then((data) => {20 console.log(data);21}).catch((err) => {22 console.log(err);23});24module.exports = class redwood {25 scanFile(path) {26 return new Promise((resolve, reject) => {27 });28 }29}30Your name to display (optional):31const redwood = require('./redwood');32const rw = new redwood();33rw.scanFile('test.txt').then((data) => {34 console.log(data);35}).catch((err) => {36 console.log(err);37});38module.exports = class redwood {39 scanFile(path) {40 return new Promise((resolve, reject) => {41 });42 }43}44Your name to display (optional):45Your name to display (optional):

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var fs = require('fs');3var file = fs.readFileSync('./test.txt');4var result = redwood.scanFile(file);5console.log(result);6var redwood = require('redwood');7var fs = require('fs');8var file = fs.readFileSync('./test.txt');9var result = redwood.scanFile(file);10console.log(result);

Full Screen

Using AI Code Generation

copy

Full Screen

1const redwood = require('redwood');2redwood.scanFile('test.pdf', function(err, result) {3 if (err) {4 console.log(err);5 }6 console.log(result);7});8const redwood = require('redwood');9redwood.scanFile('test.pdf', 'test.jpg', function(err, result) {10 if (err) {11 console.log(err);12 }13 console.log(result);14});15const redwood = require('redwood');16redwood.scanFile('test.pdf', 'test.txt', function(err, result) {17 if (err) {18 console.log(err);19 }20 console.log(result);21});22const redwood = require('redwood');23redwood.scanFile('test.pdf', 'test.json', function(err, result) {24 if (err) {25 console.log(err);26 }27 console.log(result);28});29const redwood = require('redwood');30redwood.scanFile('test.pdf', 'test.csv', function(err, result) {31 if (err) {32 console.log(err);33 }34 console.log(result);35});36const redwood = require('redwood');37redwood.scanFile('test.pdf', 'test.xml', function(err, result) {38 if (err) {39 console.log(err);40 }41 console.log(result);42});43const redwood = require('redwood');44redwood.scanFile('test.pdf

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require("redwoodfs");2redwood.scanFile("test.txt", function(err, data) {3 console.log(data);4});5### redwoodfs.scanDir(dirPath, callback)6var redwood = require("redwoodfs");7redwood.scanDir("test", function(err, data) {8 console.log(data);9});10### redwoodfs.copyFile(sourcePath, destinationPath, callback)11var redwood = require("redwoodfs");12redwood.copyFile("test.txt", "test1.txt", function(err, data) {13 console.log(data);14});15### redwoodfs.copyDir(sourcePath, destinationPath, callback)

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful