react-native-mask-segment-c.../dist/utils/resolveAssetPath.js
a1518 acd6d3a73d feat: add MaskSegmentApp source code and config
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 00:27:28 -07:00

56 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Image, Platform } from 'react-native';
import RNFS from 'react-native-fs';
import { ensurePngFile, toPngFileName } from './pngImage';
/** 将 require() 资源解析为 PNG 本地路径OpenCV / RNFS 可读) */
export async function resolveAssetPath(assetModule, cacheFileName) {
const pngCacheName = toPngFileName(cacheFileName);
const source = Image.resolveAssetSource(assetModule);
if (!source?.uri) {
throw new Error('Cannot resolve image resource');
}
const { uri } = source;
if (uri.startsWith('file://')) {
return ensurePngFile(uri, pngCacheName);
}
if (Platform.OS === 'ios' && uri.startsWith('/')) {
return ensurePngFile(uri, pngCacheName);
}
if (Platform.OS === 'ios' && uri.startsWith('/')) {
return ensurePngFile(uri, pngCacheName);
}
if (uri.startsWith('http://') || uri.startsWith('https://')) {
const tmpDest = `${RNFS.CachesDirectoryPath}/tmp_${Date.now()}_${pngCacheName}`;
const { statusCode } = await RNFS.downloadFile({
fromUrl: uri,
toFile: tmpDest,
}).promise;
if (statusCode !== 200) {
throw new Error(`Download resource failed: ${pngCacheName}`);
}
try {
return await ensurePngFile(tmpDest, pngCacheName);
}
finally {
if (await RNFS.exists(tmpDest)) {
await RNFS.unlink(tmpDest);
}
}
}
if (Platform.OS === 'android') {
const assetPath = uri
.replace('asset:/', '')
.replace('file:///android_asset/', '');
const tmpDest = `${RNFS.CachesDirectoryPath}/tmp_${Date.now()}_${pngCacheName}`;
await RNFS.copyFileAssets(assetPath, tmpDest);
try {
return await ensurePngFile(tmpDest, pngCacheName);
}
finally {
if (await RNFS.exists(tmpDest)) {
await RNFS.unlink(tmpDest);
}
}
}
return ensurePngFile(uri, pngCacheName);
}
//# sourceMappingURL=resolveAssetPath.js.map