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:
@@ -235,26 +235,16 @@ class AppState(private val configStore: ConfigStore) {
|
|||||||
val cachePath = cacheDir.resolve("$hash.jpg")
|
val cachePath = cacheDir.resolve("$hash.jpg")
|
||||||
if (tmpFile.toFile().exists() && tmpFile.toFile().length() > 0 && !cachePath.toFile().exists()) {
|
if (tmpFile.toFile().exists() && tmpFile.toFile().length() > 0 && !cachePath.toFile().exists()) {
|
||||||
val success = if (ext in setOf("heic", "heif")) {
|
val success = if (ext in setOf("heic", "heif")) {
|
||||||
// Extract embedded EXIF thumbnail from HEIC (fast, no full decode needed)
|
// Convert partial HEIC to JPEG via sips
|
||||||
// sips -getProperty thumbnail reads only the thumbnail track
|
// sips can decode HEIC even from a partial file if the header is intact
|
||||||
val sips = ProcessBuilder(
|
val sips = ProcessBuilder(
|
||||||
"sips", "-getProperty", "thumbnail",
|
"sips", "--resampleWidth", "240",
|
||||||
|
"--setProperty", "format", "jpeg",
|
||||||
tmpFile.toString(), "--out", cachePath.toString()
|
tmpFile.toString(), "--out", cachePath.toString()
|
||||||
).redirectErrorStream(true).start()
|
).redirectErrorStream(true).start()
|
||||||
val output = sips.inputStream.bufferedReader().readText()
|
sips.inputStream.readBytes()
|
||||||
sips.waitFor()
|
sips.waitFor()
|
||||||
val ok = cachePath.toFile().exists() && cachePath.toFile().length() > 0
|
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()
|
|
||||||
cachePath.toFile().exists() && cachePath.toFile().length() > 0
|
|
||||||
} else ok
|
|
||||||
} else {
|
} else {
|
||||||
// JPEG/PNG: simple resample
|
// JPEG/PNG: simple resample
|
||||||
val sips = ProcessBuilder(
|
val sips = ProcessBuilder(
|
||||||
|
|||||||
@@ -176,15 +176,20 @@ async def cmd_batch_thumbs(pairs_json: str):
|
|||||||
for device_path, local_path in pairs:
|
for device_path, local_path in pairs:
|
||||||
ext = device_path.rsplit('.', 1)[-1].lower() if '.' in device_path else ''
|
ext = device_path.rsplit('.', 1)[-1].lower() if '.' in device_path else ''
|
||||||
is_heic = ext in ('heic', 'heif')
|
is_heic = ext in ('heic', 'heif')
|
||||||
read_size = 1024 * 1024 if is_heic else 512 * 1024 # 1MB for HEIC, 512KB for others
|
|
||||||
try:
|
try:
|
||||||
os.makedirs(os.path.dirname(os.path.abspath(local_path)), exist_ok=True)
|
os.makedirs(os.path.dirname(os.path.abspath(local_path)), exist_ok=True)
|
||||||
try:
|
if is_heic:
|
||||||
data = await afc.fread(device_path, read_size, 0)
|
# HEIC: pull full file — sips needs the full image to convert to JPEG
|
||||||
with open(local_path, 'wb') as f:
|
# Cached permanently so only transferred once
|
||||||
f.write(data)
|
|
||||||
except Exception:
|
|
||||||
await afc.pull(device_path, local_path)
|
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, 512 * 1024, 0)
|
||||||
|
with open(local_path, 'wb') as f:
|
||||||
|
f.write(data)
|
||||||
|
except Exception:
|
||||||
|
await afc.pull(device_path, local_path)
|
||||||
results.append({'devicePath': device_path, 'ok': True})
|
results.append({'devicePath': device_path, 'ok': True})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
results.append({'devicePath': device_path, 'ok': False, 'error': str(e)})
|
results.append({'devicePath': device_path, 'ok': False, 'error': str(e)})
|
||||||
|
|||||||
Reference in New Issue
Block a user