From 10551a7d2466d6e2e4c4418ae240008c6fc7d106 Mon Sep 17 00:00:00 2001 From: rumanzo Date: Fri, 7 May 2021 20:00:37 +0300 Subject: [PATCH] Add ability to transfer torrents with renamed files --- bt2qbt.go | 7 +++++++ libtorrent/torrent.go | 29 ++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/bt2qbt.go b/bt2qbt.go index 8c559ce..ce55146 100644 --- a/bt2qbt.go +++ b/bt2qbt.go @@ -163,6 +163,7 @@ func logic(key string, value map[string]interface{}, flags *Flags, chans *Channe WithoutLabels: flags.WithoutLabels, WithoutTags: flags.WithoutTags, Separator: flags.PathSeparator, + Targets: map[int64]string{}, } if isAbs, _ := regexp.MatchString(`^([A-Za-z]:)?\\`, key); isAbs == true { @@ -206,6 +207,12 @@ func logic(key string, value map[string]interface{}, flags *Flags, chans *Channe newstructure.HasFiles = false } + if ok := value["targets"]; ok != nil { + for _, entry := range value["targets"].([]interface{}) { + newstructure.Targets[entry.([]interface{})[0].(int64)] = entry.([]interface{})[1].(string) + } + } + // remove separator from end lastRune, lastRuneSize := utf8.DecodeLastRuneInString(value["path"].(string)) separatorRunes := []rune("/\\") diff --git a/libtorrent/torrent.go b/libtorrent/torrent.go index 2ceb102..5b20d54 100644 --- a/libtorrent/torrent.go +++ b/libtorrent/torrent.go @@ -72,6 +72,7 @@ type NewTorrentStructure struct { PieceLenght int64 `bencode:"-"` Replace []replace.Replace `bencode:"-"` Separator string `bencode:"-"` + Targets map[int64]string `bencode:"-"` } func (newstructure *NewTorrentStructure) Started(started int64) { @@ -173,12 +174,21 @@ func (newstructure *NewTorrentStructure) FillSizes() { for num, file := range newstructure.TorrentFile["info"].(map[string]interface{})["files"].([]interface{}) { var lenght, mtime int64 var filestrings []string - if path, ok := file.(map[string]interface{})["path.utf-8"].([]interface{}); ok { - for _, f := range path { - filestrings = append(filestrings, f.(string)) - } + var mappedPath []interface{} + if paths, ok := file.(map[string]interface{})["path.utf-8"].([]interface{}); ok { + mappedPath = paths } else { - for _, f := range file.(map[string]interface{})["path"].([]interface{}) { + mappedPath = file.(map[string]interface{})["path"].([]interface{}) + } + + for n, f := range mappedPath { + if len(mappedPath)-1 == n && len(newstructure.Targets) > 0 { + for index, rewrittenFileName := range newstructure.Targets { + if index == int64(num) { + filestrings = append(filestrings, rewrittenFileName) + } + } + } else { filestrings = append(filestrings, f.(string)) } } @@ -264,6 +274,15 @@ func (newstructure *NewTorrentStructure) FillSavePaths() { if lastdirname == torrentname { newstructure.QbthasRootFolder = 1 newstructure.QbtSavePath = origpath[0 : len(origpath)-len(lastdirname)] + if len(newstructure.Targets) > 0 { + for _, path := range newstructure.torrentFileList { + if len(path) > 0 { + newstructure.MappedFiles = append(newstructure.MappedFiles, lastdirname+newstructure.Separator+path) + } else { + newstructure.MappedFiles = append(newstructure.MappedFiles, path) + } + } + } } else { newstructure.QbthasRootFolder = 0 newstructure.QbtSavePath = newstructure.Path + newstructure.Separator