Allow Sharing Public Profile Images
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2022-02-03 14:39:52 -08:00
parent 5dc0579075
commit f3ac8c0098
7 changed files with 46 additions and 35 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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"

View File

@ -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
}
}

View File

@ -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:

View File

@ -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
}
}
}
}

View File

@ -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)