2019-05-26 21:37:39 +00:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-25 07:23:52 +00:00
|
|
|
"fmt"
|
2019-05-26 21:37:39 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2019-06-11 18:26:13 +00:00
|
|
|
"path/filepath"
|
2019-06-15 10:28:03 +00:00
|
|
|
"strings"
|
2019-05-26 21:37:39 +00:00
|
|
|
"time"
|
|
|
|
|
2019-06-11 18:26:13 +00:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-05-26 21:37:39 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-09-20 16:16:29 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-05-26 21:37:39 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 17:33:11 +00:00
|
|
|
type Save struct{}
|
|
|
|
|
2019-05-26 21:37:39 +00:00
|
|
|
func init() {
|
2019-06-27 17:33:11 +00:00
|
|
|
register(Save{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Save) Aliases() []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return []string{"save"}
|
2019-05-26 21:37:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Save) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-09-20 16:16:29 +00:00
|
|
|
path := strings.Join(args, " ")
|
|
|
|
return commands.CompletePath(path)
|
2019-06-27 17:33:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Save) Execute(aerc *widgets.Aerc, args []string) error {
|
2020-01-19 19:22:56 +00:00
|
|
|
opts, optind, err := getopt.Getopts(args, "fp")
|
2019-06-11 18:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-26 21:37:39 +00:00
|
|
|
}
|
2019-06-27 17:33:11 +00:00
|
|
|
|
2019-06-11 18:26:13 +00:00
|
|
|
var (
|
2020-01-19 19:22:56 +00:00
|
|
|
force bool
|
|
|
|
createDirs bool
|
|
|
|
trailingSlash bool
|
2019-06-11 18:26:13 +00:00
|
|
|
)
|
2019-06-27 17:33:11 +00:00
|
|
|
|
2019-06-11 18:26:13 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
2020-01-19 19:22:56 +00:00
|
|
|
case 'f':
|
|
|
|
force = true
|
2019-06-11 18:26:13 +00:00
|
|
|
case 'p':
|
2020-01-19 19:22:56 +00:00
|
|
|
createDirs = true
|
2019-06-11 18:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 19:22:56 +00:00
|
|
|
|
|
|
|
defaultPath := aerc.Config().General.DefaultSavePath
|
|
|
|
// we either need a path or a defaultPath
|
|
|
|
if defaultPath == "" && len(args) == optind {
|
|
|
|
return errors.New("Usage: :save [-fp] <path>")
|
2019-06-11 18:26:13 +00:00
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
// as a convenience we join with spaces, so that the user doesn't need to
|
|
|
|
// quote filenames containing spaces
|
|
|
|
path := strings.Join(args[optind:], " ")
|
|
|
|
|
|
|
|
// needs to be determined prior to calling filepath.Clean / filepath.Join
|
|
|
|
// it gets stripped by Clean.
|
|
|
|
// we auto generate a name if a directory was given
|
|
|
|
if len(path) > 0 {
|
|
|
|
trailingSlash = path[len(path)-1] == '/'
|
|
|
|
} else if len(defaultPath) > 0 && len(path) == 0 {
|
|
|
|
// empty path, so we might have a default that ends in a trailingSlash
|
|
|
|
trailingSlash = defaultPath[len(defaultPath)-1] == '/'
|
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
// Absolute paths are taken as is so that the user can override the default
|
|
|
|
// if they want to
|
|
|
|
if !isAbsPath(path) {
|
|
|
|
path = filepath.Join(defaultPath, path)
|
|
|
|
}
|
2019-06-15 10:28:03 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
path, err = homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
mv, ok := aerc.SelectedTab().(*widgets.MessageViewer)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("SelectedTab is not a MessageViewer")
|
|
|
|
}
|
|
|
|
pi := mv.SelectedMessagePart()
|
|
|
|
|
|
|
|
if trailingSlash || isDirExists(path) {
|
|
|
|
filename := generateFilename(pi.Part)
|
|
|
|
path = filepath.Join(path, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
if createDirs && dir != "" {
|
|
|
|
err := os.MkdirAll(dir, 0755)
|
2019-05-26 21:37:39 +00:00
|
|
|
if err != nil {
|
2020-01-19 19:22:56 +00:00
|
|
|
return err
|
2019-05-26 21:37:39 +00:00
|
|
|
}
|
2020-01-19 19:22:56 +00:00
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
if pathExists(path) && !force {
|
|
|
|
return fmt.Errorf("%q already exists and -f not given", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, 1)
|
2020-03-06 15:33:44 +00:00
|
|
|
store := mv.Store()
|
2020-05-17 09:44:38 +00:00
|
|
|
store.FetchBodyPart(pi.Msg.Uid, pi.Index, func(reader io.Reader) {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, reader)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- nil
|
|
|
|
})
|
2019-06-11 18:26:13 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
// we need to wait for the callback prior to displaying a result
|
|
|
|
go func() {
|
|
|
|
err := <-ch
|
2019-05-26 21:37:39 +00:00
|
|
|
if err != nil {
|
2020-05-28 14:32:42 +00:00
|
|
|
aerc.PushError(fmt.Sprintf("Save failed: %v", err))
|
2019-05-26 21:37:39 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-28 14:32:32 +00:00
|
|
|
aerc.PushStatus("Saved to "+path, 10*time.Second)
|
2020-01-19 19:22:56 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
//isDir returns true if path is a directory and exists
|
|
|
|
func isDirExists(path string) bool {
|
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false // we don't really care
|
|
|
|
}
|
|
|
|
if pathinfo.IsDir() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
//pathExists returns true if path exists
|
|
|
|
func pathExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
2022-03-09 21:48:00 +00:00
|
|
|
|
|
|
|
return err == nil
|
2020-01-19 19:22:56 +00:00
|
|
|
}
|
2019-05-26 21:37:39 +00:00
|
|
|
|
2020-01-19 19:22:56 +00:00
|
|
|
//isAbsPath returns true if path given is anchored to / or . or ~
|
|
|
|
func isAbsPath(path string) bool {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch path[0] {
|
|
|
|
case '/':
|
|
|
|
return true
|
|
|
|
case '.':
|
|
|
|
return true
|
|
|
|
case '~':
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateFilename tries to get the filename from the given part.
|
|
|
|
// if that fails it will fallback to a generated one based on the date
|
|
|
|
func generateFilename(part *models.BodyStructure) string {
|
|
|
|
var filename string
|
|
|
|
if fn, ok := part.DispositionParams["filename"]; ok {
|
|
|
|
filename = fn
|
|
|
|
} else if fn, ok := part.Params["name"]; ok {
|
|
|
|
filename = fn
|
2021-01-12 20:58:54 +00:00
|
|
|
}
|
|
|
|
// Some MUAs send attachments with names like /some/stupid/idea/happy.jpeg
|
|
|
|
// Assuming non hostile intent it does make sense to use just the last
|
|
|
|
// portion of the pathname as the filename for saving it.
|
|
|
|
filename = filename[strings.LastIndex(filename, "/")+1:]
|
|
|
|
switch filename {
|
|
|
|
case "", ".", "..":
|
2020-01-19 19:22:56 +00:00
|
|
|
timestamp := time.Now().Format("2006-01-02-150405")
|
|
|
|
filename = fmt.Sprintf("aerc_%v", timestamp)
|
2021-01-12 20:58:54 +00:00
|
|
|
default:
|
|
|
|
// already have a valid name
|
2020-01-19 19:22:56 +00:00
|
|
|
}
|
|
|
|
return filename
|
2019-05-26 21:37:39 +00:00
|
|
|
}
|