fix: improve cache management and sticker deletion
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:
@@ -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 在底层使用原生图片库,磁盘缓存目录名与平台相关:
|
||||
// - iOS(SDWebImage): {cache}/com.hackemist.SDImageCache/
|
||||
// - Android(Glide): {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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user