diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenames.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenames.java index 98f4d74ce..c39705f8c 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenames.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenames.java @@ -21,6 +21,34 @@ private ExportFilenames() {} /** Characters not allowed in a FAT / exFAT file name, plus the path separators. */ private static final String ILLEGAL = "\\/:*?\"<>|"; + /** The app whose server name is already stable and human-readable: a ZIM is named by its own + * filename convention and Kiwix addresses it by that name, so it is kept as-is. Every other + * app's suggested name is an opaque id, so its archive is renamed from the item title. */ + private static final String APP_KEEPS_NATIVE_NAME = "kiwix"; + + /** Length of the short id suffix appended after a readable title. */ + private static final int SHORT_ID_LEN = 8; + + /** + * Resolve the name to store this item under, and record it in {@code used}. + * + *

Kiwix keeps its native, already-readable name; every other app's opaque id is replaced by + * a readable {@code "-<short id>.<ext>"} name. The manifest keeps the full identity, so + * only the on-drive file name changes here. + * + * @param title the item title, used to build a readable name; may be null/empty + * @param suggested the server-suggested name ({@code Content-Disposition}); may be null/empty + * @param ref the item address: its app selects the rule, its localRef the short id + * @param used names already taken in this bundle; the returned name is added to it + */ + public static String forItem(String title, String suggested, ItemRef ref, Set<String> used) { + String app = ref != null ? ref.app() : null; + if (APP_KEEPS_NATIVE_NAME.equals(app)) { + return resolve(suggested, ref, used); + } + return readable(title, suggested, ref, used); + } + /** * Resolve the name to store this item under, and record it in {@code used}. * @@ -38,6 +66,31 @@ public static String resolve(String suggested, ItemRef ref, Set<String> used) { return unique; } + /** + * Build a readable name {@code "<title>-<short id>.<ext>"} for an item whose server-suggested + * name is an opaque id. The title keeps spaces and accents (exFAT / FAT store Unicode), only the + * FAT-illegal characters are stripped; the short id is the first {@value #SHORT_ID_LEN} + * characters of the localRef, lowercased; the extension is taken from the server-suggested name. + * Falls back to {@link #resolve} when the title is null, empty, or sanitizes to nothing. + * + * @param title the item title; may be null/empty + * @param suggested the server-suggested name, used only for its extension; may be null/empty + * @param ref the item address, used for the short id and the fallback name + * @param used names already taken in this bundle; the returned name is added to it + */ + public static String readable(String title, String suggested, ItemRef ref, Set<String> used) { + String base = safeTitle(title); + if (base == null) { + return resolve(suggested, ref, used); + } + String shortId = shortId(ref != null ? ref.localRef() : null); + String ext = extensionOf(suggested); + String candidate = base + (shortId != null ? "-" + shortId : "") + ext; + String unique = deduplicate(candidate, used); + used.add(unique); + return unique; + } + /** Strip the path, drop illegal and control characters, trim. Returns null when nothing remains. * Public so the import staging path reuses the one "safe FAT basename" rule. */ public static String safeBaseName(String raw) { @@ -49,6 +102,18 @@ public static String safeBaseName(String raw) { if (slash >= 0) { name = name.substring(slash + 1); } + return sanitize(name); + } + + /** Make a free-text label FAT-safe without the path-strip of {@link #safeBaseName}: a "/" in a + * title (e.g. "Grade 1/2 Math") is dropped as an illegal character, not treated as a separator + * that truncates the name. Returns null when nothing usable remains. */ + public static String safeTitle(String raw) { + return raw == null ? null : sanitize(raw); + } + + /** Drop illegal and control characters, trim, and strip leading dots. Returns null when empty. */ + private static String sanitize(String name) { StringBuilder out = new StringBuilder(name.length()); for (int i = 0; i < name.length(); i++) { char c = name.charAt(i); @@ -65,6 +130,28 @@ public static String safeBaseName(String raw) { return trimmed.isEmpty() ? null : trimmed; } + /** First {@value #SHORT_ID_LEN} characters of the localRef, lowercased and FAT-safe. Returns + * null when nothing usable remains, so the caller drops the suffix. */ + private static String shortId(String localRef) { + String safe = safeBaseName(localRef); + if (safe == null) { + return null; + } + String head = safe.length() > SHORT_ID_LEN ? safe.substring(0, SHORT_ID_LEN) : safe; + head = head.toLowerCase(java.util.Locale.US); + return head.isEmpty() ? null : head; + } + + /** The extension (with its dot) of the server-suggested name, or "" when it has none. */ + private static String extensionOf(String suggested) { + String safe = safeBaseName(suggested); + if (safe == null) { + return ""; + } + int dot = safe.lastIndexOf('.'); + return dot > 0 ? safe.substring(dot) : ""; + } + private static String fallback(ItemRef ref) { String app = safeBaseName(ref != null ? ref.app() : null); String local = safeBaseName(ref != null ? ref.localRef() : null); diff --git a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentExportService.java b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentExportService.java index 6bb686068..d4c97ae9e 100644 --- a/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentExportService.java +++ b/controller/app/src/main/java/org/appdevforall/k2go/contentexchange/presentation/ContentExportService.java @@ -245,7 +245,7 @@ private void drain(boolean fresh) { notifyNow(); try (ContentExportSource.ItemPayload payload = source.open(ref)) { - String name = ExportFilenames.resolve(payload.filename(), ref, used); + String name = ExportFilenames.forItem(SESSION.label(i), payload.filename(), ref, used); // Write into the item's per-app subfolder; the sink reports the stored path // ("app/name"), which becomes the manifest payload. ExportCarrier.PayloadSink sink = carrier.openPayload(ref.app(), name); diff --git a/controller/app/src/test/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenamesTest.java b/controller/app/src/test/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenamesTest.java index 59d59f39e..098c754e9 100644 --- a/controller/app/src/test/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenamesTest.java +++ b/controller/app/src/test/java/org/appdevforall/k2go/contentexchange/domain/ExportFilenamesTest.java @@ -57,4 +57,75 @@ public void deduplicatesExtensionlessName() { assertEquals("data", ExportFilenames.resolve("data", ref, used)); assertEquals("data_2", ExportFilenames.resolve("data", ref, used)); } + + // ---- readable names (Calibre / Kolibri opaque ids become "<title>-<short id>.<ext>") -------- + + @Test + public void readableBuildsTitleWithShortIdAndExtension() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("calibre", "a79d2c9f-4e1b-4c3a-9d2e-000000000000"); + String name = ExportFilenames.readable("Romeo y Julieta", + "a79d2c9f-4e1b-4c3a-9d2e-000000000000.tar", ref, used); + assertEquals("Romeo y Julieta-a79d2c9f.tar", name); + } + + @Test + public void readableKeepsSpacesAndAccentsAndStripsIllegal() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("kolibri", "0123456789abcdef0123456789abcdef"); + String name = ExportFilenames.readable("Matemáticas: 1/2 <intro>", "0123456789abcdef.tar", + ref, used); + // Accents kept, spaces kept; the FAT-illegal ":" "/" "<" ">" dropped. + assertEquals("Matemáticas 12 intro-01234567.tar", name); + } + + @Test + public void readableLowercasesTheShortId() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("calibre", "ABCDEF12-0000"); + String name = ExportFilenames.readable("Title", "ABCDEF12-0000.tar", ref, used); + assertEquals("Title-abcdef12.tar", name); + } + + @Test + public void readableFallsBackToResolveWhenTitleUnusable() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("kolibri", "chan-7"); + // Title sanitizes to nothing: fall back to the server-name rule. + assertEquals("chan-7.tar", ExportFilenames.readable("///", "chan-7.tar", ref, used)); + } + + @Test + public void readableDeduplicatesSameTitleAndId() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("calibre", "deadbeef-1111"); + assertEquals("Book-deadbeef.tar", ExportFilenames.readable("Book", "deadbeef-1111.tar", ref, used)); + assertEquals("Book-deadbeef_2.tar", ExportFilenames.readable("Book", "deadbeef-1111.tar", ref, used)); + } + + @Test + public void readableWithNoExtensionSuggestion() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("calibre", "cafebabe-2222"); + assertEquals("Book-cafebabe", ExportFilenames.readable("Book", "cafebabe-2222", ref, used)); + } + + // ---- forItem policy (Kiwix keeps its native name; others are renamed) ----------------------- + + @Test + public void forItemKeepsKiwixNativeName() { + Set<String> used = new HashSet<>(); + ItemRef ref = new ItemRef("kiwix", "wikipedia_en_all_2024.zim"); + String name = ExportFilenames.forItem("Wikipedia", "wikipedia_en_all_2024.zim", ref, used); + assertEquals("wikipedia_en_all_2024.zim", name); + } + + @Test + public void forItemRenamesCalibreAndKolibri() { + Set<String> used = new HashSet<>(); + assertEquals("Romeo-a79d2c9f.tar", ExportFilenames.forItem("Romeo", + "a79d2c9f-0000.tar", new ItemRef("calibre", "a79d2c9f-0000"), used)); + assertEquals("Algebra-0123abcd.tar", ExportFilenames.forItem("Algebra", + "0123abcd4567.tar", new ItemRef("kolibri", "0123abcd4567"), used)); + } }