file resumption support

This commit is contained in:
erinn 2021-11-04 14:07:43 -07:00
parent 5162561b33
commit bf1a92528a
2 changed files with 22 additions and 8 deletions

View File

@ -2,19 +2,21 @@ package filesharing
import ( import (
"crypto/rand" "crypto/rand"
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/files"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"git.openprivacy.ca/openprivacy/log"
"io" "io"
"math" "math"
path "path/filepath" path "path/filepath"
"strconv" "strconv"
"time"
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/files"
"git.openprivacy.ca/openprivacy/log"
) )
// Functionality groups some common UI triggered functions for contacts... // Functionality groups some common UI triggered functions for contacts...
@ -88,6 +90,9 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, handl
// manifest.FileName gets redacted in filesharing_subsystem (to remove the system-specific file hierarchy), // manifest.FileName gets redacted in filesharing_subsystem (to remove the system-specific file hierarchy),
// but we need to *store* the full path because the sender also uses it to locate the file // but we need to *store* the full path because the sender also uses it to locate the file
lenDiff := len(filepath) - len(path.Base(filepath)) lenDiff := len(filepath) - len(path.Base(filepath))
profile.SetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.ts", key), strconv.FormatInt(time.Now().Unix(), 10))
profile.SetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest", key), string(serializedManifest))
profile.SetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest.size", key), strconv.Itoa(int(math.Ceil(float64(len(serializedManifest)-lenDiff)/float64(files.DefaultChunkSize))))) profile.SetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest.size", key), strconv.Itoa(int(math.Ceil(float64(len(serializedManifest)-lenDiff)/float64(files.DefaultChunkSize)))))
profile.ShareFile(key, string(serializedManifest)) profile.ShareFile(key, string(serializedManifest))

View File

@ -1,7 +1,6 @@
package peer package peer
import ( import (
"cwtch.im/cwtch/model/constants"
"encoding/base32" "encoding/base32"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
@ -13,6 +12,8 @@ import (
"sync" "sync"
"time" "time"
"cwtch.im/cwtch/model/constants"
"cwtch.im/cwtch/event" "cwtch.im/cwtch/event"
"cwtch.im/cwtch/model" "cwtch.im/cwtch/model"
"cwtch.im/cwtch/model/attr" "cwtch.im/cwtch/model/attr"
@ -879,6 +880,14 @@ func (cp *cwtchPeer) eventHandler() {
val, exists = cp.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name) val, exists = cp.GetScopedZonedAttribute(attr.PublicScope, attr.ProfileZone, constants.Name)
} }
if exists && zone == attr.FilesharingZone && strings.HasSuffix(zpath, ".manifest.size") {
fileKey := strings.TrimSuffix(zpath, ".manifest.size")
serializedManifest, exists2 := cp.GetScopedZonedAttribute(attr.ConversationScope, attr.FilesharingZone, fmt.Sprintf("%s.manifest", fileKey))
if exists2 {
cp.ShareFile(fileKey, serializedManifest)
}
}
resp := event.NewEvent(event.SendRetValMessageToPeer, map[event.Field]string{event.RemotePeer: onion, event.Exists: strconv.FormatBool(exists)}) resp := event.NewEvent(event.SendRetValMessageToPeer, map[event.Field]string{event.RemotePeer: onion, event.Exists: strconv.FormatBool(exists)})
resp.EventID = ev.EventID resp.EventID = ev.EventID
if exists { if exists {
@ -902,7 +911,7 @@ func (cp *cwtchPeer) eventHandler() {
manifestFilePath, exists := cp.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%v.manifest", fileKey)) manifestFilePath, exists := cp.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%v.manifest", fileKey))
if exists { if exists {
downloadFilePath, exists := cp.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fileKey) downloadFilePath, exists := cp.GetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%v.path", fileKey))
if exists { if exists {
log.Debugf("downloading manifest to %v, file to %v", manifestFilePath, downloadFilePath) log.Debugf("downloading manifest to %v, file to %v", manifestFilePath, downloadFilePath)
var manifest files.Manifest var manifest files.Manifest
@ -939,7 +948,7 @@ func (cp *cwtchPeer) eventHandler() {
} }
case event.FileDownloaded: case event.FileDownloaded:
fileKey := ev.Data[event.FileKey] fileKey := ev.Data[event.FileKey]
cp.SetAttribute(fmt.Sprintf("%s.complete", fileKey), "true") cp.SetScopedZonedAttribute(attr.LocalScope, attr.FilesharingZone, fmt.Sprintf("%s.complete", fileKey), "true")
case event.NewRetValMessageFromPeer: case event.NewRetValMessageFromPeer:
onion := ev.Data[event.RemotePeer] onion := ev.Data[event.RemotePeer]
scope := ev.Data[event.Scope] scope := ev.Data[event.Scope]