mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-01-11 22:40:36 +00:00
Move cacheDir creation to storage_provider
- Move the cacheDir creation to storage_provider from `others.dart`, `custom_extended_image_provider.dart` and `storage_usage.dart`. - Use the correct directory, `getApplicationCacheDirectory()` instead of the `getTemporaryDirectory()` (which is being deleted by the OS regularly) for cache files. - remove the `_cacheDownloadPath` from `storage_usage.dart` as the path is never being created in the first place, so using that path in `clearCache()` and `_getTotalDiskSpace()` is unnecessary.
This commit is contained in:
parent
67dee18776
commit
0ed8ee2cd2
4 changed files with 25 additions and 39 deletions
|
|
@ -5,8 +5,6 @@ import 'package:mangayomi/models/settings.dart';
|
|||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/providers/storage_provider.dart';
|
||||
import 'package:mangayomi/router/router.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
part 'storage_usage.g.dart';
|
||||
|
||||
|
|
@ -21,28 +19,19 @@ class TotalChapterCacheSizeState extends _$TotalChapterCacheSizeState {
|
|||
return "0.00 B";
|
||||
}
|
||||
|
||||
final String _cacheImageMangaPath = join('Mangayomi', 'cacheimagemanga');
|
||||
final String _cacheDownloadPath = join('Mangayomi', 'downloads');
|
||||
final _storage = StorageProvider();
|
||||
|
||||
Future<void> clearCache({bool showToast = true}) async {
|
||||
final tempPath = (await getTemporaryDirectory()).path;
|
||||
String? msg;
|
||||
try {
|
||||
final dir = Directory(join(tempPath, _cacheImageMangaPath));
|
||||
final dir = await _storage.getCacheDirectory('cacheimagemanga');
|
||||
if (dir.existsSync()) {
|
||||
await dir.delete(recursive: true);
|
||||
}
|
||||
msg = "0.00 B";
|
||||
} catch (_) {}
|
||||
try {
|
||||
final dir = Directory(join(tempPath, _cacheDownloadPath));
|
||||
if (dir.existsSync()) {
|
||||
await dir.delete(recursive: true);
|
||||
}
|
||||
msg = "0.00 B";
|
||||
} catch (_) {}
|
||||
try {
|
||||
await StorageProvider().deleteTmpDirectory();
|
||||
await _storage.deleteTmpDirectory();
|
||||
} catch (_) {}
|
||||
if (msg != null && showToast) {
|
||||
state = msg;
|
||||
|
|
@ -53,14 +42,10 @@ class TotalChapterCacheSizeState extends _$TotalChapterCacheSizeState {
|
|||
}
|
||||
|
||||
Future<int> _getTotalDiskSpace() async {
|
||||
final tempPath = (await getTemporaryDirectory()).path;
|
||||
try {
|
||||
return await _getdirectorySize(
|
||||
Directory(join(tempPath, _cacheImageMangaPath)),
|
||||
) +
|
||||
await _getdirectorySize(
|
||||
Directory(join(tempPath, _cacheDownloadPath)),
|
||||
);
|
||||
await _storage.getCacheDirectory('cacheimagemanga'),
|
||||
);
|
||||
} catch (_) {}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import 'package:http_client_helper/http_client_helper.dart';
|
|||
import 'package:mangayomi/providers/storage_provider.dart';
|
||||
import 'package:mangayomi/services/http/m_client.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:extended_image_library/src/network/extended_network_image_provider.dart'
|
||||
as image_provider;
|
||||
|
||||
|
|
@ -214,7 +213,7 @@ class CustomExtendedNetworkImageProvider
|
|||
@override
|
||||
final bool printError;
|
||||
|
||||
/// The max duration to cahce image.
|
||||
/// The max duration to cache image.
|
||||
/// After this time the cache is expired and the image is reloaded.
|
||||
@override
|
||||
final Duration? cacheMaxAge;
|
||||
|
|
@ -317,15 +316,9 @@ class CustomExtendedNetworkImageProvider
|
|||
return cachedData;
|
||||
}
|
||||
|
||||
final Directory cacheImagesDirectory = Directory(
|
||||
join(
|
||||
(await getTemporaryDirectory()).path,
|
||||
'Mangayomi',
|
||||
imageCacheFolderName ?? 'cacheimagecover',
|
||||
),
|
||||
);
|
||||
final Directory cacheImagesDirectory = await StorageProvider()
|
||||
.createCacheDirectory(imageCacheFolderName);
|
||||
Uint8List? data;
|
||||
await StorageProvider().createDirectorySafely(cacheImagesDirectory.path);
|
||||
final File cacheFile = File(join(cacheImagesDirectory.path, md5Key));
|
||||
|
||||
// exist, try to find cache image file
|
||||
|
|
|
|||
|
|
@ -86,6 +86,20 @@ class StorageProvider {
|
|||
return Directory(tmpPath);
|
||||
}
|
||||
|
||||
Future<Directory> getCacheDirectory(String? imageCacheFolderName) async {
|
||||
final cacheImagesDirectory = path.join(
|
||||
(await getApplicationCacheDirectory()).path,
|
||||
imageCacheFolderName ?? 'cacheimagecover',
|
||||
);
|
||||
return Directory(cacheImagesDirectory);
|
||||
}
|
||||
|
||||
Future<Directory> createCacheDirectory(String? imageCacheFolderName) async {
|
||||
final cachePath = await getCacheDirectory(imageCacheFolderName);
|
||||
await createDirectorySafely(cachePath.path);
|
||||
return cachePath;
|
||||
}
|
||||
|
||||
Future<String> _tempDirectoryPath() async {
|
||||
final defaultDirectory = await getDirectory();
|
||||
return path.join(defaultDirectory!.path, 'tmp');
|
||||
|
|
|
|||
|
|
@ -10,10 +10,9 @@ import 'package:intl/intl.dart';
|
|||
import 'package:mangayomi/modules/manga/reader/u_chap_data_preload.dart';
|
||||
import 'package:mangayomi/modules/more/settings/reader/providers/reader_state_provider.dart';
|
||||
import 'package:mangayomi/modules/widgets/custom_extended_image_provider.dart';
|
||||
import 'package:mangayomi/providers/storage_provider.dart';
|
||||
import 'package:mangayomi/utils/headers.dart';
|
||||
import 'package:mangayomi/utils/reg_exp_matcher.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
extension FileFormatter on num {
|
||||
|
|
@ -144,13 +143,8 @@ extension UChapDataPreloadExtensions on UChapDataPreload {
|
|||
Future<File?> _getCachedImageFile(String url, {String? cacheKey}) async {
|
||||
try {
|
||||
final String key = cacheKey ?? keyToMd5(url);
|
||||
final Directory cacheImagesDirectory = Directory(
|
||||
join(
|
||||
(await getTemporaryDirectory()).path,
|
||||
'Mangayomi',
|
||||
'cacheimagemanga',
|
||||
),
|
||||
);
|
||||
final Directory cacheImagesDirectory = await StorageProvider()
|
||||
.getCacheDirectory('cacheimagemanga');
|
||||
if (cacheImagesDirectory.existsSync()) {
|
||||
await for (final FileSystemEntity file in cacheImagesDirectory.list()) {
|
||||
if (file.path.endsWith(key)) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue