From 5ae269c531857a76cfd467e89891ff44f4c92437 Mon Sep 17 00:00:00 2001 From: erinn Date: Thu, 16 Dec 2021 16:40:28 -0800 Subject: [PATCH] message previews - safety checks --- .../filesharing/filesharing_functionality.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/functionality/filesharing/filesharing_functionality.go b/functionality/filesharing/filesharing_functionality.go index 26a192f..0b8510f 100644 --- a/functionality/filesharing/filesharing_functionality.go +++ b/functionality/filesharing/filesharing_functionality.go @@ -10,6 +10,7 @@ import ( "math" "os" path "path/filepath" + "regexp" "strconv" "strings" "time" @@ -54,6 +55,12 @@ func (om *OverlayMessage) FileKey() string { return fmt.Sprintf("%s.%s", om.Hash, om.Nonce) } +// checks file size and file name. *DOES NOT* check user settings or contact state +func (om *OverlayMessage) ShouldAutoDL() bool { + lname := strings.ToLower(om.Name) + return om.Size <= 20971520 && (strings.HasSuffix(lname, "jpg") || strings.HasSuffix(lname, "jpeg") || strings.HasSuffix(lname, "png") || strings.HasSuffix(lname, "gif") || strings.HasSuffix(lname, "webp") || strings.HasSuffix(lname, "bmp")) +} + // DownloadFile given a profile, a conversation handle and a file sharing key, start off a download process // to downloadFilePath func (f *Functionality) DownloadFile(profile peer.CwtchPeer, conversationID int, downloadFilePath string, manifestFilePath string, key string) { @@ -118,9 +125,26 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, conve } func GenerateDownloadPath(basePath, fileName string) (filePath, manifestPath string) { + // avoid all kina funky shit + re := regexp.MustCompile(`[^A-Za-z0-9._-]`) + filePath = re.ReplaceAllString(filePath, "") + // avoid hidden files on linux + for strings.HasPrefix(filePath, ".") { + filePath = strings.TrimPrefix(filePath, ".") + } + // avoid empties + if strings.TrimSpace(filePath) == "" { + filePath = "untitled" + } + // if you like it, put a / on it + if !strings.HasSuffix(basePath, string(os.PathSeparator)) { + basePath = fmt.Sprintf("%s%s", basePath, string(os.PathSeparator)) + } filePath = fmt.Sprintf("%s%s", basePath, fileName) manifestPath = fmt.Sprintf("%s.manifest", filePath) + // if file is named "file", iterate "file", "file (2)", "file (3)", ... until DNE + // if file is named "file.ext", iterate "file.ext", "file (2).ext", "file (3).ext", ... until DNE parts := strings.Split(fileName, ".") fileNameBase := parts[0] fileNameExt := ""