Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Provide download functionality for documents
Browse files Browse the repository at this point in the history
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
Zibby committed Feb 16, 2020
1 parent 47b3197 commit 5c3e1f9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/download.go
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 added paperless-cli
Binary file not shown.
80 changes: 80 additions & 0 deletions paperless/downloadfile.go
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)
}

0 comments on commit 5c3e1f9

Please sign in to comment.