Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import org.appdevforall.k2go.SystemStateEvaluator;
import org.appdevforall.k2go.contentexchange.domain.ExportFilenames;
import org.appdevforall.k2go.contentexchange.domain.PartialPayload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

public final class BoxImportStaging {

Expand All @@ -48,10 +50,34 @@ public static String safeName(String name) {

/** Open the host staging file for writing, creating the staging directory if needed. */
public OutputStream open(String storedName) throws IOException {
return open(storedName, false);
}

/** Open the host staging file, appending to the kept bytes when {@code append} (a resume). */
public OutputStream open(String storedName, boolean append) throws IOException {
if (!hostDir.exists() && !hostDir.mkdirs()) {
throw new IOException("Could not create the import staging folder");
}
return new FileOutputStream(new File(hostDir, storedName));
return new FileOutputStream(new File(hostDir, storedName), append);
}

/** Bytes in a staged file, 0 when it does not exist. */
public long length(String storedName) {
return new File(hostDir, storedName).length();
}

/** Remove every kept partial whose name is not in {@code keep} (K2GO-577). */
public void sweepPartials(Set<String> keep) {
File[] files = hostDir.listFiles();
if (files == null) {
return;
}
for (File f : files) {
if (PartialPayload.isPartial(f.getName()) && !keep.contains(f.getName())) {
//noinspection ResultOfMethodCallIgnored
f.delete();
}
}
}

/** Remove a staged file after the core has ingested it (best effort). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Description : K2GO-376. The network ImportCarrier: the mirror of SafImportCarrier, but each item is
* read over HTTP from the sender's live conex-core gate on the shared hotspot, not from a
* SAF drive. readManifest() GETs the invite's manifest URL; openPayload(itemRef) GETs
* its sibling .../fetch/<itemRef>, retrying while the sender stages the item; the token
* its sibling .../fetch/<itemRef>, retrying while the sender stages the item, with a
* Range header to resume a cut item (K2GO-577); the token
* rides the X-Share-Token header, never the URL. Everything downstream (stage to
* /library/.conex-staging, POST /v1/import/item) is unchanged from the disk path, so the
* carrier is the only sender-specific dependency of the import service. On a clean finish
Expand All @@ -20,6 +21,7 @@
import android.util.Log;

import org.appdevforall.k2go.contentexchange.domain.ImportCarrier;
import org.appdevforall.k2go.contentexchange.domain.PartialPayload;
import org.appdevforall.k2go.contentexchange.domain.ShareTransport;
import org.appdevforall.k2go.contentexchange.domain.ShareUrls;
import org.appdevforall.k2go.sync.transport.WifiNetworkBinder;
Expand Down Expand Up @@ -121,24 +123,40 @@ public List<DriveFile> listFiles() {

@Override
public InputStream openPayload(String itemRef) throws IOException {
return openPayloadFrom(itemRef, 0L).stream();
}

/** Fetch one item, asking for {@code Range: bytes=offset-} to resume a cut copy (K2GO-577). */
@Override
public Payload openPayloadFrom(String itemRef, long offset) throws IOException {
String url = ShareUrls.fetch(baseUrl, manifestUrl, itemRef);
long deadline = System.currentTimeMillis() + FETCH_MAX_WAIT_MS;
while (true) {
if (cancel.isCancelled()) {
throw new IOException("cancelled");
}
HttpURLConnection c = open(url, "GET", FETCH_STALL_TIMEOUT_MS);
if (offset > 0) {
c.setRequestProperty("Range", "bytes=" + offset + "-");
}
int code;
try {
code = c.getResponseCode();
} catch (IOException e) {
c.disconnect();
throw e;
}
if (code == 200) {
if (code == 200 || code == 206) {
long start = PartialPayload.bodyStart(code, offset, c.getHeaderField("Content-Range"));
if (start < 0) {
c.disconnect();
throw new IOException("Share fetch: reply does not continue byte " + offset);
}
// The caller streams and closes this; closing disconnects the connection.
try {
return new ConnectionInputStream(c, c.getInputStream());
// The sender marks a byte-exact file (a ZIM) with Accept-Ranges; a tar has none.
boolean exact = "bytes".equalsIgnoreCase(c.getHeaderField("Accept-Ranges"));
return new Payload(new ConnectionInputStream(c, c.getInputStream()), start, exact);
} catch (IOException e) {
c.disconnect(); // never leak the socket if the body stream cannot be opened
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ private ShareManifestReader() {
* missing (unknown app / not found) are not in {@code items}, so they never produce an entry. An
* item that cannot be addressed (no app, no localRef, or a non-numeric itemRef) is skipped.
*/
/**
* The item's exact-build id (the ZIM UUID) from its entry JSON, or null when the identity is not
* authoritative or has no build. Keys the kept partial of a cut transfer (K2GO-577).
*/
public static String buildId(String entryJson) {
if (entryJson == null) {
return null;
}
try {
JSONObject dedup = new JSONObject(entryJson).optJSONObject("identity");
dedup = dedup != null ? dedup.optJSONObject("dedup") : null;
if (dedup == null || !"authoritative".equals(dedup.optString("idKind"))) {
return null;
}
String build = dedup.optString("build", "");
return build.isEmpty() || "null".equals(build) ? null : build;
} catch (Exception e) {
return null;
}
}

public static List<ImportEntry> parse(byte[] manifestBytes) throws Exception {
List<ImportEntry> out = new ArrayList<>();
if (manifestBytes == null || manifestBytes.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,44 @@ public interface ImportCarrier {
/** Open a readable stream for one payload file; the caller copies it, then closes it. */
InputStream openPayload(String name) throws IOException;

/**
* Open one payload from byte {@code offset} to resume a cut copy (K2GO-577). The source may send
* the whole item instead; {@link Payload#start()} says where the body starts. Default: the whole
* item, not resumable (a drive source has nothing to resume).
*/
default Payload openPayloadFrom(String name, long offset) throws IOException {
return new Payload(openPayload(name), 0L, false);
}

/**
* A payload body, the item byte it starts at, and whether the item is one exact file the source
* can resume (so a cut copy is worth keeping). Immutable value.
*/
final class Payload {

private final InputStream stream;
private final long start;
private final boolean exact;

public Payload(InputStream stream, long start, boolean exact) {
this.stream = stream;
this.start = start;
this.exact = exact;
}

public boolean exact() {
return exact;
}

public InputStream stream() {
return stream;
}

public long start() {
return start;
}
}

/**
* Signal that the run finished with every requested item delivered (a clean success). The default
* is a no-op (a drive source has nothing to close); the network carrier uses it to tell the sender
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* ============================================================================
* Name : PartialPayload.java
* Author : AppDevForAll
* Copyright : Copyright (c) 2026 AppDevForAll
* Description : K2GO-577. The rules for resuming a cut Share item: which items keep a partial file
* (an exact-build id, e.g. the ZIM UUID), where a resume starts, and whether the
* sender's reply continues the kept bytes. Pure JVM, unit-tested.
* ============================================================================
*/
package org.appdevforall.k2go.contentexchange.domain;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A cut Share item keeps its bytes in a partial file named by the item's exact-build id, so a Retry,
* or a new share of the same build, continues it with {@code Range: bytes=N-}. The sender decides if
* it can resume (206) or must send the whole item (200); the box verifies the item before import.
*/
public final class PartialPayload {

/** Staging-name prefix of every kept partial, so a run can find and sweep stale ones. */
public static final String PREFIX = "partial-";

private static final Pattern APP = Pattern.compile("[a-z0-9]{1,16}");
private static final Pattern BUILD = Pattern.compile("[A-Za-z0-9-]{1,64}");
private static final Pattern CONTENT_RANGE = Pattern.compile("bytes (\\d{1,19})-\\d{1,19}/\\d{1,19}");

private PartialPayload() {
}

/**
* The staging name of the partial for an item, or null when the item has no safe exact-build id
* (then it is not kept: a cut item restarts at 0, as before).
*/
public static String name(String app, String buildId) {
if (app == null || buildId == null || !APP.matcher(app).matches()
|| !BUILD.matcher(buildId).matches()) {
return null;
}
return PREFIX + app + "-" + buildId;
}

public static boolean isPartial(String storedName) {
return storedName != null && storedName.startsWith(PREFIX);
}

/**
* The byte to resume from: the kept length when it is not more than the item size, else 0 (a
* longer file is not this item; start over). Equal to the size means nothing is left to fetch.
*/
public static long resumeFrom(long kept, long size) {
return kept > 0 && kept <= size ? kept : 0L;
}

/**
* True when a failed item keeps its partial for a later resume: only an exact-file transfer that
* was cut. A failed ingest means the box rejected the bytes, and a cancel means the user gave up.
*/
public static boolean keepOnFailure(boolean resumable, boolean copied, boolean cancelled) {
return resumable && !copied && !cancelled;
}

/** True when an exact-file copy holds the whole item, no more and no less. */
public static boolean isComplete(long copied, long size) {
return copied == size;
}

/**
* Where the reply body starts in the item: 0 for a whole item (200), the requested byte for a 206
* whose Content-Range starts there, else -1 (a reply that does not continue the kept bytes).
*/
public static long bodyStart(int code, long requested, String contentRange) {
if (code == 200) {
return 0L;
}
if (code != 206 || contentRange == null) {
return -1L;
}
Matcher m = CONTENT_RANGE.matcher(contentRange.trim());
if (!m.matches()) {
return -1L;
}
try {
return Long.parseLong(m.group(1)) == requested ? requested : -1L;
} catch (NumberFormatException e) {
return -1L;
}
}
}
Loading
Loading