uploadclient

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Package uploadclient contains functions for uploading one file to different types of service.

Index

Examples

Constants

View Source
const (

	// ErrAuthorizationFailed used at uploader
	ErrAuthorizationFailed
)

Variables

This section is empty.

Functions

func FtpAFile

func FtpAFile(ctx context.Context, where *ConnectConfig, fullfilename string, bsha1 []byte) error

FtpAFile send file to a ftp server.

func SendAFile

func SendAFile(ctx context.Context, where *ConnectConfig, fullfilename string, jar *cookiejar.Jar, bsha1 []byte) error

SendAFile sends file to a service Upload. jar holds cookies from server http.Responses and use them in http.Requests

Example
package main

import (
	"context"
	"crypto/sha1"
	"io"
	"net/http/cookiejar"
	"os"
	"time"

	"github.com/zavla/upload/uploadclient"
	"golang.org/x/net/publicsuffix"
)

func main() {
	// a jar to hold our cookies
	jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})

	// specify where to connect
	config := uploadclient.ConnectConfig{
		ToURL:    "https://127.0.0.1:64000/upload/testuser",
		Password: "testuser",
		//PasswordHash: string, // you better to use a hash of a password
		Username:           "testuser",
		InsecureSkipVerify: true, // skips certificates chain test, don't use 'true' in production, of course use a CAPool!
		//CApool: *x509.CertPool, // a root CA public certificate that signed a service's certificate

	}

	// use context to have a cancel of a long running upload process
	ctx, callmetofreeresources := context.WithDeadline(
		context.Background(),
		time.Now().Add(time.Second*10),
	)
	defer callmetofreeresources()

	const filename = "./testdata/testfile.txt"

	// compute sha1
	sha1file := sha1get(filename)

	err := uploadclient.SendAFile(ctx, &config, filename, jar, sha1file)
	if err != nil {
		println(err.Error())
		return
	}
	println("Normal OK:")
}

func sha1get(filename string) []byte {
	f, err := os.Open(filename)
	if err != nil {
		println(err)
		return nil
	}
	defer f.Close()
	sha1 := sha1.New()
	io.Copy(sha1, f)
	return sha1.Sum(nil)
}
Output:

Normal OK:

Types

type ConnectConfig

type ConnectConfig struct {
	// ToURL hold a connection URL to a service
	ToURL string

	Password string // TODO(zavla): check the cyrillic passwords
	// OR you may specify a hash
	PasswordHash string // one may already store a hash of password
	Username     string

	// Certs is used in TLSConfig for the client to identify itself.
	Certs []tls.Certificate

	// CApool holds your CA certs that this connection will use to verify its peer.
	CApool *x509.CertPool // for the client to check the servers certificates

	// InsecureSkipVerify is used in x509.TLSConfig to skip chain verification.
	// Do not use in production
	InsecureSkipVerify bool

	// DontUseFileAttribute allows scheme where there are two services that recieves the same files simultaniously.
	// But one service is a master.
	DontUseFileAttribute bool
}

ConnectConfig holds connection parameters: URL and username etc..

Jump to

Keyboard shortcuts

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