This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide download functionality for documents
Addressing: #7 Uses existing function to find a list of matching filenames, if only one entry matches, downloads it to current dir, if there are multiple matches provide an indexed list to select from
- Loading branch information
Showing
3 changed files
with
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var downloadCmd = &cobra.Command{ | ||
Use: "download", | ||
Short: "Download a Document", | ||
Long: "Download a remote document from a paperless server", | ||
|
||
Run: func(cmd *cobra.Command, args []string) { | ||
log.Debugf("Called Download with args %v", args) | ||
if len(args) < 1 { | ||
log.Debugf("Missing filename to download") | ||
} | ||
if viper.ConfigFileUsed() == "" { | ||
fmt.Println("No configuration file found! Try 'config create'") | ||
} else { | ||
fmt.Println(PaperInst) | ||
for index := range args { | ||
PaperInst.DownloadFiles(args[index]) | ||
} | ||
} | ||
log.Debug("Done downloading") | ||
}, | ||
} | ||
|
||
func init() { | ||
documentsCmd.AddCommand(downloadCmd) | ||
} |
Binary file not shown.
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,80 @@ | ||
package paperless | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var documentselection int | ||
|
||
func (p Paperless) setFullDownloadURL(location string) string { | ||
switch p.UseHTTPS { | ||
case true: | ||
return "https://" + p.Hostname + location | ||
case false: | ||
return "http://" + p.Hostname + location | ||
} | ||
return "" | ||
} | ||
|
||
func (p Paperless) writeFile(document Document) { | ||
downloadURL := p.setFullDownloadURL(document.DownloadURL) | ||
|
||
client := http.Client{Timeout: time.Second * 5} | ||
log.Debugf("downloading from: %v", downloadURL) | ||
req, _ := http.NewRequest("GET", downloadURL, nil) | ||
req.Header.Set("User-Agent", "paperless-cli") | ||
req.SetBasicAuth(p.Username, p.Password) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Errorf("Error downloading file: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
out, err := os.Create(document.FileName) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
defer out.Close() | ||
|
||
_, err = io.Copy(out, resp.Body) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
} | ||
|
||
// DownloadFiles pulls either a paperless document or allows selection from a list | ||
func (p Paperless) DownloadFiles(filename string) { | ||
documents, err := Paperless.GetDocument(p, filename, true) | ||
if err != nil { | ||
log.Errorf("Error finding file") | ||
return | ||
} | ||
if len(documents) == 0 { | ||
log.Panicln("No matches for", filename) | ||
return | ||
} | ||
if len(documents) > 1 { | ||
fmt.Println("Multiple files found. Please select from:") | ||
for index, element := range documents { | ||
fmt.Println(strconv.Itoa(index) + ": " + element.FileName) | ||
} | ||
var selection string | ||
fmt.Scanln(&selection) | ||
documentselection, err = strconv.Atoi(selection) | ||
if err != nil { | ||
log.Error("Please pass a number back for selection") | ||
return | ||
} | ||
} | ||
|
||
document := documents[documentselection] | ||
p.writeFile(document) | ||
} |