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
@@ -0,0 +1,143 @@
|
||||
// ── Native bridge (Capacitor / Android APK) ───────────────────
|
||||
// ทำงานเมื่อรันใน APK เท่านั้น — บนเว็บทุกฟังก์ชันคืน null แล้วแอปใช้ทางเดิม
|
||||
// ต้องมีปลั๊กอิน: @capacitor/filesystem, @capacitor/share
|
||||
|
||||
const cap = () => (typeof window !== "undefined" ? window.Capacitor : undefined);
|
||||
|
||||
export function isNative() {
|
||||
try {
|
||||
return !!(cap() && typeof cap().isNativePlatform === "function" && cap().isNativePlatform());
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
export function platform() {
|
||||
try { return cap()?.getPlatform?.() || "web"; } catch { return "web"; }
|
||||
}
|
||||
|
||||
function plugin(name) {
|
||||
return cap()?.Plugins?.[name] || null;
|
||||
}
|
||||
|
||||
const MAX_NAME = 120;
|
||||
|
||||
function safeSeg(s, fallback = "file") {
|
||||
const v = String(s ?? "")
|
||||
.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim()
|
||||
.replace(/^\.+/, "");
|
||||
return (v || fallback).slice(0, MAX_NAME);
|
||||
}
|
||||
|
||||
let lastDirLabel = "";
|
||||
export function displayPath(folderName) {
|
||||
return `${lastDirLabel || "Documents"}/CamCat/${safeSeg(folderName, "folder")}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* เขียนไฟล์โดยไล่ directory ที่ใช้ได้:
|
||||
* DOCUMENTS (Android ≤10 / เข้าถึงจากแอปอื่นได้) → EXTERNAL (app-specific, ใช้ได้ทุกเวอร์ชัน)
|
||||
*/
|
||||
async function writeWithFallback(fs, path, data) {
|
||||
const tries = [
|
||||
{ directory: "DOCUMENTS", label: "Documents" },
|
||||
{ directory: "EXTERNAL", label: "Android/data/…/files" },
|
||||
];
|
||||
let lastErr = null;
|
||||
for (const t of tries) {
|
||||
try {
|
||||
const res = await fs.writeFile({ path, data, directory: t.directory, recursive: true });
|
||||
lastDirLabel = t.label;
|
||||
return res?.uri || "ok";
|
||||
} catch (err) { lastErr = err; }
|
||||
}
|
||||
throw lastErr || new Error("write failed");
|
||||
}
|
||||
|
||||
function blobToBase64(blob) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => {
|
||||
const s = String(r.result || "");
|
||||
resolve(s.slice(s.indexOf(",") + 1));
|
||||
};
|
||||
r.onerror = () => reject(r.error);
|
||||
r.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
|
||||
/** เขียนรูปลงโฟลเดอร์จริงบนเครื่อง: Documents/CamCat/<โฟลเดอร์>/<ไฟล์>.jpg */
|
||||
export async function savePhoto(folderName, fileName, blob) {
|
||||
if (!isNative()) return null;
|
||||
const fs = plugin("Filesystem");
|
||||
if (!fs) return null;
|
||||
try {
|
||||
const data = await blobToBase64(blob);
|
||||
return await writeWithFallback(fs, `CamCat/${safeSeg(folderName, "folder")}/${safeSeg(fileName, "photo.jpg")}`, data);
|
||||
} catch (err) {
|
||||
console.warn("native savePhoto failed", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** เขียนไฟล์ ZIP ลง Documents/CamCat-ZIP */
|
||||
export async function saveZip(file) {
|
||||
if (!isNative()) return null;
|
||||
const fs = plugin("Filesystem");
|
||||
if (!fs) return null;
|
||||
try {
|
||||
const data = await blobToBase64(file);
|
||||
return await writeWithFallback(fs, `CamCat-ZIP/${safeSeg(file.name, "photos.zip")}`, data);
|
||||
} catch (err) {
|
||||
console.warn("native saveZip failed", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* แชร์ไฟล์ (รูปเดี่ยว/หลายรูป/ZIP) ผ่าน share sheet ของ Android
|
||||
* items = [{ name, blob }]
|
||||
* หมายเหตุ: Android แชร์ "โฟลเดอร์" ทั้งโฟลเดอร์เป็น URI ไม่ได้ → ใช้หลายไฟล์ หรือ ZIP
|
||||
*/
|
||||
export async function shareItems(items, title = "") {
|
||||
if (!isNative()) return { ok: false, reason: "notnative" };
|
||||
const fs = plugin("Filesystem");
|
||||
const share = plugin("Share");
|
||||
if (!fs || !share) return { ok: false, reason: "noplugin" };
|
||||
|
||||
try {
|
||||
const uris = [];
|
||||
for (const it of items) {
|
||||
const data = await blobToBase64(it.blob);
|
||||
const res = await fs.writeFile({
|
||||
path: `share/${Date.now()}-${safeSeg(it.name, "file")}`,
|
||||
data,
|
||||
directory: "CACHE",
|
||||
recursive: true,
|
||||
});
|
||||
if (res?.uri) uris.push(res.uri);
|
||||
}
|
||||
if (!uris.length) return { ok: false, reason: "nofiles" };
|
||||
|
||||
await share.share({
|
||||
title: title || (items.length === 1 ? items[0].name : "CamCat"),
|
||||
dialogTitle: title || "แชร์ด้วย",
|
||||
files: uris,
|
||||
});
|
||||
return { ok: true };
|
||||
} catch (err) {
|
||||
const msg = String(err?.message || err);
|
||||
if (/cancel/i.test(msg)) return { ok: false, reason: "cancelled" };
|
||||
console.warn("native share failed", err);
|
||||
return { ok: false, reason: "failed", error: err };
|
||||
}
|
||||
}
|
||||
|
||||
/** จำนวนไฟล์ที่บันทึกลงเครื่องแล้ว (ใช้โชว์ใน diagnostics) */
|
||||
export function nativeInfo() {
|
||||
return {
|
||||
native: isNative(),
|
||||
platform: platform(),
|
||||
label: isNative() ? `APK (${platform()})` : "เว็บ / PWA",
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user