HEIC thumbnails: pull full file (partial reads unreliable for sips conversion)

1MB partial HEIC reads are not sufficient for sips to convert to JPEG.
Revert to full file pull for HEIC thumbnails. Files are cached permanently
so each HEIC is only transferred once — subsequent launches skip already-cached files.
This commit is contained in:
Kyle Bolen
2026-07-30 03:57:44 +00:00
parent 30b600e2a4
commit 558a7add89
2 changed files with 17 additions and 22 deletions

View File

@@ -235,26 +235,16 @@ class AppState(private val configStore: ConfigStore) {
val cachePath = cacheDir.resolve("$hash.jpg")
if (tmpFile.toFile().exists() && tmpFile.toFile().length() > 0 && !cachePath.toFile().exists()) {
val success = if (ext in setOf("heic", "heif")) {
// Extract embedded EXIF thumbnail from HEIC (fast, no full decode needed)
// sips -getProperty thumbnail reads only the thumbnail track
// Convert partial HEIC to JPEG via sips
// sips can decode HEIC even from a partial file if the header is intact
val sips = ProcessBuilder(
"sips", "-getProperty", "thumbnail",
tmpFile.toString(), "--out", cachePath.toString()
).redirectErrorStream(true).start()
val output = sips.inputStream.bufferedReader().readText()
sips.waitFor()
val ok = cachePath.toFile().exists() && cachePath.toFile().length() > 0
if (!ok) {
// Thumbnail extraction failed — fall back to full resample
val sips2 = ProcessBuilder(
"sips", "--resampleWidth", "240",
"--setProperty", "format", "jpeg",
tmpFile.toString(), "--out", cachePath.toString()
).redirectErrorStream(true).start()
sips2.inputStream.readBytes()
sips2.waitFor()
sips.inputStream.readBytes()
sips.waitFor()
cachePath.toFile().exists() && cachePath.toFile().length() > 0
} else ok
} else {
// JPEG/PNG: simple resample
val sips = ProcessBuilder(

View File

@@ -176,11 +176,16 @@ async def cmd_batch_thumbs(pairs_json: str):
for device_path, local_path in pairs:
ext = device_path.rsplit('.', 1)[-1].lower() if '.' in device_path else ''
is_heic = ext in ('heic', 'heif')
read_size = 1024 * 1024 if is_heic else 512 * 1024 # 1MB for HEIC, 512KB for others
try:
os.makedirs(os.path.dirname(os.path.abspath(local_path)), exist_ok=True)
if is_heic:
# HEIC: pull full file — sips needs the full image to convert to JPEG
# Cached permanently so only transferred once
await afc.pull(device_path, local_path)
else:
# JPEG/PNG: 512KB partial read is sufficient for sips resample
try:
data = await afc.fread(device_path, read_size, 0)
data = await afc.fread(device_path, 512 * 1024, 0)
with open(local_path, 'wb') as f:
f.write(data)
except Exception: