sqlitefs

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 10 Imported by: 2

README

SQLiteFS

SQLiteFS is a Go package that implements the fs.FS interface using SQLite as a storage backend. This allows you to store and access files directly from a SQLite database, which can be useful for embedded applications, resource-constrained systems, or when you need a unified storage for both files and metadata.

Features

  • Implementation of the fs.FS interface
  • File storage in SQLite database
  • Support for concurrent writes through a shared channel
  • Fragmented file storage for efficient handling of large files
  • Automatic MIME type detection for files

Installation

To use SQLiteFS in your Go project, run the following command:

go get github.com/jilio/sqlitefs

Usage

Here's a simple example of how to use SQLiteFS:

package main

import (
 "database/sql"
 "fmt"
 "io/fs"
 "log"

 "github.com/jilio/sqlitefs"
 _ "modernc.org/sqlite"
)

func main() {
 // Open a connection to the SQLite database
 db, err := sql.Open("sqlite", "files.db")
 if err != nil {
  log.Fatal(err)
 }
 defer db.Close()

 // Create a new instance of SQLiteFS
 sqliteFS, err := sqlitefs.NewSQLiteFS(db)
 if err != nil {
  log.Fatal(err)
 }
 defer sqliteFS.Close()

 // Write a file
 writer := sqliteFS.NewWriter("example.txt")
 _, err = writer.Write([]byte("Hello, SQLiteFS!"))
 if err != nil {
  log.Fatal(err)
 }
 err = writer.Close()
 if err != nil {
  log.Fatal(err)
 }

 // Read a file
 file, err := sqliteFS.Open("example.txt")
 if err != nil {
  log.Fatal(err)
 }
 defer file.Close()

 content, err := fs.ReadFile(sqliteFS, "example.txt")
 if err != nil {
  log.Fatal(err)
 }

 fmt.Printf("File content: %s\n", content)
}

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SQLiteFS

type SQLiteFS struct {
	// contains filtered or unexported fields
}

func NewSQLiteFS

func NewSQLiteFS(db *sql.DB) (*SQLiteFS, error)

NewSQLiteFS создает новый экземпляр SQLiteFS с заданной базой данных. Проверяет наличие необходимых таблиц и создает их при отсутствии.

func (*SQLiteFS) Close added in v0.3.0

func (fs *SQLiteFS) Close() error

func (*SQLiteFS) NewWriter added in v0.3.1

func (fs *SQLiteFS) NewWriter(path string) *SQLiteWriter

NewWriter creates a new writer for the specified path.

func (*SQLiteFS) Open

func (fs *SQLiteFS) Open(name string) (fs.File, error)

Open открывает файл по указанному пути.

type SQLiteFile

type SQLiteFile struct {
	// contains filtered or unexported fields
}

SQLiteFile реализует интерфейс http.File.

func NewSQLiteFile

func NewSQLiteFile(db *sql.DB, path string) (*SQLiteFile, error)

NewSQLiteFile создает новый экземпляр SQLiteFile для заданного пути.

func (*SQLiteFile) Close

func (f *SQLiteFile) Close() error

func (*SQLiteFile) Read

func (f *SQLiteFile) Read(p []byte) (int, error)

func (*SQLiteFile) Readdir

func (f *SQLiteFile) Readdir(count int) ([]os.FileInfo, error)

func (*SQLiteFile) Seek

func (f *SQLiteFile) Seek(offset int64, whence int) (int64, error)

func (*SQLiteFile) Stat

func (f *SQLiteFile) Stat() (os.FileInfo, error)

type SQLiteWriter

type SQLiteWriter struct {
	// contains filtered or unexported fields
}

func NewSQLiteWriter

func NewSQLiteWriter(fs *SQLiteFS, path string) *SQLiteWriter

NewSQLiteWriter creates a new SQLiteWriter for the specified path. Deprecated: Use SQLiteFS.NewWriter instead.

func (*SQLiteWriter) Close

func (w *SQLiteWriter) Close() error

func (*SQLiteWriter) Write

func (w *SQLiteWriter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL