UNPKG

fc-store

Version:

File chunking, and storage with MongoDB

104 lines (64 loc) 2.7 kB
# FileChunk Storage A lightweight Node.js package for splitting (chunking) files, encrypting and storing them in MongoDB, with support for reassembling and deleting files as needed. --- ## 📦 Installation ```bash npm install fc-store ``` ## USAGE const { MongoClient } = require("mongodb"); const path = require("path"); const { FileChunk } = require("fc-store"); async function init() { const mongodbClient = new MongoClient("mongodb://localhost:27017"); await mongodbClient.connect(); const db = mongodbClient.db("fileStorage"); const fileChunk = new FileChunk(db, "fileMeta", "fileData"); // Disassemble a file into chunks and store in MongoDB await fileChunk .disassembleFileFromPath(path.join(\_\_dirname, "video.mp4"), 1024) .then(console.log) .catch(console.error); // Reassemble a file from MongoDB and save it to disk const fileId = "f767b3984686e2c4fc70c403b02527"; await fileChunk .reassembleFileFromPath(fileId, 1024, \_\_dirname, "video") .then(console.log) .catch(console.error); // Delete a file and its chunks from MongoDB await fileChunk .deleteFile(fileId) .then(console.log) .catch(console.error); await mongodbClient.close(); } init().catch(console.error); ## API Reference disassembleFileFromPath(filePath, chunkSize?) Description: Splits a file into chunks and stores them in MongoDB. Parameters: filePath (string): Path to the input file. chunkSize (number, optional): Size of each chunk in bytes. Default: 1024. reassembleFileFromPath(fileId, chunkSize?, outputDir?, outputName?) Description: Reconstructs a file from chunks and saves it to disk. Parameters: fileId (string): Unique identifier of the file. chunkSize (number, optional): Must match the original chunk size used. Default: 1024. outputDir (string, optional): Target directory. Default: current directory. outputName (string, optional): Name of the output file. Default: original name. deleteFile(fileId) Description: Deletes all chunks and metadata related to a file. Parameters: fileId (string): Unique identifier of the file to delete. ## Notes Make sure MongoDB is running and accessible at the specified URI. The chunkSize parameter is optional in both disassembleFileFromPath and reassembleFileFromPath. Default is 1024 bytes. Suitable for storing and reconstructing large files (e.g., videos, images, backups). ## Example Use Cases Uploading large files to a MongoDB-backed service. Creating resumable file uploads/downloads. Efficient storage of binary blobs. ## License MIT --- Let me know if you'd like to add badges, CI/CD info, or an example repo link.