Filesystem

Provides an abstraction around local and remote filesystems

Technology

The filesystem module provides a unified abstraction layer for Project Forge applications to interact with different storage backends. It enables applications to work seamlessly with local disk storage, in-memory filesystems, and remote storage systems through a consistent API.

Overview

This module provides:

Key Features

Storage Backends

File Operations

Advanced Capabilities

Package Structure

Core Interface

Implementation

File Types and Utilities

Usage Examples

Basic File Operations

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create a new filesystem rooted at ./data
fs, err := NewFileSystem("./data", false, "file")
if err != nil {
return err
}

// Read a file
content, err := fs.ReadFile("config.json")
if err != nil {
return err
}

// Write a file
err = fs.WriteFile("output.txt", []byte("Hello World"), 0644, false)
if err != nil {
return err
}

JSON File Handling

1
2
3
4
5
6
// Write structured data as JSON
data := map[string]interface{}{
"version": "1.0",
"enabled": true,
}
err := fs.WriteJSONFile("config.json", data, 0644, true)

Directory Operations

1
2
3
4
5
6
7
8
// Create directory structure
err := fs.CreateDirectory("logs/application")

// List files with filtering
files := fs.ListFiles("./", []string{".git", "node_modules"}, logger)

// Recursive file listing with patterns
matches, err := fs.ListFilesRecursive("./src", nil, logger, "*.go")

Advanced Features

1
2
3
4
5
6
7
8
// Download remote file
size, err := fs.Download(ctx, "https://example.com/file.zip", "downloads/file.zip", true, logger)

// Extract ZIP archive
fileMap, err := fs.UnzipToDir("archive.zip", "extracted/")

// Copy with ignore patterns
err := fs.CopyRecursive("src/", "backup/", []string{"*.tmp", ".DS_Store"}, logger)

Configuration

The filesystem can be configured through the NewFileSystem constructor:

Source Code

See Also