mds-file-upload
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

mds-file-upload

mds-file-upload is a simple and efficient file upload utility built for Node.js applications. It supports uploading files to a specified folder and managing temporary and permanent storage paths with ease.


Features

  • Upload files to a specified folder.
  • Temporary file storage option.
  • Move files from temporary to permanent storage.
  • Delete uploaded files.
  • Check for the existence of files or folders.

Installation

To install the package, run:

npm install mds-file-upload

Usage

Importing and Initialization

Import the MdsFileUpload class and initialize it with your project's base directory:

import { MdsFileUpload } from "mds-file-upload";

const uploader = new MdsFileUpload(process.cwd());

Methods

1. handleFileUpload

Uploads a file to the uploads folder by default or to the temp folder if specified.

Parameters:

  • infile (File): The file to upload.
  • toTemp (boolean, optional): Whether to upload to the temporary folder. Defaults to false.

Example:

const file = new File(["content"], "example.txt");

uploader.handleFileUpload(file)
  .then(result => console.log("File uploaded:", result))
  .catch(err => console.error("Upload failed:", err));

2. deleteUploadedFile

Deletes a file from the uploads folder.

Parameters:

  • filename (string): The name of the file to delete.

Example:

uploader.deleteUploadedFile("example.txt")
  .then(() => console.log("File deleted."))
  .catch(err => console.error("Deletion failed:", err));

3. moveFileToUpload

Moves a file from the temp folder to the uploads folder.

Parameters:

  • filename (string): The name of the file to move.

Example:

uploader.moveFileToUpload("temp-example.txt")
  .then(success => console.log("File moved successfully:", success))
  .catch(err => console.error("Move failed:", err));

4. checkFileOrFolderExists

Checks if a file or folder exists at a specified path.

Parameters:

  • targetPath (string): The path to check.

Example:

uploader.checkFileOrFolderExists("/path/to/file.txt")
  .then(exists => console.log("Exists:", exists))
  .catch(err => console.error("Error checking existence:", err));

File Structure

mds-file-upload/
├── src/
│   ├── index.ts       # Main source file
├── dist/              # Compiled output
├── package.json       # NPM configuration
├── README.md          # Project documentation
└── tsconfig.json      # TypeScript configuration

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit your changes (git commit -m 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Open a Pull Request.

License

This project is licensed under the MIT License.


Author

Developed and maintained by Mwero Abdalla.


Repository

The source code for this project is available on GitHub:

https://github.com/mwenaro/mds-file-upload


Versioning

This package follows semantic versioning. Refer to the CHANGELOG for details on updates.

Package Sidebar

Install

npm i mds-file-upload

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

10.3 kB

Total Files

6

Last publish

Collaborators

  • mwero