104 lines
5.1 KiB
JavaScript
104 lines
5.1 KiB
JavaScript
/* 本地文件图片引用(2026-11-26)
|
||
*
|
||
* 为什么单独成模块:相册要「只做引用、不复制字节」——节点里只存原文件绝对路径
|
||
* ({__file: '/abs/a.png'}),生成/预览时才按需读盘。这样 22 张 4K 图 = 画布 JSON 2.6KB,
|
||
* 而不是往 images/canvas/ 复制 176MB。
|
||
*
|
||
* 边界:
|
||
* · 只做「路径 ↔ 字节」这一层,不碰画布/存储/CSS;宿主 app.js 通过 wImgRefDataUrl 等入口调用
|
||
* · 非桌面端(网页版)没有 __TAURI__ ⇒ available() false,调用方必须自己降级为复制
|
||
* · fs 权限是运行时的:pick() 选完会把路径加进 fs scope(Rust grant_paths),重启后失效需重新选择
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const g = global || {}
|
||
const MIME = { png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', avif: 'image/avif', bmp: 'image/bmp', svg: 'image/svg+xml' }
|
||
const extOf = (p) => { const m = /\.([a-z0-9]{1,5})$/i.exec(String(p || '')); return m ? m[1].toLowerCase() : '' }
|
||
const mimeOf = (p) => MIME[extOf(p)] || 'application/octet-stream'
|
||
const nameOf = (p) => String(p || '').replace(/^.*[\\/]/, '')
|
||
const is = (v) => !!(v && typeof v === 'object' && !Array.isArray(v) && typeof v.__file === 'string' && v.__file)
|
||
const T = () => (typeof window !== 'undefined' ? window.__TAURI__ : null)
|
||
const fsApi = () => { const t = T(); return t && t.fs ? t.fs : null }
|
||
const available = () => { const f = fsApi(); return !!(f && typeof f.readFile === 'function') }
|
||
|
||
async function readBytes (path) {
|
||
const f = fsApi()
|
||
if (!f || !path) return null
|
||
try { const u8 = await f.readFile(String(path)); return u8 || null } catch (e) { return null }
|
||
}
|
||
|
||
// 手工 base64(不依赖 FileReader/btoa 的边界行为;Uint8Array → 分块拼,避免超长 apply 爆栈)
|
||
function toBase64 (u8) {
|
||
const bytes = (u8 && u8.length !== undefined) ? u8 : new Uint8Array(0)
|
||
const CHUNK = 0x8000
|
||
let bin = ''
|
||
for (let i = 0; i < bytes.length; i += CHUNK) bin += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK))
|
||
try { return btoa(bin) } catch (e) { return '' }
|
||
}
|
||
|
||
async function readDataUrl (path) {
|
||
const u8 = await readBytes(path)
|
||
if (!u8 || !u8.length) return ''
|
||
const b64 = toBase64(u8)
|
||
return b64 ? ('data:' + mimeOf(path) + ';base64,' + b64) : ''
|
||
}
|
||
|
||
async function readBlob (path) {
|
||
const u8 = await readBytes(path)
|
||
if (!u8 || !u8.length) return null
|
||
try { return new Blob([u8], { type: mimeOf(path) }) } catch (e) { return null }
|
||
}
|
||
|
||
// 失联检测:相册打开画布时用它给「图片已不在原位置」的格子打标
|
||
async function stat (path) {
|
||
const f = fsApi()
|
||
if (!f || !path) return { ok: false, exists: false, reason: 'no-fs' }
|
||
try {
|
||
const exists = (typeof f.exists === 'function') ? await f.exists(String(path)) : true
|
||
if (!exists) return { ok: true, exists: false }
|
||
const st = (typeof f.stat === 'function') ? await f.stat(String(path)) : null
|
||
return { ok: true, exists: true, size: st && Number(st.size) || 0, mtime: st && Number(st.mtime) || 0, name: nameOf(path) }
|
||
} catch (e) { return { ok: false, exists: false, reason: 'stat-failed' } }
|
||
}
|
||
|
||
// 把用户选中的路径加进运行时 fs scope(Rust 侧 grant_paths)
|
||
async function grant (paths) {
|
||
const t = T(); const list = (Array.isArray(paths) ? paths : [paths]).filter((p) => typeof p === 'string' && p)
|
||
if (!t || !t.core || typeof t.core.invoke !== 'function' || !list.length) return []
|
||
try { return await t.core.invoke('grant_paths', { paths: list }) || [] } catch (e) { return [] }
|
||
}
|
||
|
||
// 系统选择器:返回绝对路径数组(桌面端才有路径;网页版返回空 ⇒ 调用方降级复制)
|
||
async function pick (opts) {
|
||
const t = T(); const dlg = t && t.dialog
|
||
if (!dlg || typeof dlg.open !== 'function') return []
|
||
const o = opts || {}
|
||
let picked = null
|
||
try {
|
||
picked = await dlg.open({
|
||
multiple: o.multiple !== false,
|
||
directory: o.directory === true,
|
||
title: String(o.title || (o.directory ? '选择图片文件夹' : '选择图片')),
|
||
filters: o.filters || [{ name: '图片', extensions: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'avif', 'bmp', 'svg'] }],
|
||
})
|
||
} catch (e) { return [] }
|
||
if (!picked) return []
|
||
const list = Array.isArray(picked) ? picked : [picked]
|
||
await grant(list)
|
||
return list.filter((p) => typeof p === 'string' && p)
|
||
}
|
||
|
||
// 单张引用:读 stat 拿元信息(不读字节)——相册导入时用
|
||
async function reference (path) {
|
||
const p = String(path || '')
|
||
if (!p) return { ok: false, reason: 'empty-path' }
|
||
const st = await stat(p)
|
||
if (!st.ok) return { ok: false, reason: st.reason || 'stat-failed', path: p }
|
||
if (!st.exists) return { ok: false, reason: 'not-found', path: p }
|
||
return { ok: true, path: p, name: nameOf(p), size: st.size, mtime: st.mtime }
|
||
}
|
||
|
||
const API = Object.freeze({ is, available, mimeOf, nameOf, readBytes, readDataUrl, readBlob, stat, grant, pick, reference })
|
||
global.IWBImageFileRef = API
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|