plik

package
v0.0.0-...-e675dcb Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 18 Imported by: 0

README

Plik library

Plik library is a Golang library to upload files to a Plik server.

Installation
go get -v github.com/root-gg/plik/plik
1 Easy mode
plik.NewClient("https://plik.server.url")

// To upload regular files
upload, file, err := client.UploadFile("/home/file1")

// To upload a byte stream
upload, file, err := client.UploadReader("filename", ioReader)
2 Full mode

The workflow is :

  • Create a new client
  • Create an Upload
  • Create some Files
  • Add the files to the Upload
  • Create the upload to get the necessary metadata
  • Upload the files
client := plik.NewClient("https://plik.server.url")

// Optional client configuration
client.OneShot = true
client.Token = "xxxx-xxxx-xxxx-xxxx"

upload := client.NewUpload()

// Optional upload configuration
upload.OneShot = false

// Create file from path
file1, err = upload.AddFileFromPath(path)

// Create file from reader
file2, err = upload.AddFileFromReader("filename", ioReader)

// Create upload server side ( optional step that is called by upload.Upload() / file.Upload() if omitted )
err = upload.Create()

// Upload all added files in parallel
err = upload.Upload()

// Upload a single file
err = file.Upload()

// Get upload URL
uploadURL, err := upload.GetURL()

// Get file URL
for _, file := range upload.Files() {
    fileURL, err := file.GetURL()
}
3 Bonus
// Get Upload
upload = client.GetUpload(id)

// Download file
reader, err = upload.Files()[0].Download()

// Download archive
reader, err = upload.DownloadZipArchive()

// Remove File ( need to be authenticated )
err = upload.Files()[0].Delete()

// Remove Upload ( need to be authenticated )
err = upload.Delete()

// Add file still works ( need to be authenticated )
err = upload.AddFileFromPath(path)
err = upload.Upload()

// Get remote server version
buildInfo, err = client.GetServerVersion()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPClient

func NewHTTPClient(insecure bool) *http.Client

NewHTTPClient Create a new HTTP client with ProxyFromEnvironment and InsecureSkipVerify setup

Types

type Client

type Client struct {
	*UploadParams // Default upload params for the Client. Those can be overridden per upload

	Debug bool // Display HTTP request and response and other helpful debug data

	URL             string // URL of the Plik server
	ClientName      string // X-ClientApp HTTP Header setting
	ClientVersion   string // X-ClientVersion HTTP Header setting
	ClientUserAgent string // User-Agent HTTP Header setting

	HTTPClient *http.Client // HTTP Client ot use to make the requests
}

Client manage the process of communicating with a Plik server via the HTTP API

func NewClient

func NewClient(url string) (c *Client)

NewClient creates a new Plik Client

func (*Client) GetServerConfig

func (c *Client) GetServerConfig() (config *common.Configuration, err error)

GetServerConfig return the remote server configuration

func (*Client) GetServerVersion

func (c *Client) GetServerVersion() (bi *common.BuildInfo, err error)

GetServerVersion return the remote server version

func (*Client) GetUpload

func (c *Client) GetUpload(id string) (upload *Upload, err error)

GetUpload fetch upload metadata from the server

func (*Client) GetUploadProtectedByPassword

func (c *Client) GetUploadProtectedByPassword(id string, login string, password string) (upload *Upload, err error)

GetUploadProtectedByPassword fetch upload metadata from the server with login and password

func (*Client) Insecure

func (c *Client) Insecure()

Insecure mode does not verify the server's certificate chain and host name

func (*Client) MakeRequest

func (c *Client) MakeRequest(req *http.Request) (resp *http.Response, err error)

MakeRequest perform an HTTP request to a Plik Server HTTP API.

  • Manage request header X-ClientApp and X-ClientVersion
  • Log the request and response if the client is in Debug mode
  • Parsing response error to Go error

func (*Client) NewUpload

func (c *Client) NewUpload() *Upload

NewUpload create a new Upload object with the client default upload params

func (*Client) UploadFile

func (c *Client) UploadFile(path string) (upload *Upload, file *File, err error)

UploadFile is a handy wrapper to upload a file from the filesystem

func (*Client) UploadReader

func (c *Client) UploadReader(name string, reader io.Reader) (upload *Upload, file *File, err error)

