42 lines
1.3 KiB
TypeScript

interface RawData {
uri: string;
exif?: Record<string, any>;
location?: {
latitude?: string | number;
};
}
export function transformData(data: RawData): RawData {
const result = { ...data };
if (result.exif) {
const newExif: Record<string, any> = {};
for (const key in result.exif) {
const value = result.exif[key];
// 普通对象:{Exif}, {TIFF}, {XMP} 等
if (typeof value === 'object' && !Array.isArray(value)) {
if (key === '{GPS}') {
// 处理 GPS 字段:所有子字段加前缀 "GPS"
for (const subKey in value) {
const newKey = 'GPS' + subKey; // 所有字段都加前缀
newExif[newKey] = value[subKey];
}
} else {
// 其它嵌套对象直接展开字段
for (const subKey in value) {
newExif[subKey] = value[subKey];
}
}
} else {
// 非对象字段保留原样
newExif[key] = value;
}
}
result.exif = newExif;
}
// 最后将result的exif信息平铺
return { ...result, ...result.exif, exif: undefined };
}