// Package convert turns an IMVU product revision, as IMVU's CDN serves it, into a T5 product's files (t5de-hub's // docs/imports.md, "What's imported"): the CFL's entries, with the small placeholder textures replaced by the // originals its _contents.json names (and files kept only on the CDN fetched), without _contents.json (packs don't // use it), and with index.xml's ProductRating set. The parent stays as index.xml names it (__DATAIMPORT). package convert import ( "archive/zip" "bytes" "context" "encoding/json" "errors" "fmt" "os" "path/filepath" "regexp" "slices" "strconv" "git.t5de.dev/t5de/t5de-api/cfl" "git.t5de.dev/t5de/t5de-api/pack" ) // ContentsName is the file listing a revision's files on the CDN. const ContentsName = "_contents.json" // File is one of a product's files. type File struct { Name string Data []byte } // content is one _contents.json entry. Originals (tag "original") and files kept only on the CDN have a url. type content struct { Name string `json:"name"` URL string `json:"url"` Tags []string `json:"tags"` } // Fetch downloads a file of the revision by its _contents.json url. type Fetch func(ctx context.Context, url string) ([]byte, error) // Files converts a revision's CFL entries. rating is IMVU's ("general" or "adult"); the files say AP if either // IMVU or the files themselves do. func Files(ctx context.Context, entries []cfl.Entry, rating string, fetch Fetch) ([]File, error) { if rating != "general" && rating != "adult" { return nil, fmt.Errorf("unknown rating %q", rating) } files := make([]File, 0, len(entries)) at := map[string]int{} var contents []content for _, e := range entries { if e.Name == ContentsName { if err := json.Unmarshal(e.Data, &contents); err != nil { return nil, fmt.Errorf("%s: %w", ContentsName, err) } continue } if _, dup := at[e.Name]; dup { return nil, fmt.Errorf("the CFL has %q twice", e.Name) } at[e.Name] = len(files) files = append(files, File{Name: e.Name, Data: e.Data}) } for _, c := range contents { if c.URL == "" || c.Name == ContentsName { continue } i, inCFL := at[c.Name] if inCFL && !slices.Contains(c.Tags, "original") { continue } data, err := fetch(ctx, c.URL) if err != nil { return nil, fmt.Errorf("%s: %w", c.Name, err) } if inCFL { files[i].Data = data } else { at[c.Name] = len(files) files = append(files, File{Name: c.Name, Data: data}) } } i, ok := at[pack.IndexName] if !ok { return nil, errors.New("the product has no index.xml") } index, err := SetRating(files[i].Data, rating) if err != nil { return nil, err } files[i].Data = index return files, nil } var ( ratingElement = regexp.MustCompile(`\s*([A-Za-z]*)\s*`) templateOpen = regexp.MustCompile(`]*)?>`) ) // SetRating sets index.xml's ProductRating: AP if rating is "adult" or it says AP already, else GA. IMVU's files // usually have none, and the hub rates an imported revision the higher of IMVU's rating and the files'. func SetRating(index []byte, rating string) ([]byte, error) { want := "GA" if m := ratingElement.FindSubmatch(index); rating == "adult" || (m != nil && string(m[1]) == "AP") { want = "AP" } element := []byte("" + want + "") if loc := ratingElement.FindIndex(index); loc != nil { return slices.Concat(index[:loc[0]], element, index[loc[1]:]), nil } loc := templateOpen.FindIndex(index) if loc == nil { return nil, errors.New("index.xml has no