message previews - safety checks
This commit is contained in:
parent
9d34b7ef57
commit
5ae269c531
|
@ -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 := ""
|
||||
|
|
Loading…
Reference in New Issue