RapidMod

Node File Watcher

May 5th, 2023 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * File Watcher and Processor
  3.  *
  4.  * Description:
  5.  * This script, authored by RapidMod, utilizes the Chokidar library to watch for new files in a specified directory
  6.  * and processes them in the order they were added. It's designed to monitor an upload directory,
  7.  * automatically adding new files to a processing queue. Each file is then processed according
  8.  * to the logic defined in the `processFile` function. This setup is ideal for applications that
  9.  * need to handle file uploads and process them sequentially, ensuring no file is overlooked and
  10.  * each is handled in a timely manner.
  11.  *
  12.  * Features:
  13.  * - Watches a specified directory for new file additions.
  14.  * - Ignores hidden files (those beginning with a dot).
  15.  * - Maintains a queue of files to be processed, ensuring order based on creation time.
  16.  * - Processes each file sequentially through a customizable processing function.
  17.  *
  18.  * Getting Started:
  19.  * 1. Ensure Node.js is installed on your system.
  20.  * 2. Install Chokidar via npm with `npm install chokidar`.
  21.  * 3. Modify the `uploadDir` variable to point to your target directory.
  22.  * 4. Implement your file processing logic in the `processFile` function.
  23.  * 5. Run the script using `node <script_name>.js`.
  24.  *
  25.  * The script will then monitor the specified directory and log a message to the console
  26.  * each time a new file is processed. Modify the `processFile` function to fit your specific
  27.  * processing requirements.
  28.  *
  29.  * Note: This script is designed to run indefinitely. Terminate it manually when it's no longer needed.
  30.  *
  31.  * Author: RapidMod
  32.  * Website: https://rapidmod.io/
  33.  */
  34.  
  35. const chokidar = require('chokidar');
  36. const path = require('path');
  37. const fs = require('fs');
  38.  
  39. const uploadDir = '/my/upload/dir';
  40. const watcher = chokidar.watch(uploadDir, {
  41.   ignored: /^\./, // Ignore hidden files
  42.   persistent: true
  43. });
  44.  
  45. const fileQueue = [];
  46.  
  47. // Function to process a file
  48. function processFile(filePath) {
  49.   // Implement your file processing logic here
  50.   console.log(`Processing file: ${filePath}`);
  51. }
  52.  
  53. // Process files in the queue in the order they were created
  54. function processQueue() {
  55.   if (fileQueue.length > 0) {
  56.     const nextFile = fileQueue.shift();
  57.     processFile(nextFile);
  58.   }
  59. }
  60.  
  61. watcher
  62.   .on('add', (filePath) => {
  63.     // Add the new file to the queue
  64.     fileQueue.push(filePath);
  65.     // Sort the queue based on file creation time
  66.     fileQueue.sort((a, b) => {
  67.       return fs.statSync(a).birthtimeMs - fs.statSync(b).birthtimeMs;
  68.     });
  69.     // Process the files in the queue
  70.     processQueue();
  71.   });
  72.  
  73. console.log(`Watching for new files in directory: ${uploadDir}`);
  74.  
  75. // Keep the script running
  76. setInterval(() => {}, 1000);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
Advertisement