From f3ac8c0098636356951015dc24b1b201e0420439 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 3 Feb 2022 14:39:52 -0800 Subject: [PATCH] Allow Sharing Public Profile Images --- event/common.go | 15 +++++----- .../filesharing/filesharing_functionality.go | 30 +++++++++++-------- model/constants/attributes.go | 2 ++ peer/cwtch_peer.go | 3 +- protocol/connections/engine.go | 1 - protocol/files/filesharing_subsystem.go | 28 +++++++++-------- .../file_sharing_integration_test.go | 2 +- 7 files changed, 46 insertions(+), 35 deletions(-) diff --git a/event/common.go b/event/common.go index 83b83fe..d0ab3ad 100644 --- a/event/common.go +++ b/event/common.go @@ -279,13 +279,14 @@ const ( Source = Field("Source") - FileKey = Field("FileKey") - FileSizeInChunks = Field("FileSizeInChunks") - ManifestSize = Field("ManifestSize") - SerializedManifest = Field("SerializedManifest") - TempFile = Field("TempFile") - FilePath = Field("FilePath") - NameSuggestion = Field("NameSuggestion") + FileKey = Field("FileKey") + FileSizeInChunks = Field("FileSizeInChunks") + ManifestSize = Field("ManifestSize") + SerializedManifest = Field("SerializedManifest") + TempFile = Field("TempFile") + FilePath = Field("FilePath") + FileDownloadFinished = Field("FileDownloadFinished") + NameSuggestion = Field("NameSuggestion") ) // Defining Common errors diff --git a/functionality/filesharing/filesharing_functionality.go b/functionality/filesharing/filesharing_functionality.go index baac637..9b03864 100644 --- a/functionality/filesharing/filesharing_functionality.go +++ b/functionality/filesharing/filesharing_functionality.go @@ -88,16 +88,16 @@ func (f *Functionality) DownloadFile(profile peer.CwtchPeer, conversationID int, // ShareFile given a profile and a conversation handle, sets up a file sharing process to share the file // at filepath -func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, conversationID int) error { +func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, conversationID int) (string, error) { manifest, err := files.CreateManifest(filepath) if err != nil { - return err + return "", err } var nonce [24]byte if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil { log.Errorf("Cannot read from random: %v\n", err) - return err + return "", err } message := OverlayMessage{ @@ -138,13 +138,16 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, conve profile.ShareFile(key, string(serializedManifest)) - profile.SendMessage(conversationID, string(wrapperJSON)) + // non-specific conversation + if conversationID != -1 { + err = profile.SendMessage(conversationID, string(wrapperJSON)) + } - return nil + return key, err } // GenerateDownloadPath creates a file path that doesn't currently exist on the filesystem -func GenerateDownloadPath(basePath, fileName string) (filePath, manifestPath string) { +func GenerateDownloadPath(basePath, fileName string, overwrite bool) (filePath, manifestPath string) { // avoid all kina funky shit re := regexp.MustCompile(`[^A-Za-z0-9._-]`) filePath = re.ReplaceAllString(filePath, "") @@ -173,13 +176,16 @@ func GenerateDownloadPath(basePath, fileName string) (filePath, manifestPath str 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 + if !overwrite { + 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) } - filePath = fmt.Sprintf("%s%s (%d)%s", basePath, fileNameBase, i, fileNameExt) - manifestPath = fmt.Sprintf("%s.manifest", filePath) } + return } diff --git a/model/constants/attributes.go b/model/constants/attributes.go index d3385a6..d74dc48 100644 --- a/model/constants/attributes.go +++ b/model/constants/attributes.go @@ -49,3 +49,5 @@ const AttrRejected = "rejected-invite" // AttrDownloaded - conversation attribute for storing downloaded prompts (for file downloads) const AttrDownloaded = "file-downloaded" + +const CustomProfileImageKey = "custom-profile-image" diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 3263436..17ff613 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -1048,7 +1048,8 @@ func (cp *cwtchPeer) eventHandler() { success, dgm := group.AttemptDecryption(ciphertext, signature) if success { // Time to either acknowledge the message or insert a new message - cp.attemptInsertOrAcknowledgeLegacyGroupConversation(conversationInfo.ID, ev.Data[event.Signature], dgm) + // Re-encode signature to base64 + cp.attemptInsertOrAcknowledgeLegacyGroupConversation(conversationInfo.ID, base64.StdEncoding.EncodeToString(signature), dgm) break } } diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 52d8f18..11af969 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -221,7 +221,6 @@ func (e *engine) eventHandler() { key := ev.Data[event.FileKey] size, _ := strconv.Atoi(ev.Data[event.ManifestSize]) if err := e.sendPeerMessage(handle, e.filesharingSubSystem.FetchManifest(key, uint64(size))); err != nil { - log.Errorf("error sending manifest: %v", err) e.eventManager.Publish(event.NewEvent(event.SendMessageToPeerError, map[event.Field]string{event.RemotePeer: ev.Data[event.RemotePeer], event.EventID: ev.EventID, event.Error: err.Error()})) } case event.ManifestSaved: diff --git a/protocol/files/filesharing_subsystem.go b/protocol/files/filesharing_subsystem.go index 1a22cd3..5f71198 100644 --- a/protocol/files/filesharing_subsystem.go +++ b/protocol/files/filesharing_subsystem.go @@ -82,7 +82,7 @@ func (fsss *FileSharingSubSystem) RequestManifestParts(fileKey string) []model.P if exists { oldManifest := manifestI.(*Manifest) serializedOldManifest := oldManifest.Serialize() - log.Debugf("found serialized manifest: %s", serializedOldManifest) + log.Debugf("found serialized manifest") // copy so we dont get threading issues by modifying the original // and then redact the file path before sending @@ -130,20 +130,22 @@ func (fsss *FileSharingSubSystem) ReceiveManifestPart(manifestKey string, part [ log.Debugf("storing manifest part %v %v", offset, end) serializedManifestBytes := []byte(serializedManifest) - copy(serializedManifestBytes[offset:end], part[:]) + if len(serializedManifestBytes) > offset && len(serializedManifestBytes) >= end { + copy(serializedManifestBytes[offset:end], part[:]) - if len(part) < DefaultChunkSize { - serializedManifestBytes = serializedManifestBytes[0 : len(serializedManifestBytes)-(DefaultChunkSize-len(part))] - } + if len(part) < DefaultChunkSize { + serializedManifestBytes = serializedManifestBytes[0 : len(serializedManifestBytes)-(DefaultChunkSize-len(part))] + } - serializedManifest = string(serializedManifestBytes) - fsss.prospectiveManifests.Store(fileKey, serializedManifest) - log.Debugf("current manifest: [%s]", serializedManifest) - var manifest Manifest - err := json.Unmarshal([]byte(serializedManifest), &manifest) - if err == nil && hex.EncodeToString(manifest.RootHash) == fileKeyParts[0] { - log.Debugf("valid manifest received! %x", manifest.RootHash) - return fileKey, serializedManifest + serializedManifest = string(serializedManifestBytes) + fsss.prospectiveManifests.Store(fileKey, serializedManifest) + log.Debugf("current manifest: [%s]", serializedManifest) + var manifest Manifest + err := json.Unmarshal([]byte(serializedManifest), &manifest) + if err == nil && hex.EncodeToString(manifest.RootHash) == fileKeyParts[0] { + log.Debugf("valid manifest received! %x", manifest.RootHash) + return fileKey, serializedManifest + } } } } diff --git a/testing/filesharing/file_sharing_integration_test.go b/testing/filesharing/file_sharing_integration_test.go index 7eddc64..c8e0afd 100644 --- a/testing/filesharing/file_sharing_integration_test.go +++ b/testing/filesharing/file_sharing_integration_test.go @@ -132,7 +132,7 @@ func TestFileSharing(t *testing.T) { filesharingFunctionality, _ := filesharing.FunctionalityGate(map[string]bool{constants.FileSharingExperiment: true}) - err = filesharingFunctionality.ShareFile("cwtch.png", alice, 1) + _, err = filesharingFunctionality.ShareFile("cwtch.png", alice, 1) if err != nil { t.Fatalf("Error!: %v", err)