Split download progress and verification
This commit is contained in:
parent
c728987259
commit
8e9ed132d8
|
@ -257,6 +257,7 @@ const (
|
||||||
ManifestSaved = Type("ManifestSaved")
|
ManifestSaved = Type("ManifestSaved")
|
||||||
FileDownloadProgressUpdate = Type("FileDownloadProgressUpdate")
|
FileDownloadProgressUpdate = Type("FileDownloadProgressUpdate")
|
||||||
FileDownloaded = Type("FileDownloaded")
|
FileDownloaded = Type("FileDownloaded")
|
||||||
|
FileVerificationFailed = Type("FileVerificationFailed")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Field defines common event attributes
|
// Field defines common event attributes
|
||||||
|
|
|
@ -540,13 +540,18 @@ func (e *engine) handlePeerMessage(hostname string, eventID string, context stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if context == event.ContextSendFile {
|
} else if context == event.ContextSendFile {
|
||||||
fileKey, downloaded, progress, totalChunks, _ := e.filesharingSubSystem.ProcessChunk(eventID, message)
|
fileKey, progress, totalChunks, _ := e.filesharingSubSystem.ProcessChunk(eventID, message)
|
||||||
if len(fileKey) != 0 {
|
if len(fileKey) != 0 {
|
||||||
if downloaded {
|
|
||||||
log.Debugf("file verified and downloaded!")
|
|
||||||
e.eventManager.Publish(event.NewEvent(event.FileDownloaded, map[event.Field]string{event.FileKey: fileKey}))
|
|
||||||
}
|
|
||||||
e.eventManager.Publish(event.NewEvent(event.FileDownloadProgressUpdate, map[event.Field]string{event.FileKey: fileKey, event.Progress: strconv.Itoa(int(progress)), event.FileSizeInChunks: strconv.Itoa(int(totalChunks))}))
|
e.eventManager.Publish(event.NewEvent(event.FileDownloadProgressUpdate, map[event.Field]string{event.FileKey: fileKey, event.Progress: strconv.Itoa(int(progress)), event.FileSizeInChunks: strconv.Itoa(int(totalChunks))}))
|
||||||
|
if progress == totalChunks {
|
||||||
|
if e.filesharingSubSystem.VerifyFile(fileKey) {
|
||||||
|
log.Debugf("file verified and downloaded!")
|
||||||
|
e.eventManager.Publish(event.NewEvent(event.FileDownloaded, map[event.Field]string{event.FileKey: fileKey}))
|
||||||
|
} else {
|
||||||
|
log.Debugf("file failed to verify!")
|
||||||
|
e.eventManager.Publish(event.NewEvent(event.FileVerificationFailed, map[event.Field]string{event.FileKey: fileKey}))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fall through handler for the default text conversation.
|
// Fall through handler for the default text conversation.
|
||||||
|
|
|
@ -169,9 +169,8 @@ func (fsss *FileSharingSubSystem) ProcessChunkRequest(fileKey string, serialized
|
||||||
// Always return the progress of a matched download if it exists along with the total number of chunks and the
|
// Always return the progress of a matched download if it exists along with the total number of chunks and the
|
||||||
// given chunk ID
|
// given chunk ID
|
||||||
// If not such active download exists then return an empty file key and ignore all further processing.
|
// If not such active download exists then return an empty file key and ignore all further processing.
|
||||||
func (fsss *FileSharingSubSystem) ProcessChunk(chunkKey string, chunk []byte) (fileKey string, downloaded bool, progress uint64, totalChunks uint64, chunkID uint64) {
|
func (fsss *FileSharingSubSystem) ProcessChunk(chunkKey string, chunk []byte) (fileKey string, progress uint64, totalChunks uint64, chunkID uint64) {
|
||||||
fileKeyParts := strings.Split(chunkKey, ".")
|
fileKeyParts := strings.Split(chunkKey, ".")
|
||||||
downloaded = false
|
|
||||||
log.Debugf("got chunk for %s", fileKeyParts)
|
log.Debugf("got chunk for %s", fileKeyParts)
|
||||||
if len(fileKeyParts) == 3 { // fileKey is rootHash.nonce.chunk
|
if len(fileKeyParts) == 3 { // fileKey is rootHash.nonce.chunk
|
||||||
// recalculate file key
|
// recalculate file key
|
||||||
|
@ -187,23 +186,30 @@ func (fsss *FileSharingSubSystem) ProcessChunk(chunkKey string, chunk []byte) (f
|
||||||
log.Debugf("found active manifest %v", manifest)
|
log.Debugf("found active manifest %v", manifest)
|
||||||
progress, err = manifest.StoreChunk(chunkID, chunk)
|
progress, err = manifest.StoreChunk(chunkID, chunk)
|
||||||
log.Debugf("attempts to store chunk %v %v", progress, err)
|
log.Debugf("attempts to store chunk %v %v", progress, err)
|
||||||
// malicious contacts who share conversations can share random chunks
|
if err != nil {
|
||||||
// these will not match the chunk hash and as such will fail.
|
// malicious contacts who share conversations can share random chunks
|
||||||
// at this point we can't differentiate between a malicious chunk and failure to store a
|
// these will not match the chunk hash and as such will fail.
|
||||||
// legitimate chunk, so if there is an error we silently drop it and expect the higher level callers (e.g. the ui)
|
// at this point we can't differentiate between a malicious chunk and failure to store a
|
||||||
//to detect and respond to missing chunks if it detects them..
|
// legitimate chunk, so if there is an error we silently drop it and expect the higher level callers (e.g. the ui)
|
||||||
if err == nil {
|
//to detect and respond to missing chunks if it detects them..
|
||||||
if progress == totalChunks {
|
|
||||||
if manifest.VerifyFile() == nil {
|
|
||||||
manifest.Close()
|
|
||||||
fsss.activeDownloads.Delete(fileKey)
|
|
||||||
log.Debugf("file verified and downloaded!")
|
|
||||||
downloaded = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (fsss *FileSharingSubSystem) VerifyFile(fileKey string) (downloaded bool) {
|
||||||
|
manifestI, exists := fsss.activeDownloads.Load(fileKey)
|
||||||
|
if exists {
|
||||||
|
manifest := manifestI.(*Manifest)
|
||||||
|
if manifest.VerifyFile() == nil {
|
||||||
|
manifest.Close()
|
||||||
|
fsss.activeDownloads.Delete(fileKey)
|
||||||
|
log.Debugf("file verified and downloaded!")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in New Issue