From: Sergey Matveev Date: Wed, 20 Nov 2019 12:21:44 +0000 (+0300) Subject: Initial X-Git-Url: http://www.git.stargrave.org/?p=mmfileget.git;a=commitdiff_plain;h=7a622bab122020ebab3232aafa84997b20d3189f Initial --- 7a622bab122020ebab3232aafa84997b20d3189f diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19b737d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +mattermost-server +mmfileget +vendor diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..85265b0 --- /dev/null +++ b/COPYING @@ -0,0 +1 @@ +Public domain diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..1f55245 --- /dev/null +++ b/INSTALL @@ -0,0 +1,3 @@ +Mattermost requires Go 1.13+. +Not all mattermost-server's dependencies work with go modules, +so we use their vendored versions. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..56163a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +GO ?= go + +all: mmfileget + +mmfileget: main.go mattermost-server vendor + $(GO) build -mod=vendor + +mattermost-server: + git clone --depth 1 --branch v5.11.1 https://github.com/mattermost/mattermost-server.git + +vendor: mattermost-server + ln -s mattermost-server/vendor + ln -s $(PWD)/mattermost-server vendor/github.com/mattermost/mattermost-server diff --git a/README b/README new file mode 100644 index 0000000..2b5f969 --- /dev/null +++ b/README @@ -0,0 +1,23 @@ +Utility for downloading files from Mattermost. + +Mattermost (MM) gives URLs for file downloading like +https://MACHINE/api/v4/files/FILE +and you can not download it with simple fetch/curl/wget. +That utility uses MM's API to download it. + + $ mmfileget https://MACHINE/api/v4/files/FILE + Name: Some filename.jpg + Type: image/jpeg + Size: 43713 + Download? Ctrl-D/C + +Press Ctrl-D to allow file download under provided name. +Press Ctrl-C to quit program. + + $ mmfileget https://MACHINE/api/v4/files/FILE TARGET-NAME + +will download file and save it in TARGET-NAME file. + +It uses ~/.netrc file (single line per machine format) for finding +authentication credentials for MACHINE. You can override it with NETRC +environment variable. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..37e097c --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module go.stargrave.org/mmfileget + +go 1.13 + +require github.com/mattermost/mattermost-server v5.11.1+incompatible + +replace github.com/mattermost/mattermost-server v5.11.1+incompatible => ./mattermost-server diff --git a/main.go b/main.go new file mode 100644 index 0000000..1e717ec --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/mattermost/mattermost-server/model" +) + +func main() { + url := os.Args[1] // https://MACHINE/api/v4/files/FILE + s := strings.Split(url, "/") + machine, fileId := s[2], s[6] + + netrcPath, ok := os.LookupEnv("NETRC") + if !ok { + netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc") + } + data, err := ioutil.ReadFile(netrcPath) + if err != nil { + panic(err) + } + var login string + var password string + for _, line := range strings.Split(string(data), "\n") { + if i := strings.Index(line, "#"); i >= 0 { + line = line[:i] + } + f := strings.Fields(line) + if len(f) >= 6 && + f[0] == "machine" && f[1] == machine && + f[2] == "login" && f[4] == "password" { + login, password = f[3], f[5] + break + } + } + if login == "" || password == "" { + fmt.Fprintln(os.Stderr, "No credentials found for:", machine) + os.Exit(1) + } + + Client := model.NewAPIv4Client("https://" + machine) + Client.Login(login, password) + info, _ := Client.GetFileInfo(fileId) + if info == nil { + os.Exit(1) + } + filename := info.Name + if len(os.Args) > 2 { + filename = os.Args[2] + } else { + fmt.Fprintln(os.Stderr, "Name:", info.Name) + fmt.Fprintln(os.Stderr, "Type:", info.MimeType) + fmt.Fprintln(os.Stderr, "Size:", info.Size) + fmt.Fprintln(os.Stderr, "Download? Ctrl-D/C") + os.Stdin.Read(make([]byte, 1)) + } + data, _ = Client.GetFile(fileId) + if err = ioutil.WriteFile(filename, data, os.FileMode(0666)); err != nil { + panic(err) + } +}