Philipp Häfelfinger
8068d829fe
Optimized http calls to reuse more code and DRY Interface for PiwigoContext WIP Naming WIP
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package piwigo
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/sirupsen/logrus"
|
|
"os"
|
|
)
|
|
|
|
type PiwigoCategory struct {
|
|
Id int
|
|
ParentId int
|
|
Name string
|
|
Key string
|
|
}
|
|
|
|
func buildLookupMap(categories map[int]*PiwigoCategory) map[string]*PiwigoCategory {
|
|
categoryLookups := map[string]*PiwigoCategory{}
|
|
for _, category := range categories {
|
|
logrus.Debugf("Loaded existing category %s", category.Key)
|
|
categoryLookups[category.Key] = category
|
|
}
|
|
return categoryLookups
|
|
}
|
|
|
|
func buildCategoryMap(statusResponse *getCategoryListResponse) map[int]*PiwigoCategory {
|
|
categories := map[int]*PiwigoCategory{}
|
|
for _, category := range statusResponse.Result.Categories {
|
|
categories[category.ID] = &PiwigoCategory{Id: category.ID, ParentId: category.IDUppercat, Name: category.Name, Key: category.Name}
|
|
}
|
|
return categories
|
|
}
|
|
|
|
func buildCategoryKeys(categories map[int]*PiwigoCategory) {
|
|
for _, category := range categories {
|
|
if category.ParentId == 0 {
|
|
category.Key = category.Name
|
|
continue
|
|
}
|
|
|
|
key := category.Name
|
|
parentId := category.ParentId
|
|
for parentId != 0 {
|
|
parent := categories[parentId]
|
|
// as we build the category as a directory hierarchy,
|
|
// we have to use the path separator to construct the path key
|
|
key = fmt.Sprintf("%s%c%s", parent.Name, os.PathSeparator, key)
|
|
parentId = parent.ParentId
|
|
}
|
|
|
|
category.Key = key
|
|
}
|
|
}
|