file extension cleanup

This commit is contained in:
balzack 2024-06-10 23:22:24 -07:00
parent 8c609d2f9d
commit f9538c0927

View File

@ -29,6 +29,27 @@ export function useImageAsset(asset) {
setState((s) => ({ ...s, ...value }));
}
const getExtension = async (path) => {
const block = await RNFS.read(path, 8, 0, 'base64');
if (block === '/9j/4AAQSkY=') {
return 'jpg';
}
if (block === 'iVBORw0KGgo=') {
return 'png';
}
if (block === 'R0lGODlhIAM=') {
return 'gif';
}
if (block.startsWith('UklGR')) {
return 'webp';
}
else if (block.startsWith('Qk')) {
return 'bmp';
}
console.log("unknown prefix: ", block);
return 'dat';
}
useEffect(() => {
if (state.loaded) {
const frameRatio = state.frameWidth / state.frameHeight;
@ -82,28 +103,13 @@ export function useImageAsset(asset) {
else {
await RNFS.downloadFile({ fromUrl: state.url, toFile: path }).promise;
}
let ext = 'dat';
const block = await RNFS.read(path, 8, 0, 'base64');
if (block === '/9j/4AAQSkY=') {
ext = 'jpg';
}
if (block === 'iVBORw0KGgo=') {
ext = 'png';
}
if (block === 'UklGRphXAQA=') {
ext = 'webp';
}
if (block === 'R0lGODlhIAM=') {
ext = 'gif';
}
else if (block.startsWith('Qk')) {
ext = 'bmp';
}
const ext = await getExtension(path);
const fullPath = `${path}.${ext}`
if (await RNFS.exists(fullPath)) {
await RNFS.unlink(fullPath);
}
await RNFS.moveFile(path, fullPath)
Share.open({ url: fullPath });
},
download: async () => {
@ -118,23 +124,7 @@ export function useImageAsset(asset) {
else {
await RNFS.downloadFile({ fromUrl: state.url, toFile: path }).promise;
}
let ext = 'dat';
const block = await RNFS.read(path, 8, 0, 'base64');
if (block === '/9j/4AAQSkY=') {
ext = 'jpg';
}
if (block === 'iVBORw0KGgo=') {
ext = 'png';
}
if (block === 'UklGRphXAQA=') {
ext = 'webp';
}
if (block === 'R0lGODlhIAM=') {
ext = 'gif';
}
else if (block.startsWith('Qk')) {
ext = 'bmp';
}
const ext = await getExtension(path);
await RNFS.moveFile(path, `${path}.${ext}`);
if (Platform.OS !== 'ios') {
await RNFS.scanFile(`${path}.${ext}`);