113 lines
3.8 KiB
JavaScript
113 lines
3.8 KiB
JavaScript
// ── ZIP ทั้งโฟลเดอร์: สร้าง / ดาวน์โหลด / แชร์ผ่านระบบมือถือ ──
|
|
import { safeFileName, humanDateTime } from "./naming.js";
|
|
|
|
const cache = { signature: "", file: null, fileName: "" };
|
|
|
|
function signatureOf(folder, images) {
|
|
return `${folder.id}|${folder.updatedAt || folder.createdAt}|${images.map((i) => i.id).join(".")}`;
|
|
}
|
|
|
|
function entryBase(folder) {
|
|
return safeFileName(folder.name).replace(/\s+/g, "_");
|
|
}
|
|
|
|
/**
|
|
* สร้าง ZIP ของ "ทุกรูปในโฟลเดอร์" + metadata.json
|
|
* ใช้ compression: STORE เพราะ JPEG บีบอัดต่อไม่ได้แล้ว (เร็วขึ้นมากบนมือถือ)
|
|
*/
|
|
export async function buildZip(folder, images, onProgress) {
|
|
if (!window.JSZip) throw new Error("JSZip not loaded");
|
|
const zip = new JSZip();
|
|
const base = entryBase(folder);
|
|
const photos = [];
|
|
|
|
images.forEach((img, idx) => {
|
|
const seq = String(idx + 1).padStart(4, "0");
|
|
const name = img.name || `${base}_${seq}.jpg`;
|
|
zip.file(name, img.blob);
|
|
photos.push({ file: name, ...(img.meta || {}) });
|
|
});
|
|
|
|
zip.file(
|
|
"metadata.json",
|
|
JSON.stringify(
|
|
{
|
|
folder: folder.name,
|
|
count: images.length,
|
|
generated_at: new Date().toISOString(),
|
|
generated_at_th: humanDateTime(Date.now()),
|
|
app: "CamCat 2.0.0",
|
|
photos,
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
|
|
const blob = await zip.generateAsync({ type: "blob", compression: "STORE" }, (m) => {
|
|
if (onProgress) onProgress(m.percent);
|
|
});
|
|
|
|
const fileName = safeFileName(folder.name, "photos") + ".zip";
|
|
const file = new File([blob], fileName, { type: "application/zip" });
|
|
return { blob, file, fileName };
|
|
}
|
|
|
|
/** ใช้ cache ถ้ารายการรูปไม่เปลี่ยน — ทำให้กดแชร์แล้วเปิด share sheet ได้ทันที */
|
|
export async function ensureZip(folder, images, onProgress, { force = false } = {}) {
|
|
const sig = signatureOf(folder, images);
|
|
if (!force && cache.file && cache.signature === sig) return cache;
|
|
const { blob, file, fileName } = await buildZip(folder, images, onProgress);
|
|
cache.signature = sig;
|
|
cache.file = file;
|
|
cache.fileName = fileName;
|
|
cache.blob = blob;
|
|
return cache;
|
|
}
|
|
|
|
export function invalidateZip() {
|
|
cache.signature = "";
|
|
cache.file = null;
|
|
cache.fileName = "";
|
|
}
|
|
|
|
export function canShareFiles(file) {
|
|
try {
|
|
return !!(navigator.canShare && navigator.canShare({ files: [file] }));
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** ดาวน์โหลดลงเครื่อง (Chrome Android → โฟลเดอร์ Download) */
|
|
export function downloadFile(file) {
|
|
const url = URL.createObjectURL(file);
|
|
const a = document.createElement("a");
|
|
a.href = url;
|
|
a.download = file.name;
|
|
a.rel = "noopener";
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
|
}
|
|
|
|
/**
|
|
* แชร์ผ่าน Web Share API ระดับไฟล์ → share sheet ของ Samsung → เลือกแอปปลายทาง
|
|
* คืน {ok, reason}
|
|
*/
|
|
export async function shareFile(file, { title, text } = {}) {
|
|
if (!navigator.share) return { ok: false, reason: "unsupported" };
|
|
const payload = { files: [file], title: title || file.name, text: text || "" };
|
|
if (navigator.canShare && !navigator.canShare({ files: [file] })) {
|
|
return { ok: false, reason: "nofiles" };
|
|
}
|
|
try {
|
|
await navigator.share(payload);
|
|
return { ok: true };
|
|
} catch (err) {
|
|
if (err && err.name === "AbortError") return { ok: false, reason: "cancelled" };
|
|
return { ok: false, reason: "failed", error: err };
|
|
}
|
|
}
|