download

package module
v0.0.0-...-bd4c521 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2017 License: MIT Imports: 9 Imported by: 0

README

Download

Download helps with download files and keeping track of where you have downloaded them. It's designed to be embedded in another struct. Composition ahoy!

Imagine you have a list of books like this:

type Book struct {
	Name string
	Author string
	URL string
}

You may want to download the book from the URL, but also keep track of where you've downloaded it. Well, that's what this library is for.

Go get it:

$ go get github.com/codeclysm/download

And embed it in your struct

type Book struct {
	download.Resource
	Author string
}

book := Book{}
book.Name = "The silly lives of gophers"
book.Author = "G. O. Fer"
book.URL = "https://download.example.com/silly_gophers.epub"

book.Download("books", nil)

log.Println(book.Where()) // Will print out ["books"]

Download comes with a few options you may want to use:

// Opts is a struct of options to be passed to the Download function
type Opts struct {
	// Client is the http client used to fetch the resource
	Client *http.Client
	// Cache is a flag that tells the Download func not to download twice the resource in the same location
	Cache bool
	// Sha256Sum is the sum that's used to check that the download was correct
	Sha256Sum string
	// Handler is the function that saves or extracts the resource downloaded
	Handler func(body io.Reader, name, location string) error
}

Check out the documentation (I just love godoc): https://godoc.org/github.com/codeclysm/download

Documentation

Overview

Package download helps with download files and keeping track of where you have downloaded them. It's designed to be embedded in another struct. Composition ahoy!

Example
package main

import (
	"fmt"

	download "github.com/codeclysm/downloader"
)

func main() {
	img := struct {
		download.Resource
		Author string
	}{}

	img.Name = "Mona Lisa"
	img.Author = "Leonardo da Vinci"
	img.URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/Mona_Lisa.jpg/396px-Mona_Lisa.jpg"

	fmt.Println(img.Download("paintings", nil))
	fmt.Println(img.Where())
}
Output:

<nil>
[paintings]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opts

type Opts struct {
	// Client is the http client used to fetch the resource
	Client *http.Client
	// Cache is a flag that tells the Download func not to download twice the resource in the same location
	Cache bool
	// Sha256Sum is the sum that's used to check that the download was correct
	Sha256Sum string
	// Handler is the function that saves or extracts the resource downloaded
	Handler func(body io.Reader, name, location string) error
}

Opts is a struct of options to be passed to the Download function

type Resource

type Resource struct {
	URL  string
	Name string
	// contains filtered or unexported fields
}

Resource is an embeddable struct that keep tracks of where a resource is being downloaded.

func (*Resource) Download

func (r *Resource) Download(location string, opts *Opts) error

Download will retrieve the resource at .URL and save it on disk. Its behaviour can be modified with some options

func (*Resource) Where

func (r *Resource) Where() []string

Where returns a list of all the places where the resource was downloaded to

Jump to

Keyboard shortcuts

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