Giter VIP home page Giter VIP logo

filedownloader's Introduction

filedownloader

Download File from Internet

Multipurpose File Downloader for Go

  • Easy to download files from internet
  • Progress Channel to receive downloading progress
  • Multiple Download with Multiple Thread
  • Able to configure Logging Function

FUSO

Library project code is FUSO.

File Util for Simple Object

Import

import github.com/chixm/filedownloader

How to use

Example1 Most Simple Usage. Download URL file to local File Example.

	fdl := filedownloader.New(nil)
	user, _ := user.Current()
	err := fdl.SimpleFileDownload(`https://golang.org/pkg/net/http/`, user.HomeDir+`/fuso.html`)
	if err != nil {
		log.Println(err)
	}

Example2: Multiple Files Download From Internet

Simply, put Url and local file path to Download structure slice and call MultipleFileDownload

	fdl := New(nil)
	user, _ := user.Current()
	// Download Progress Observer
	var downloadFiles []*Download
	downloadFiles = append(downloadFiles, &Download{URL: `https://files.hareruyamtg.com/img/goods/L/M21/EN/0001.jpg`, LocalFilePath: user.HomeDir + `/ugin.jpg`})
	downloadFiles = append(downloadFiles, &Download{URL: `https://files.hareruyamtg.com/img/goods/L/ELD/EN/BRAWL0329.jpg`, LocalFilePath: user.HomeDir + `/korvold.jpg`})
	err := fdl.MultipleFileDownload(downloadFiles)
	if err != nil {
		t.Error(err)
	}

Self defined Log functions

FUSO writes log by Golang "log" library by default. You can configure Config and set your own logger.

conf := Config{logfunc: myLogger}
	fileDownloader := New(&conf)
	// downloading to use home
	user, _ := user.Current()
	fileDownloader.SimpleFileDownload(`https://golang.org/pkg/net/http/`, user.HomeDir+`/fuso.html`)

...

func myLogger(params ...interface{}) {
	log.Println(`log prefix`, params)
}

Configure RequiresDetailProgress And Receive Progress

You can show downloading progress and downloading speed if you need. Set RequiresDetailProgress: true in Config and write channel receives progress data.

See example below,

	// default setting of RequiresDetailProgress is false, you need to set it true if you need download progress.
	conf := Config{logfunc: myLogger, MaxDownloadThreads: 1, DownloadTimeoutMinutes: 3, MaxRetry: 3, RequiresDetailProgress: true}
	fileDownloader := New(&conf)

	done := make(chan int)
	// if you set RequiresDetailProgress = true, you can receive progress from channel
	go func() {
	LOOP:
		for {
			select {
			case speed := <-fileDownloader.DownloadBytesPerSecond:
				// DownloadBytesPerSecond Channel can receive how fast the download is running.
				log.Println(fmt.Sprintf(`%d bytes/sec`, speed))
			case progress := <-fileDownloader.ProgressChan:
				// Progress Channel (ProgressChan) receives how much download has progressed.
				log.Println(fmt.Sprintf(`%f percent has done`, progress)) // ex. 10.5 percent has done
			case <-done:
				break LOOP // escape from forever loop
			}
		}
	}()

	// downloading file to use home directory
	user, _ := user.Current()
	// test download file 512MB
	err := fileDownloader.SimpleFileDownload(`http://ipv4.download.thinkbroadband.com/512MB.zip`, user.HomeDir+`/512.zip`)
	if err != nil {
		done <- 1
	}
	if fileDownloader.err != nil {
		log.Println(fileDownloader.err)
	}
	done <- 0

filedownloader's People

Contributors

chixm avatar

Watchers

 avatar  avatar

Forkers

sysgoblin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.