smartfile/readme.md

115 lines
4.5 KiB
Markdown
Raw Normal View History

2024-04-02 18:53:02 +00:00
# @push.rocks/smartfile
2024-04-02 18:58:33 +00:00
> offers smart ways to work with files in nodejs
2024-04-01 15:46:40 +00:00
## Install
2024-04-02 19:37:31 +00:00
To integrate `@push.rocks/smartfile` into your project, run:
2024-04-02 18:53:02 +00:00
```bash
npm install @push.rocks/smartfile
2024-04-01 15:46:40 +00:00
```
2020-02-07 21:00:54 +00:00
## Usage
2024-04-02 19:37:31 +00:00
`@push.rocks/smartfile` provides a robust suite of tools for managing files in Node.js projects using modern TypeScript and ESM syntax. It simplifies numerous file operations such as reading, writing, copying, and streaming files, as well as working with directories and virtual file systems.
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
### **Key Features and Classes**
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
- **`SmartFile`**: Facilitates reading from and writing to individual files, managing metadata.
- **`StreamFile`**: Optimizes memory usage by enabling efficient file streaming.
- **`VirtualDirectory`**: Allows manipulation of a group of files or directories as a virtual file system.
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
### **Getting Started with ESM and TypeScript**
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
First, ensure your project supports ESM syntax and TypeScript. Then, begin by importing the desired features from `@push.rocks/smartfile`:
2024-04-01 15:46:40 +00:00
```typescript
2024-04-02 19:37:31 +00:00
import { SmartFile, StreamFile, VirtualDirectory, memory, fs as smartFs } from '@push.rocks/smartfile';
2024-04-01 15:46:40 +00:00
```
2024-04-02 19:37:31 +00:00
### **Reading and Writing Files**
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
#### Reading Files
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
Reading a JSON file:
2024-04-02 18:53:02 +00:00
```typescript
2024-04-02 19:37:31 +00:00
const myJsonFile: SmartFile = await SmartFile.fromFilePath('./data.json');
const jsonData = JSON.parse(myJsonFile.contents.toString());
console.log(jsonData);
2024-04-02 18:53:02 +00:00
```
2024-04-02 19:37:31 +00:00
#### Writing Files
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
Writing content to a file:
2024-04-01 15:46:40 +00:00
2024-04-02 18:53:02 +00:00
```typescript
2024-04-02 19:37:31 +00:00
const filePath: string = './output.txt';
const content: string = 'Hello, SmartFile!';
await memory.toFs(content, filePath);
console.log('File saved successfully.');
2024-04-01 15:46:40 +00:00
```
2024-04-02 19:37:31 +00:00
### **Streaming Large Files**
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
For large files, `StreamFile` provides a memory-efficient streaming solution:
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
```typescript
import { createReadStream } from 'fs';
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
const sourceStream = createReadStream('./large-video.mp4');
const myStreamFile = await StreamFile.fromStream(sourceStream, 'large-video.mp4');
await myStreamFile.writeToDir('./storage');
console.log('Large file streamed to disk successfully.');
2024-04-02 18:53:02 +00:00
```
2024-04-02 19:37:31 +00:00
### **Working with Virtual Directories**
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
`VirtualDirectory` abstracts a collection of files allowing operations to be performed as if they were on disk:
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
```typescript
const virtualDir = new VirtualDirectory();
virtualDir.addSmartfiles([smartFile1, smartFile2]); // Assuming these are SmartFile instances
await virtualDir.saveToDisk('./virtual-output');
console.log('Virtual directory saved to disk.');
2024-04-02 18:53:02 +00:00
```
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
### **Advanced File Operations**
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
`@push.rocks/smartfile` simplifies complex file operations, including:
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
- Copying directories and files
- Removing files or directories
- Listing files and directories with filters
- Reading file content directly into JavaScript objects
2024-04-01 15:46:40 +00:00
2024-04-02 19:37:31 +00:00
### **Web File Handling**
2020-02-07 21:00:54 +00:00
2024-04-02 19:37:31 +00:00
Handling files from HTTP requests:
2020-02-07 21:00:54 +00:00
2024-04-02 19:37:31 +00:00
`@push.rocks/smartfile` offers utilities to work with files from web sources, making it simpler to manage downloads and uploads.
2020-02-07 21:00:54 +00:00
2024-04-02 19:37:31 +00:00
### **Comprehensive File Management**
2024-04-02 18:53:02 +00:00
2024-04-02 19:37:31 +00:00
Whether you're dealing with local files, directories, or files over the internet, `@push.rocks/smartfile` provides a comprehensive set of tools to streamline your workflow and reduce the complexity of file management in your Node.js projects.
2024-04-02 18:53:02 +00:00
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2020-02-07 21:00:54 +00:00
2024-04-02 18:53:02 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2020-02-07 21:00:54 +00:00
2024-04-02 18:53:02 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.