The importer from t5de-hub's docs/imports.md: says hello to the hub with its key pair, follows the console's config (paused, rate, jobs at a time, IMVU accounts, whose sealed passwords it keeps), claims jobs, finds each product's newest revision on IMVU's CDN, converts it (originals for placeholders, no _contents.json, ProductRating from IMVU's rating), checks it by t5de-api's pack rules and uploads it to the catalog with an import token. Co-Authored-By: Claude Opus 5.5 <[email protected]>
160 lines
4.2 KiB
Go
160 lines
4.2 KiB
Go
// Package imvutest fakes IMVU's CDN for tests: products made in code (never IMVU's files), served as IMVU serves
|
|
// them, in LZMA-compressed CFLs with a _contents.json.
|
|
package imvutest
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"git.t5de.dev/t5de/t5de-api/cfl"
|
|
"github.com/ulikunitz/xz/lzma"
|
|
)
|
|
|
|
// File is a file of a fake revision. Original, if set, is served on the CDN under URL as the full-size original
|
|
// (Data is then the CFL's placeholder); with no Data and an URL, the file is on the CDN only.
|
|
type File struct {
|
|
Name string
|
|
Data []byte
|
|
URL string
|
|
Original []byte
|
|
}
|
|
|
|
// CDN is a fake CDN: Revisions[product][revision] is a revision's files.
|
|
type CDN struct {
|
|
*httptest.Server
|
|
mu sync.Mutex
|
|
Revisions map[int64]map[int][]File
|
|
Requests map[string]int // by method
|
|
}
|
|
|
|
// New starts a fake CDN.
|
|
func New(t testing.TB) *CDN {
|
|
c := &CDN{Revisions: map[int64]map[int][]File{}, Requests: map[string]int{}}
|
|
c.Server = httptest.NewServer(http.HandlerFunc(c.serve))
|
|
t.Cleanup(c.Close)
|
|
return c
|
|
}
|
|
|
|
// Add adds a product revision.
|
|
func (c *CDN) Add(product int64, revision int, files ...File) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.Revisions[product] == nil {
|
|
c.Revisions[product] = map[int][]File{}
|
|
}
|
|
c.Revisions[product][revision] = files
|
|
}
|
|
|
|
func (c *CDN) serve(w http.ResponseWriter, r *http.Request) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.Requests[r.Method]++
|
|
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
if len(parts) < 2 || len(parts) > 3 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
product, _ := strconv.ParseInt(parts[0], 10, 64)
|
|
revision, _ := strconv.Atoi(parts[1])
|
|
files, ok := c.Revisions[product][revision]
|
|
if !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if len(parts) == 3 {
|
|
for _, f := range files {
|
|
if f.URL == parts[2] {
|
|
data := f.Original
|
|
if data == nil {
|
|
data = f.Data
|
|
}
|
|
w.Write(data)
|
|
return
|
|
}
|
|
}
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
w.Write(Compressed(files))
|
|
}
|
|
|
|
// Compressed is a revision's CFL as IMVU's CDN serves it: its files that have Data, and _contents.json, every entry
|
|
// and the directory LZMA-compressed (pylzma's format, with end markers).
|
|
func Compressed(files []File) []byte {
|
|
type content struct {
|
|
Name string `json:"name"`
|
|
URL string `json:"url,omitempty"`
|
|
Tags []string `json:"tags,omitempty"`
|
|
}
|
|
var entries []cfl.Entry
|
|
var contents []content
|
|
for _, f := range files {
|
|
c := content{Name: f.Name, URL: f.URL}
|
|
if f.Original != nil {
|
|
c.Tags = []string{"original", "thumbnail"}
|
|
}
|
|
contents = append(contents, c)
|
|
if f.Data != nil {
|
|
entries = append(entries, cfl.Entry{Name: f.Name, Data: f.Data})
|
|
}
|
|
}
|
|
list, _ := json.Marshal(contents)
|
|
entries = append(entries, cfl.Entry{Name: "_contents.json", Data: list})
|
|
|
|
le := binary.LittleEndian
|
|
b := make([]byte, 12)
|
|
copy(b, "CFL3")
|
|
var dir []byte
|
|
for _, e := range entries {
|
|
offset := len(b)
|
|
stored := pylzma(e.Data)
|
|
b = le.AppendUint32(b, uint32(len(stored)))
|
|
b = append(b, stored...)
|
|
name, _ := cfl.Latin1(e.Name)
|
|
dir = le.AppendUint32(dir, uint32(len(e.Data)))
|
|
dir = le.AppendUint32(dir, uint32(offset))
|
|
dir = le.AppendUint32(dir, cfl.LZMA)
|
|
dir = le.AppendUint16(dir, uint16(len(name)))
|
|
dir = append(dir, name...)
|
|
}
|
|
le.PutUint32(b[4:], uint32(len(b)))
|
|
le.PutUint32(b[8:], uint32(len(dir)))
|
|
stored := pylzma(dir)
|
|
b = le.AppendUint32(b, cfl.LZMA)
|
|
b = le.AppendUint32(b, uint32(len(stored)))
|
|
return append(b, stored...)
|
|
}
|
|
|
|
// pylzma compresses as pylzma does: 5 property bytes, then the raw stream with an end marker.
|
|
func pylzma(data []byte) []byte {
|
|
var b bytes.Buffer
|
|
w, err := lzma.WriterConfig{EOSMarker: true}.NewWriter(&b)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
w.Write(data)
|
|
if err := w.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
out := b.Bytes()
|
|
return append(bytes.Clone(out[:5]), out[lzma.HeaderLen:]...)
|
|
}
|
|
|
|
// Index is a minimal index.xml as IMVU's files have it: a parent, and no ProductRating.
|
|
func Index(parent int64) []byte {
|
|
return []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<Template>
|
|
<__DATAIMPORT>product://` + strconv.FormatInt(parent, 10) + `/index.xml</__DATAIMPORT><ParticleSystems>
|
|
</ParticleSystems>
|
|
</Template>
|
|
`)
|
|
}
|