- android/ — Capacitor project (build/, local.properties ignored) - app/js/native.js — bridge สำหรับ APK: savePhoto/saveZip/shareItems ผ่าน Filesystem+Share - app/js/csv.js — นำเข้าโฟลเดอร์หลายรายการจาก CSV (site,letter,datetime,name) - app/js/orient.js — จัดการ orientation ของ preview/กล้อง - app/manual.html + APK.md — คู่มือใช้งานและวิธี build APK - app/templates/folders-template.csv — เทมเพลต CSV - package.json / package-lock.json / capacitor.config.json / scripts/build-apk.sh - ปรับ ui, camera, exif, archive, db, naming, main, sw + css/README ตามงานข้างบน
161 lines
6.4 KiB
JavaScript
161 lines
6.4 KiB
JavaScript
// ── ZIP: สร้าง / ดาวน์โหลด (ทุกโฟลเดอร์ หรือโฟลเดอร์เดียว) ──
|
|
// โครงสร้างใน ZIP: <ชื่อโฟลเดอร์>/<ชื่อไฟล์>.jpg → แตกไฟล์ออกมาเป็นโฟลเดอร์จริง
|
|
import { safeFileName, fileStamp } 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 — กันซ้ำและตัดอักขระที่ทำให้ path เพี้ยน */
|
|
function folderDirName(folder, used) {
|
|
const base = safeFileName(folder.name, "photos").replace(/[\\/]+/g, "-").replace(/\.+$/, "").trim() || "photos";
|
|
let dir = base;
|
|
let n = 2;
|
|
while (used.has(dir.toLowerCase())) dir = `${base} (${n++})`;
|
|
used.add(dir.toLowerCase());
|
|
return dir;
|
|
}
|
|
|
|
/** กันชื่อไฟล์ซ้ำภายในโฟลเดอร์เดียว (เช่นหลังเปลี่ยนชื่อโฟลเดอร์) */
|
|
function uniqueEntryName(name, usedNames) {
|
|
const clean = String(name || "photo.jpg").replace(/[\\/]+/g, "-");
|
|
let cand = clean;
|
|
let n = 2;
|
|
while (usedNames.has(cand.toLowerCase())) {
|
|
cand = clean.replace(/(\.[a-z0-9]+)?$/i, (ext) => `-${n++}${ext || ""}`);
|
|
}
|
|
usedNames.add(cand.toLowerCase());
|
|
return cand;
|
|
}
|
|
|
|
/**
|
|
* ZIP ของโฟลเดอร์เดียว — ทุกรูปอยู่ในโฟลเดอร์ชื่อเดียวกับโฟลเดอร์
|
|
* compression STORE เพราะ JPEG บีบอัดต่อไม่ได้แล้ว
|
|
*/
|
|
export async function buildZip(folder, images, onProgress) {
|
|
if (!window.JSZip) throw new Error("JSZip not loaded");
|
|
const zip = new JSZip();
|
|
const dir = folderDirName(folder, new Set());
|
|
const used = new Set();
|
|
|
|
zip.folder(dir);
|
|
images.forEach((img, idx) => {
|
|
const fallback = `${entryBase(folder)}_${String(idx + 1).padStart(4, "0")}.jpg`;
|
|
zip.file(`${dir}/${uniqueEntryName(img.name || fallback, used)}`, img.blob);
|
|
});
|
|
|
|
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, dir, photos: images.length };
|
|
}
|
|
|
|
/**
|
|
* ZIP รวมทุกโฟลเดอร์ในครั้งเดียว — แต่ละโฟลเดอร์เป็นโฟลเดอร์ย่อยของตัวเอง
|
|
* folders: [{id,name,...}] · imagesByFolder: { [folderId]: images[] }
|
|
*/
|
|
export async function buildAllZip(folders, imagesByFolder, onProgress) {
|
|
if (!window.JSZip) throw new Error("JSZip not loaded");
|
|
const zip = new JSZip();
|
|
const usedDirs = new Set();
|
|
let photos = 0;
|
|
let folderCount = 0;
|
|
|
|
folders.forEach((folder) => {
|
|
const images = imagesByFolder[folder.id] || [];
|
|
const dir = folderDirName(folder, usedDirs);
|
|
zip.folder(dir); // สร้างโฟลเดอร์แม้ยังไม่มีรูป
|
|
folderCount++;
|
|
const used = new Set();
|
|
images.forEach((img, idx) => {
|
|
const fallback = `${entryBase(folder)}_${String(idx + 1).padStart(4, "0")}.jpg`;
|
|
zip.file(`${dir}/${uniqueEntryName(img.name || fallback, used)}`, img.blob);
|
|
photos++;
|
|
});
|
|
});
|
|
|
|
const blob = await zip.generateAsync({ type: "blob", compression: "STORE" }, (m) => {
|
|
if (onProgress) onProgress(m.percent);
|
|
});
|
|
|
|
const fileName = `CamCat-all-${fileStamp()}.zip`;
|
|
const file = new File([blob], fileName, { type: "application/zip" });
|
|
return { blob, file, fileName, folders: folderCount, photos };
|
|
}
|
|
|
|
/** ใช้ cache ถ้ารายการรูปไม่เปลี่ยน */
|
|
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;
|
|
}
|
|
|
|
/** คืน File ที่สร้างไว้แล้วถ้ารายการรูปยังไม่เปลี่ยน */
|
|
export function peekZip(folder, images) {
|
|
try {
|
|
if (folder && cache.file && cache.signature === signatureOf(folder, images)) return cache.file;
|
|
} catch { /* ignore */ }
|
|
return null;
|
|
}
|
|
|
|
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 ระดับไฟล์ — คืน {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" };
|
|
if (err && (err.name === "NotAllowedError" || err.name === "InvalidStateError")) {
|
|
return { ok: false, reason: "notallowed", error: err };
|
|
}
|
|
if (err && err.name === "TypeError") return { ok: false, reason: "nofiles", error: err };
|
|
return { ok: false, reason: "failed", error: err };
|
|
}
|
|
}
|