From d8bf2d3227d25081561c0d88600e8e1f9c424db3 Mon Sep 17 00:00:00 2001 From: erinn Date: Tue, 14 Dec 2021 13:21:45 -0800 Subject: [PATCH] image previews wip --- .../filesharing/filesharing_functionality.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/functionality/filesharing/filesharing_functionality.go b/functionality/filesharing/filesharing_functionality.go index 0dae38a..1cd6005 100644 --- a/functionality/filesharing/filesharing_functionality.go +++ b/functionality/filesharing/filesharing_functionality.go @@ -8,8 +8,10 @@ import ( "fmt" "io" "math" + "os" path "path/filepath" "strconv" + "strings" "time" "cwtch.im/cwtch/model" @@ -31,6 +33,13 @@ func FunctionalityGate(experimentMap map[string]bool) (*Functionality, error) { return nil, errors.New("filesharing is not enabled") } +func PreviewFunctionalityGate(experimentMap map[string]bool) (*Functionality, error) { + if experimentMap["filesharing"] == true && experimentMap["filesharing-images"] == true { + return new(Functionality), nil + } + return nil, errors.New("image previews are not enabled") +} + // OverlayMessage presents the canonical format of the File Sharing functionality Overlay Message // This is the format that the UI will parse to display the message type OverlayMessage struct { @@ -40,6 +49,10 @@ type OverlayMessage struct { Size uint64 `json:"s"` } +func (om *OverlayMessage) FileKey() string { + return fmt.Sprintf("%s.%s", om.Hash, om.Nonce) +} + // 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, handle string, downloadFilePath string, manifestFilePath string, key string) { @@ -101,3 +114,26 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, handl return nil } + +func GenerateDownloadPath(basePath, fileName string) (filePath, manifestPath string) { + filePath = fmt.Sprintf("%s%s", basePath, fileName) + manifestPath = fmt.Sprintf("%s.manifest", filePath) + + parts := strings.Split(fileName, ".") + fileNameBase := parts[0] + fileNameExt := "" + if len(parts) > 1 { + fileNameBase = strings.Join(parts[0:len(parts)-1], ".") + fileNameExt = fmt.Sprintf(".%s", parts[len(parts)-1]) + } + + for i := 2; ; i++ { + if _, err := os.Open(filePath); os.IsNotExist(err) { + if _, err := os.Open(manifestPath); os.IsNotExist(err) { + return + } + } + filePath = fmt.Sprintf("%s%s (%d)%s", basePath, fileNameBase, i, fileNameExt) + manifestPath = fmt.Sprintf("%s.manifest", filePath) + } +}