forked from bensadeh/circumflex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8f6d7b
commit d3a8b86
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package download | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path" | ||
"time" | ||
) | ||
|
||
const DownloadDir string = "circumflex/downloads" | ||
|
||
// Guess the PDF filename by extracting the last part of the download url | ||
func GuessDownloadFileName(url string) (string, error) { | ||
absoluteFilepath := path.Base(url) | ||
|
||
if absoluteFilepath == "." || absoluteFilepath == "/" { | ||
return "", fmt.Errorf("Could not guess filename, potential invalid URL!") | ||
} | ||
|
||
return absoluteFilepath, nil | ||
} | ||
|
||
func GetDownloadDir() (string, error) { | ||
dir, err := os.UserHomeDir() | ||
if err != nil { | ||
// If getting the current working directory fails as well, | ||
// the user might have greater issues than simply downloading a file | ||
dir, err = os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
} else { | ||
dir = fmt.Sprintf("%s/%s", dir, DownloadDir) | ||
} | ||
|
||
return dir, nil | ||
} | ||
|
||
// Writes to the destination file as it downloads it, without | ||
// loading the entire file into memory. | ||
func DownloadFile(url string, absoluteFilepath string) error { | ||
out, err := os.Create(absoluteFilepath) | ||
if err != nil { | ||
return fmt.Errorf("Could not create file") | ||
} | ||
defer out.Close() | ||
|
||
// Timeout download attempt after 10 seconds | ||
client := http.Client{Timeout: 10 * time.Second} | ||
|
||
resp, err := client.Get(url) | ||
if err != nil { | ||
return fmt.Errorf("Could not download file") | ||
} | ||
defer resp.Body.Close() | ||
|
||
_, err = io.Copy(out, resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("Could not write download file data to destination") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters