| 1 |
package msgview |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/app" |
| 13 |
"git.sr.ht/~rjarry/aerc/commands" |
| 14 |
"git.sr.ht/~rjarry/aerc/config" |
| 15 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 17 |
"git.sr.ht/~rjarry/aerc/models" |
| 18 |
) |
| 19 |
|
| 20 |
type Save struct { |
| 21 |
Force bool `opt:"-f" desc:"Overwrite destination path."` |
| 22 |
CreateDirs bool `opt:"-p" desc:"Create missing directories."` |
| 23 |
Attachments bool `opt:"-a" desc:"Save all attachments parts."` |
| 24 |
AllAttachments bool `opt:"-A" desc:"Save all named parts."` |
| 25 |
Path string `opt:"path" required:"false" complete:"CompletePath" desc:"Target file path."` |
| 26 |
} |
| 27 |
|
| 28 |
func init() { |
| 29 |
commands.Register(Save{}) |
| 30 |
} |
| 31 |
|
| 32 |
func (Save) Description() string { |
| 33 |
return "Save the current message part to the given path." |
| 34 |
} |
| 35 |
|
| 36 |
func (Save) Context() commands.CommandContext { |
| 37 |
return commands.MESSAGE_VIEWER |
| 38 |
} |
| 39 |
|
| 40 |
func (Save) Aliases() []string { |
| 41 |
return []string{"save"} |
| 42 |
} |
| 43 |
|
| 44 |
func (*Save) CompletePath(arg string) []string { |
| 45 |
defaultPath := config.General().DefaultSavePath |
| 46 |
if defaultPath != "" && !isAbsPath(arg) { |
| 47 |
arg = filepath.Join(defaultPath, arg) |
| 48 |
} |
| 49 |
return commands.CompletePath(arg, false) |
| 50 |
} |
| 51 |
|
| 52 |
func (s Save) Execute(args []string) error { |
| 53 |
// we either need a path or a defaultPath |
| 54 |
if s.Path == "" && config.General().DefaultSavePath == "" { |
| 55 |
return errors.New("No default save path in config") |
| 56 |
} |
| 57 |
|
| 58 |
// Absolute paths are taken as is so that the user can override the default |
| 59 |
// if they want to |
| 60 |
if !isAbsPath(s.Path) { |
| 61 |
s.Path = filepath.Join(config.General().DefaultSavePath, s.Path) |
| 62 |
} |
| 63 |
|
| 64 |
s.Path = xdg.ExpandHome(s.Path) |
| 65 |
|
| 66 |
mv, ok := app.SelectedTabContent().(*app.MessageViewer) |
| 67 |
if !ok { |
| 68 |
return fmt.Errorf("SelectedTabContent is not a MessageViewer") |
| 69 |
} |
| 70 |
|
| 71 |
if s.Attachments || s.AllAttachments { |
| 72 |
parts := mv.AttachmentParts(s.AllAttachments) |
| 73 |
if len(parts) == 0 { |
| 74 |
return fmt.Errorf("This message has no attachments") |
| 75 |
} |
| 76 |
names := make(map[string]struct{}) |
| 77 |
for _, pi := range parts { |
| 78 |
if err := s.savePart(pi, mv, names); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
} |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
pi := mv.SelectedMessagePart() |
| 86 |
return s.savePart(pi, mv, make(map[string]struct{})) |
| 87 |
} |
| 88 |
|
| 89 |
func (s *Save) savePart( |
| 90 |
pi *app.PartInfo, |
| 91 |
mv *app.MessageViewer, |
| 92 |
names map[string]struct{}, |
| 93 |
) error { |
| 94 |
path := s.Path |
| 95 |
if s.Attachments || s.AllAttachments || isDirExists(path) { |
| 96 |
filename := generateFilename(pi.Part) |
| 97 |
path = filepath.Join(path, filename) |
| 98 |
} |
| 99 |
|
| 100 |
dir := filepath.Dir(path) |
| 101 |
if s.CreateDirs && dir != "" { |
| 102 |
err := os.MkdirAll(dir, 0o755) |
| 103 |
if err != nil { |
| 104 |
return err |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
path = getCollisionlessFilename(path, names) |
| 109 |
names[path] = struct{}{} |
| 110 |
|
| 111 |
if pathExists(path) && !s.Force { |
| 112 |
return fmt.Errorf("%q already exists and -f not given", path) |
| 113 |
} |
| 114 |
|
| 115 |
ch := make(chan error, 1) |
| 116 |
mv.MessageView().FetchBodyPart(pi.Index, func(reader io.Reader) { |
| 117 |
f, err := os.Create(path) |
| 118 |
if err != nil { |
| 119 |
ch <- err |
| 120 |
return |
| 121 |
} |
| 122 |
defer f.Close() |
| 123 |
_, err = io.Copy(f, reader) |
| 124 |
if err != nil { |
| 125 |
ch <- err |
| 126 |
return |
| 127 |
} |
| 128 |
ch <- nil |
| 129 |
}) |
| 130 |
|
| 131 |
// we need to wait for the callback prior to displaying a result |
| 132 |
go func() { |
| 133 |
defer log.PanicHandler() |
| 134 |
|
| 135 |
err := <-ch |
| 136 |
if err != nil { |
| 137 |
app.PushError(fmt.Sprintf("Save failed: %v", err)) |
| 138 |
return |
| 139 |
} |
| 140 |
app.PushStatus("Saved to "+path, 10*time.Second) |
| 141 |
}() |
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
func getCollisionlessFilename(path string, existing map[string]struct{}) string { |
| 146 |
ext := filepath.Ext(path) |
| 147 |
name := strings.TrimSuffix(path, ext) |
| 148 |
_, exists := existing[path] |
| 149 |
counter := 1 |
| 150 |
for exists { |
| 151 |
path = fmt.Sprintf("%s_%d%s", name, counter, ext) |
| 152 |
counter++ |
| 153 |
_, exists = existing[path] |
| 154 |
} |
| 155 |
return path |
| 156 |
} |
| 157 |
|
| 158 |
// isDir returns true if path is a directory and exists |
| 159 |
func isDirExists(path string) bool { |
| 160 |
pathinfo, err := os.Stat(path) |
| 161 |
if err != nil { |
| 162 |
return false // we don't really care |
| 163 |
} |
| 164 |
if pathinfo.IsDir() { |
| 165 |
return true |
| 166 |
} |
| 167 |
return false |
| 168 |
} |
| 169 |
|
| 170 |
// pathExists returns true if path exists |
| 171 |
func pathExists(path string) bool { |
| 172 |
_, err := os.Stat(path) |
| 173 |
|
| 174 |
return err == nil |
| 175 |
} |
| 176 |
|
| 177 |
// isAbsPath returns true if path given is anchored to / or . or ~ |
| 178 |
func isAbsPath(path string) bool { |
| 179 |
if len(path) == 0 { |
| 180 |
return false |
| 181 |
} |
| 182 |
switch path[0] { |
| 183 |
case '/': |
| 184 |
return true |
| 185 |
case '.': |
| 186 |
return true |
| 187 |
case '~': |
| 188 |
return true |
| 189 |
default: |
| 190 |
return false |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// generateFilename tries to get the filename from the given part. |
| 195 |
// if that fails it will fallback to a generated one based on the date |
| 196 |
func generateFilename(part *models.BodyStructure) string { |
| 197 |
filename := part.FileName() |
| 198 |
// Some MUAs send attachments with names like /some/stupid/idea/happy.jpeg |
| 199 |
// Assuming non hostile intent it does make sense to use just the last |
| 200 |
// portion of the pathname as the filename for saving it. |
| 201 |
filename = filename[strings.LastIndex(filename, "/")+1:] |
| 202 |
switch filename { |
| 203 |
case "", ".", "..": |
| 204 |
timestamp := time.Now().Format("2006-01-02-150405") |
| 205 |
filename = fmt.Sprintf("aerc_%v", timestamp) |
| 206 |
default: |
| 207 |
// already have a valid name |
| 208 |
} |
| 209 |
return filename |
| 210 |
} |