fix: improve cache management and sticker deletion
Some checks failed
Frontend CI / ota-android (push) Successful in 4m46s
Frontend CI / ota-ios (push) Successful in 3m24s
Frontend CI / build-and-push-web (push) Failing after 24m8s
Frontend CI / build-android-apk (push) Successful in 34m17s

Update cache directory scanning to use platform-specific paths for iOS and Android.
Add a delay after clearing cache to ensure native deletion completes.
Correct the request body structure for sticker deletion API calls.
This commit is contained in:
2026-06-15 03:58:15 +08:00
parent 97477c3471
commit d8ef51fa13
2 changed files with 19 additions and 10 deletions

View File

@@ -63,13 +63,19 @@ export const DataStorageScreen: React.FC = () => {
if (Platform.OS === 'web') return 0;
try {
const cacheDir = Paths.cache;
const sdImageCacheDir = new Directory(cacheDir, 'defaultDiskCache');
const expoImageCacheDir = new Directory(cacheDir, 'expo-image');
// expo-image 在底层使用原生图片库,磁盘缓存目录名与平台相关:
// - iOSSDWebImage: {cache}/com.hackemist.SDImageCache/
// - AndroidGlide: {cache}/image_manager_disk_cache/
// 同时保留旧的猜测路径作为兜底,避免未来库升级改路径时彻底失效。
const candidateDirNames =
Platform.OS === 'ios'
? ['com.hackemist.SDImageCache', 'defaultDiskCache', 'expo-image']
: ['image_manager_disk_cache', 'expo-image', 'defaultDiskCache'];
const scanDirectorySize = async (dir: Directory): Promise<number> => {
let size = 0;
try {
if (!(await dir.exists)) return 0;
if (!dir.exists) return 0;
const items = dir.list();
for (const item of items) {
try {
@@ -87,7 +93,10 @@ export const DataStorageScreen: React.FC = () => {
return size;
};
const totalSize = await scanDirectorySize(sdImageCacheDir) + await scanDirectorySize(expoImageCacheDir);
let totalSize = 0;
for (const name of candidateDirNames) {
totalSize += await scanDirectorySize(new Directory(cacheDir, name));
}
return totalSize;
} catch (error) {
console.warn('读取 expo-image 缓存大小失败:', error);
@@ -144,6 +153,9 @@ export const DataStorageScreen: React.FC = () => {
await mediaCacheManager.clearAll();
await Image.clearDiskCache();
await Image.clearMemoryCache();
// expo-image 的磁盘缓存清理由原生库SDWebImage / Glide在后台线程执行
// 立即读取目录大小可能仍是清理前的值,这里短暂等待以确保文件落盘删除完成。
await new Promise((resolve) => setTimeout(resolve, 300));
await loadStats();
Alert.alert('完成', '缓存已清除');
} catch (error) {

View File

@@ -85,9 +85,8 @@ export const addStickerFromUrl = async (
*/
export const deleteSticker = async (stickerId: string): Promise<boolean> => {
try {
await api.delete('/stickers', {
data: { sticker_id: stickerId },
});
// api.delete(path, body) 的第二个参数直接作为请求体,不需要 axios 风格的 { data } 包装
await api.delete('/stickers', { sticker_id: stickerId });
return true;
} catch (error) {
console.error('删除自定义表情失败:', error);
@@ -130,9 +129,7 @@ export const batchDeleteStickers = async (stickerIds: string[]): Promise<{ succe
for (const stickerId of stickerIds) {
try {
await api.delete('/stickers', {
data: { sticker_id: stickerId },
});
await api.delete('/stickers', { sticker_id: stickerId });
success++;
} catch (error) {
console.error(`删除表情 ${stickerId} 失败:`, error);