Merge pull request #30 from rumanzo/transferrenamedfiles

Add ability to transfer torrents with renamed files
This commit is contained in:
Alexey Kostin 2021-05-07 20:00:58 +03:00 committed by GitHub
commit 2ddae8fb8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View File

@ -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("/\\")

View File

@ -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 {
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 {
for _, f := range file.(map[string]interface{})["path"].([]interface{}) {
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