tgstore

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2020 License: MIT Imports: 24 Imported by: 0

README

TGStore

PkgGoDev

An encrypted object storage system with unlimited space backed by Telegram.

Installation

Open your terminal and execute

$ go get golang.design/x/tgstore

done.

The only requirement is the Go, at least v1.13.

Hello, 世界

Create a file named hello.go

package main

import (
	"context"
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"log"
	"strings"
	"time"

	"golang.design/x/tgstore"
	"golang.org/x/crypto/chacha20poly1305"
)

func main() {
	tgs := tgstore.New()
	tgs.BotToken = "<your-telegram-bot-token"
	tgs.ChatID = 1234567890

	objectKey := make([]byte, chacha20poly1305.KeySize)
	if _, err := rand.Read(objectKey); err != nil {
		log.Fatal(err)
	}

	startTime := time.Now()

	object, err := tgs.Upload(
		context.TODO(),
		objectKey,
		strings.NewReader("Hello, 世界"),
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Upload time:", time.Since(startTime))

	startTime = time.Now()

	downloadedObject, err := tgs.Download(
		context.TODO(),
		object.ID,
		objectKey,
	)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Download time:", time.Since(startTime))

	startTime = time.Now()

	rc, err := downloadedObject.NewReader(context.TODO())
	if err != nil {
		log.Fatal(err)
	}
	defer rc.Close()

	b, err := ioutil.ReadAll(rc)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Read time:", time.Since(startTime))

	fmt.Println("Content:", string(b))
}

and run it

$ go run hello.go

then check what your terminal outputs.

Community

If you want to discuss TGStore, or ask questions about it, simply post questions or ideas here.

Contributing

If you want to help build TGStore, simply follow this to send pull requests here.

License

This project is licensed under the MIT License.

License can be found here.

Documentation

Overview

Package tgstore implements an encrypted object storage system with unlimited space backed by Telegram.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Object

type Object struct {
	ID       string
	Size     int64
	Checksum []byte
	// contains filtered or unexported fields
}

Object is the unit of the `TGStore`.

func (*Object) NewReader

func (o *Object) NewReader(ctx context.Context) (*ObjectReader, error)

NewReader returns a new instance of the `ObjectReader`.

type ObjectReader

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

ObjectReader is the reader of the `Object`.

func (*ObjectReader) Close

func (or *ObjectReader) Close() error

Close implements the `io.Closer`.

func (*ObjectReader) Read

func (or *ObjectReader) Read(b []byte) (int, error)

Read implements the `io.Reader`.

func (*ObjectReader) Seek

func (or *ObjectReader) Seek(offset int64, whence int) (int64, error)

Seek implements the `io.Seeker`.

type TGStore

type TGStore struct {
	// BotAPIEndpoint is the endpoint of the Telegram Bot API.
	//
	// You might prefer to use your local Telegram Bot API server. See
	// https://core.telegram.org/bots/api#using-a-local-bot-api-server for
	// benefits.
	//
	// Default value: "https://api.telegram.org"
	BotAPIEndpoint string `mapstructure:"bot_api_endpoint"`

	// BotToken is the Telegram bot token.
	//
	// Default value: ""
	BotToken string `mapstructure:"bot_token"`

	// ChatID is the ID of the Telegram chat used to store the objects to be
	// uploaded.
	//
	// It is ok to change the `ChatID` if you want. The objects that have
	// already been uploaded are not affected.
	//
	// Default value: 0
	ChatID int64 `mapstructure:"chat_id"`

	// MaxFileBytes is the maximum number of bytes allowed for a Telegram
	// file to have.
	//
	// The `MaxFileBytes` must be at least 20971520 (20 MiB).
	//
	// It is ok to change the `MaxFileBytes` if you want. The objects that
	// have already been uploaded are not affected.
	//
	// Default value: 20971520
	MaxFileBytes int `mapstructure:"max_file_bytes"`

	// MaxObjectMetadataCacheBytes is the maximum number of bytes allowed
	// for object metadata cache to use.
	//
	// The `MaxObjectMetadataCacheBytes` must be at least 1048576 (1 MiB).
	//
	// Default value: 1048576
	MaxObjectMetadataCacheBytes int `mapstructure:"max_object_metadata_cache_bytes"`

	// HTTPClient is the `http.Client` used to communicate with the Telegram
	// Bot API.
	//
	// Default value: `http.DefaultClient`
	HTTPClient *http.Client `mapstructure:"-"`
	// contains filtered or unexported fields
}

TGStore is the top-level struct of this project.

It is highly recommended not to modify the value of any field of the `TGStore` after calling any methods of it, which will cause unpredictable problems.

The new instances of the `TGStore` should only be created by calling the `New`.

func New

func New() *TGStore

New returns a new instance of the `TGStore` with default field values.

The `New` is the only function that creates new instances of the `TGStore` and keeps everything working.

func (*TGStore) Append

func (tgs *TGStore) Append(
	ctx context.Context,
	id string,
	key []byte,
	content io.Reader,
) (*Object, error)

Append appends the content to the object targeted by the id.

The lenth of the key must be 16.

func (*TGStore) Download

func (tgs *TGStore) Download(
	ctx context.Context,
	id string,
	key []byte,
) (*Object, error)

Download downloads the object targeted by the id from the cloud. It returns `os.ErrNotExist` if not found.

The lenth of the key must be 16.

func (*TGStore) Upload

func (tgs *TGStore) Upload(
	ctx context.Context,
	key []byte,
	content io.Reader,
) (*Object, error)

Upload uploads the content to the cloud.

The lenth of the key must be 16.

Jump to

Keyboard shortcuts

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