u
This commit is contained in:
@@ -54,7 +54,7 @@ export function getMimeType(filePath) {
|
||||
|
||||
export function extractFullHash(p) {
|
||||
if (typeof p !== 'string' || !p.trim()) return null;
|
||||
const clean = p.trim().replace(/^([a-zA-Z]:|\/)+/, '');
|
||||
const clean = p.trim().replace(/^vfs:\/+/i, '').replace(/^([a-zA-Z]:|\/)+/, '');
|
||||
const parts = clean.split('/');
|
||||
const last = parts[parts.length - 1];
|
||||
const withoutExt = last.replace(/\.[a-zA-Z0-9]+$/, '');
|
||||
@@ -342,13 +342,14 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
readFile: (p, opt, cb) => {
|
||||
const callback = typeof opt === 'function' ? opt : cb;
|
||||
const options = typeof opt === 'function' ? undefined : opt;
|
||||
try {
|
||||
const data = vfs.readFileSync(p, options);
|
||||
if (typeof callback === 'function') process.nextTick(callback, null, data);
|
||||
} catch (err) {
|
||||
if (typeof callback === 'function') process.nextTick(callback, err);
|
||||
else throw err;
|
||||
}
|
||||
vfs.promises.readFile(p, options)
|
||||
.then((data) => {
|
||||
if (typeof callback === 'function') process.nextTick(callback, null, data);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (typeof callback === 'function') process.nextTick(callback, err);
|
||||
else throw err;
|
||||
});
|
||||
},
|
||||
|
||||
createReadStream: (p, opt) => {
|
||||
@@ -476,7 +477,34 @@ export function createVfs(vfsRoot, options = {}) {
|
||||
},
|
||||
|
||||
promises: {
|
||||
readFile: async (p, opt) => vfs.readFileSync(p, opt),
|
||||
readFile: async (p, opt) => {
|
||||
const hash = extractFullHash(p);
|
||||
if (hash) {
|
||||
const buf = syncFetchImage(hash);
|
||||
if (buf) {
|
||||
const encoding = typeof opt === 'string' ? opt : opt?.encoding;
|
||||
return encoding ? buf.toString(encoding) : buf;
|
||||
}
|
||||
if (googleAuthId) {
|
||||
try {
|
||||
const store = await getStore();
|
||||
const img = await store.pic.getImageByHash(googleAuthId, hash);
|
||||
if (img?.buffer) {
|
||||
try {
|
||||
const cachePath = path.join(vfsRoot, 'images', `${hash}.avif`);
|
||||
fs.mkdirSync(path.join(vfsRoot, 'images'), { recursive: true });
|
||||
fs.writeFileSync(cachePath, img.buffer);
|
||||
} catch { /* ignore cache write error */ }
|
||||
const encoding = typeof opt === 'string' ? opt : opt?.encoding;
|
||||
return encoding ? img.buffer.toString(encoding) : img.buffer;
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
}
|
||||
return vfs.readFileSync(p, opt);
|
||||
},
|
||||
writeFile: async (p, data, opt) => vfs.writeFileSync(p, data, opt),
|
||||
appendFile: async (p, data, opt) => vfs.appendFileSync(p, data, opt),
|
||||
stat: async (p, opt) => vfs.statSync(p, opt),
|
||||
|
||||
Reference in New Issue
Block a user