feat(android): Capacitor APK build + native save/share bridge
- 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 ตามงานข้างบน
This commit is contained in:
1 parent
a0bccc2316
commit
6f146aef59
75 files changed
+4637
-151
No files matched your search
+80
-32
@@ -1,5 +1,6 @@
|
||||
// ── ZIP ทั้งโฟลเดอร์: สร้าง / ดาวน์โหลด / แชร์ผ่านระบบมือถือ ──
|
||||
import { safeFileName, humanDateTime } from "./naming.js";
|
||||
// ── ZIP: สร้าง / ดาวน์โหลด (ทุกโฟลเดอร์ หรือโฟลเดอร์เดียว) ──
|
||||
// โครงสร้างใน ZIP: <ชื่อโฟลเดอร์>/<ชื่อไฟล์>.jpg → แตกไฟล์ออกมาเป็นโฟลเดอร์จริง
|
||||
import { safeFileName, fileStamp } from "./naming.js";
|
||||
|
||||
const cache = { signature: "", file: null, fileName: "" };
|
||||
|
||||
@@ -11,49 +12,87 @@ 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 ของ "ทุกรูปในโฟลเดอร์" + metadata.json
|
||||
* ใช้ compression: STORE เพราะ JPEG บีบอัดต่อไม่ได้แล้ว (เร็วขึ้นมากบนมือถือ)
|
||||
* 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 base = entryBase(folder);
|
||||
const photos = [];
|
||||
const dir = folderDirName(folder, new Set());
|
||||
const used = new Set();
|
||||
|
||||
zip.folder(dir);
|
||||
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 || {}) });
|
||||
const fallback = `${entryBase(folder)}_${String(idx + 1).padStart(4, "0")}.jpg`;
|
||||
zip.file(`${dir}/${uniqueEntryName(img.name || fallback, used)}`, img.blob);
|
||||
});
|
||||
|
||||
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 };
|
||||
return { blob, file, fileName, dir, photos: images.length };
|
||||
}
|
||||
|
||||
/** ใช้ cache ถ้ารายการรูปไม่เปลี่ยน — ทำให้กดแชร์แล้วเปิด share sheet ได้ทันที */
|
||||
/**
|
||||
* 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;
|
||||
@@ -65,6 +104,14 @@ export async function ensureZip(folder, images, onProgress, { force = false } =
|
||||
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;
|
||||
@@ -92,10 +139,7 @@ export function downloadFile(file) {
|
||||
setTimeout(() => URL.revokeObjectURL(url), 60_000);
|
||||
}
|
||||
|
||||
/**
|
||||
* แชร์ผ่าน Web Share API ระดับไฟล์ → share sheet ของ Samsung → เลือกแอปปลายทาง
|
||||
* คืน {ok, reason}
|
||||
*/
|
||||
/** แชร์ผ่าน 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 || "" };
|
||||
@@ -107,6 +151,10 @@ export async function shareFile(file, { title, text } = {}) {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user