filedrop

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2020 License: MIT Imports: 14 Imported by: 1

README

filedrop

Travis CI CodeCov Issues License

Lightweight file storage server with HTTP API.

Features
  • Painless configuration! You don't even have to rewrite requests on your reverse proxy!
  • Limits support! Link usage count, file size and storage time.
  • Embeddable! Can run as part of your application.

You can use filedrop either as a standalone server or as a part of your application. In former case you want to check filedropd subpackage, in later case just import filedrop package and pass config stucture to filedrop.New, returned object implements http.Handler so you can use it how you like.

Installation

This repository uses Go 1.11 modules. Things may work with old GOPATH approach but we don't support it so don't report cryptic compilation errors caused by wrong dependency version.

master branch contains code from latest (pre-)release. dev branch contains bleeding-edge code. You probably want to use one of tagged releases.

SQL drivers

filedrop uses SQL database as a meta-information storage so you need a SQL driver for it to use.

When building standalone server you may want to enable one of the supported SQL DBMS using build tags:

  • postgres for PostgreSQL
  • sqlite3 for SQLite3
  • mysql for MySQL

Note: No MS SQL Server support is planned. However if you would like to see it - PRs are welcome.

When using filedrop as a library you are given more freedom. Just make sure that you import driver you use.

Library

Just use github.com/foxcpp/filedrop as any other library. Documentation is here: godoc.org.

Standalone server

See fildropd subdirectory. To start server you need a configuration file. See example here. It should be pretty straightforward. Then just pass path to configuration file in command-line arguments.

filedropd /etc/filedropd.yml

systemd unit file is included for your convenience.

HTTP API

POST single file to any endpoint to save it. For example:

POST /filedrop
Content-Type: image/png
Content-Length: XXXX

You will get response with full file URL (endpoint used to POST + UUID), like this one:

http://example.com/filedrop/41a8f78c-ce06-11e8-b2ed-b083fe9824ac

You can add anything as last component to URL to give it human-understandable meaning:

http://example.com/filedrop/41a8f78c-ce06-11e8-b2ed-b083fe9824ac/amazing-screenshot.png

However you can't add more than one component:

http://example.com/filedrop/41a8f78c-ce06-11e8-b2ed-b083fe9824ac/invalid/in/filedrop

You can specify max-uses and store-time-secs to override default settings from server configuration (however you can't set value higher then configured).

POST /filedrop/screenshot.png?max-uses=5&store-secs=3600

Following request will store file screenshot.png for one hour (3600 seconds) and allow it to be downloaded not more than 10 times.

Note To get https scheme in URLs downstream server should set header X-HTTPS-Downstream to 1 (or you can also set HTTPSDownstream config option)

Authorization

When using filedrop as a library you can setup custom callbacks for access control.

See filedrop.AuthConfig documentation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFileDoesntExists = errors.New("file doesn't exists")

Functions

This section is empty.

Types

type AuthConfig

type AuthConfig struct {
	// Callback is called to check access before processing any request.
	// If Callback is null, no check will be performed.
	Callback func(*http.Request) bool `yaml:"omitempty"`
}

type Config

type Config struct {
	// ListenOn specifies endpoint to listen on in format ADDR:PORT. Used only by filedropd.
	ListenOn string `yaml:"listen_on"`

	Limits       LimitsConfig `yaml:"limits"`
	DB           DBConfig     `yaml:"db"`
	DownloadAuth AuthConfig   `yaml:"download_auth"`
	UploadAuth   AuthConfig   `yaml:"upload_auth"`

	// StorageDir is where files will be saved on disk.
	StorageDir string `yaml:"storage_dir"`

	// HTTPSDownstream specifies whether filedrop should return links with https scheme or not.
	// Overridden by X-HTTPS-Downstream header.
	HTTPSDownstream bool `yaml:"https_downstream"`

	// AllowedOrigins specifies Access-Control-Allow-Origin header.
	AllowedOrigins string `yaml:"allowed_origins"`

	// Internal, used only for testing. Always 60 secs in production.
	CleanupIntervalSecs int `yaml:"-"`
}
var Default Config

type DBConfig

type DBConfig struct {
	// Driver is a database/sql driver name.
	Driver string `yaml:"driver"`

	// Data Source Name.
	DSN string `yaml:"dsn"`
}

type LimitsConfig

type LimitsConfig struct {
	// MaxUses is how much much times file can be accessed. Note that it also counts HEAD requests
	// and incomplete downloads (byte-range requests).
	// Per-file max-uses parameter can't exceed this value but can be smaller.
	MaxUses uint `yaml:"max_uses"`

	// MaxStoreSecs specifies max time for which files will be stored on
	// filedrop server. Per-file store-secs parameter can't exceed this value but
	// can be smaller.
	MaxStoreSecs uint `yaml:"max_store_secs"`

	// MaxFileSize is a maximum file size in bytes that can uploaded to filedrop.
	MaxFileSize uint `yaml:"max_file_size"`
}

type Server

type Server struct {
	DB          *db
	Conf        Config
	Logger      *log.Logger
	DebugLogger *log.Logger
	// contains filtered or unexported fields
}

Main filedrop server structure, implements http.Handler.

func New

func New(conf Config) (*Server, error)

Create and initialize new server instance using passed configuration.

serv.Logger will be redirected to os.Stderr by default. Created instances should be closed by using serv.Close.

func (*Server) AddFile

func (s *Server) AddFile(contents io.Reader, contentType string, maxUses uint, storeUntil time.Time) (string, error)

AddFile adds file to storage and returns assigned UUID which can be directly substituted into URL.

func (*Server) Close

func (s *Server) Close() error

func (*Server) GetFile

func (s *Server) GetFile(fileUUID string) (r io.ReadSeeker, contentType string, err error)

GetFile opens file for reading.

Note that access using this function is equivalent to access through HTTP API, so it will count against usage count, for example. To avoid this use OpenFile(fileUUID).

func (*Server) OpenFile

func (s *Server) OpenFile(fileUUID string) (io.ReadSeeker, error)

OpenFile opens file for reading without any other side-effects applied (such as "link" usage counting).

func (*Server) RemoveFile

func (s *Server) RemoveFile(fileUUID string) error

RemoveFile removes file from database and underlying storage.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler for filedrop.Server.

Note that filedrop code is URL prefix-agnostic, so request URI doesn't matters much.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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