func (torrent *Torrent) Update(t *torrent.Torrent) { torrent.Name = t.Name() torrent.Loaded = t.Info() != nil if torrent.Loaded { torrent.Size = t.Length() } totalChunks := 0 totalCompleted := 0 tfiles := t.Files() if len(tfiles) > 0 && torrent.Files == nil { torrent.Files = make([]*File, len(tfiles)) } //merge in files for i, f := range tfiles { path := f.Path() file := torrent.Files[i] if file == nil { file = &File{Path: path} torrent.Files[i] = file } chunks := f.State() file.Size = f.Length() file.Chunks = len(chunks) completed := 0 for _, p := range chunks { if p.Complete { completed++ } } file.Completed = completed file.Percent = percent(int64(file.Completed), int64(file.Chunks)) file.f = f totalChunks += file.Chunks totalCompleted += file.Completed } //cacluate rate now := time.Now() bytes := t.BytesCompleted() torrent.Percent = percent(bytes, torrent.Size) if !torrent.updatedAt.IsZero() { dt := float32(now.Sub(torrent.updatedAt)) db := float32(bytes - torrent.Downloaded) rate := db * (float32(time.Second) / dt) if rate >= 0 { torrent.DownloadRate = rate } } torrent.Downloaded = bytes torrent.updatedAt = now torrent.t = t }