Categorygithub.com/andrewpillar/fs
modulepackage
0.0.0-20230916142854-854e0c9b1a1c
Repository: https://github.com/andrewpillar/fs.git
Documentation: pkg.go.dev

# README

fs

fs is a simple library that provides a simple abstraction over filesystem operations that is compatible with io/fs from the standard library.

Examples

File Uploads

Assume you're building an application to upload files and store them against a hash of their content but with a size limit, then we could do the following with this library,

import (
    "crypto/sha256"
    "io"
    "net/http"

    "github.com/andrewpillar/fs"
)

var defaultMaxMemory int64 = 32 << 20

func Upload(w http.ResonseWriter, r *http.Request) {
    if err := r.ParseMultipartForm(defaultMaxMemory); err != nil {
        // handle error
    }

    hdrs, ok := r.MultipartForm.File["upload"]

    if !ok {
        // handle error
    }

    tmp, err := fs.ReadFile("upload", hds[0].Open())

    if err != nil {
        // handle error
    }

    // Delete the file if it get's stored on disk from ReadFile.
    defer fs.Cleanup(tmp)

    osfs := fs.New("/tmp")
    hashfs := fs.Hash(osfs, sha256.New)
    limitfs := fs.Limit(hashfs, 5 << 20)

    f, err = limitfs.Put(tmp)

    if err != nil {
        // handle error
    }

    info, err := f.Stat()

    if err != nil {
        // handle error
    }

    w.WriteHeader(http.StatusNoContent)
    io.WriteString(info.Name())
}

With the above example we use the library to create an FS for the operating system via fs.New. This is then wrapped with fs.Hash to store the file against the SHA256 hash of the file's content. This is then wrapped with fs.Limit to limit the size of files that can be stored in the filesystem. Finally the name of the uploaded file, which would be the file content's hash, is sent in the response.

# Packages

No description provided by the author

# Functions

Cleanup deletes the given file if it exists on disk and is stored in the temporary directory.
Hash returns a filesystem that stores each file put in it against the hashed contents of the file with the given hashing mechanism.
Limit returns a filesystem that limits the size of files put in it to the given limit.
New returns a new FS for the operating system's filesystem.
Null returns a store that returns empty files.
ReadFile functions the same as ReadFileMax only using a default maxMemory of 32MB.
ReadFileMax reads the given reader into memory using at most maxMemory to store it and returns it as a File with the given name.
ReadOnly returns a filesystem that can only have a file read from it.
Rename returns a new File with the given name.
Unique returns a filesystem that will error with ErrExist when multiple files with the same name are stored in it.
WriteOnly returns a filesystem that can only have files put in it.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Interfaces

FS provides access to a hierarchical filesystem.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author