Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mgomes committed Oct 28, 2020
0 parents commit d7fdde8
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fetch
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Mauricio Gomes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Fetch (WIP)

Can potentially increase your download speeds by downloading files with concurrent streams.

Written as a proof of concept to compare against the Crystal version of the same utility: https://github.com/mgomes/grab
90 changes: 90 additions & 0 deletions fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"flag"
"fmt"
"io"
"net/http"
"os"
"strconv"
"mime"
"strings"
)

func main() {
filenamePtr := flag.String("filename", "", "custom filename")
// boostPtr := flag.Int("boost", 8, "number of concurrent downloads")

flag.Parse()

uri := flag.Args()[0]

var filesize uint64
var filename string
var err error

if *filenamePtr == "" {
filesize, filename, err = FetchMetadata(uri)
if err != nil {
panic(err)
}
}

fmt.Println(filesize)
fmt.Println(filename)

err = Fetch(filename, uri)
if err != nil {
panic(err)
}

}

func FetchMetadata(uri string) (filesize uint64, filename string, err error) {
resp, err := http.Head(uri)
if err != nil {
return
}
defer resp.Body.Close()

contentLength := resp.Header.Get("Content-Length")
filesize, err = strconv.ParseUint(contentLength, 0, 64)
if err != nil {
return
}

contentDisposition := resp.Header.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(contentDisposition)
if err != nil {
return
}
filename = params["filename"]

// No filename specified in the header; use the pathname
if filename == "" {
splitUri := strings.Split(uri, "/")
filename = splitUri[len(splitUri)-1]
}

return
}

func Fetch(filepath string, uri string) (err error) {
// Get the data
resp, err := http.Get(uri)
if err != nil {
return
}
defer resp.Body.Close()

// Create the file
out, err := os.Create(filepath)
if err != nil {
return
}
defer out.Close()

// Write the body to file
_, err = io.Copy(out, resp.Body)
return
}

0 comments on commit d7fdde8

Please sign in to comment.