UploadReader is a handy wrapper to upload a single arbitrary data stream

func (*Client) UploadRequest

func (c *Client) UploadRequest(upload *common.Upload, method, URL string, body io.Reader) (req *http.Request, err error)

UploadRequest creates a new HTTP request with the header generated from the given upload params

type File

type File struct {
	Name string
	Size int64
	// contains filtered or unexported fields
}

File contains all relevant info needed to upload data to a Plik server

func (*File) Delete

func (file *File) Delete() (err error)

Delete remove the upload and all the associated files from the remote server

func (*File) Download

func (file *File) Download() (reader io.ReadCloser, err error)

Download downloads all the upload files in a zip archive

func (*File) Error

func (file *File) Error() error

ID return the file ID if any

func (*File) GetURL

func (file *File) GetURL() (URL *url.URL, err error)

GetURL returns the URL to download the file

func (*File) Metadata

func (file *File) Metadata() (details *common.File)

Metadata return the file metadata returned by the server

func (*File) RegisterUploadCallback

func (file *File) RegisterUploadCallback(callback UploadCallback)

RegisterUploadCallback a callback to be executed after the file have been uploaded

func (*File) Upload

func (file *File) Upload() (err error)

Upload uploads a single file.

func (*File) WrapReader

func (file *File) WrapReader(wrapper func(reader io.ReadCloser) io.ReadCloser)

WrapReader a convenient function to alter the content of the file on the file ( encrypt / display progress / ... )

type Upload

type Upload struct {
	UploadParams
	// contains filtered or unexported fields
}

Upload store the necessary data to upload files to a Plik server

func (*Upload) AddFileFromPath

func (upload *Upload) AddFileFromPath(name string) (file *File, err error)

AddFileFromPath add a new file from a filesystem path

func (*Upload) AddFileFromReadCloser

func (upload *Upload) AddFileFromReadCloser(name string, reader io.ReadCloser) (file *File)

AddFileFromReadCloser add a new file from a filename and io.ReadCloser

func (*Upload) AddFileFromReader

func (upload *Upload) AddFileFromReader(name string, reader io.Reader) (file *File)

AddFileFromReader add a new file from a filename and io.Reader

func (*Upload) Create

func (upload *Upload) Create() (err error)

Create a new empty upload on a Plik Server

func (*Upload) Delete

func (upload *Upload) Delete() (err error)

Delete remove the upload and all the associated files from the remote server

func (*Upload) DownloadZipArchive

func (upload *Upload) DownloadZipArchive() (reader io.ReadCloser, err error)

DownloadZipArchive downloads all the upload files in a zip archive

func (*Upload) Files

func (upload *Upload) Files() (files []*File)

Files Return the upload files

func (*Upload) GetAdminURL

func (upload *Upload) GetAdminURL() (u *url.URL, err error)

GetAdminURL return the URL page of the upload with upload admin rights

func (*Upload) GetURL

func (upload *Upload) GetURL() (u *url.URL, err error)

GetURL returns the URL page of the upload

func (*Upload) ID

func (upload *Upload) ID() string

ID returns the upload ID if the upload has been created server side

func (*Upload) Metadata

func (upload *Upload) Metadata() (details *common.Upload)

Metadata return the upload metadata returned by the server

func (*Upload) Upload

func (upload *Upload) Upload() (err error)

Upload uploads all files of the upload in parallel

type UploadCallback

type UploadCallback func(metadata *common.File, err error)

UploadCallback to be executed once the file has been uploaded

type UploadParams

type UploadParams struct {
	Stream    bool // Don't store the file on the server
	OneShot   bool // Force deletion of the file from the server after the first download
	Removable bool // Allow upload and upload files to be removed from the server at any time

	TTL       int    // Time in second before automatic deletion of the file from the server
	ExtendTTL bool   // Extend upload expiration date by TTL when accessed
	Comments  string // Arbitrary comment to attach to the upload ( the web interface support markdown language )

	Token string // Authentication token to link an upload to a Plik user

	Login    string // HttpBasic protection for the upload
	Password string // Login and Password
}

UploadParams store the different options available when uploading file to a Plik server One should add files to the upload before calling Create or Upload

Jump to

Keyboard shortcuts

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